【发布时间】:2019-08-07 19:49:43
【问题描述】:
当我使用 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: assignment destination is read-only" 我怎样才能在这里更改我的代码以避免它?
【问题讨论】:
-
请添加完整的错误信息。
-
此代码似乎有效。 image_file 包含转义序列“\A”,您应该使用原始字符串或双 \\
-
grayImage = img的目的是什么? -
听起来
C:\A.jpg是只读的(权限或代码级别)。