the*_*oli 4 python arrays opencv numpy
我有一个由三元组numpy.array矩阵nxm表示的图像(r,g,b),我想使用我自己的函数将其转换为灰度。
我的尝试无法将矩阵nxmx3转换为单个值nxm的矩阵,这意味着从[r,g,b]我得到的数组开始,[gray, gray, gray]但我需要gray.
即初始颜色通道:[150 246 98]。转换为灰色后:[134 134 134]。我需要的 : 134
我怎样才能做到这一点?
我的代码:
def grayConversion(image):
height, width, channel = image.shape
for i in range(0, height):
for j in range(0, width):
blueComponent = image[i][j][0]
greenComponent = image[i][j][1]
redComponent = image[i][j][2]
grayValue = 0.07 * blueComponent + 0.72 * greenComponent + 0.21 * redComponent
image[i][j] = grayValue
cv2.imshow("GrayScale",image)
return image
Run Code Online (Sandbox Code Playgroud)
这是一个工作代码:
def grayConversion(image):
grayValue = 0.07 * image[:,:,2] + 0.72 * image[:,:,1] + 0.21 * image[:,:,0]
gray_img = grayValue.astype(np.uint8)
return gray_img
orig = cv2.imread(r'C:\Users\Jackson\Desktop\drum.png', 1)
g = grayConversion(orig)
cv2.imshow("Original", orig)
cv2.imshow("GrayScale", g)
cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11852 次 |
| 最近记录: |