当我使用cv2.imread()打开jpg文件时,它有时失败,这可能是由于我使用的BGR格式。所以我切换到PLT使用RGB。
import matplotlib.pyplot as plt
import numpy as np
def rgb_to_gray(img):
grayImage = np.zeros(img.shape)
R = np.array(img[:, :, 0])
G = np.array(img[:, :, 1])
B = np.array(img[:, :, 2])
R = (R *.299)
G = (G *.587)
B = (B *.114)
Avg = (R+G+B)
grayImage = img
for i in range(3):
grayImage[:,:,i] = Avg
return grayImage
image_file = 'C:\A.jpg';
img = plt.imread(image_file,0)
gray = rgb_to_gray(img).copy()
当我将图像转换为灰度时,我怎么会出错。 :“ValueError:赋值目标是只读的”我怎么能在这里更改我的代码以避免它?
此行显示为冗余并导致错误,将其删除:
grayImage = img