什么是错误:“ValueError:赋值目标是只读的”?

问题描述 投票:2回答:1

当我使用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:赋值目标是只读的”我怎么能在这里更改我的代码以避免它?

python rgb bgr
1个回答
0
投票

此行显示为冗余并导致错误,将其删除:

        grayImage = img
© www.soinside.com 2019 - 2024. All rights reserved.