gpt4 book ai didi

Python openCV : I get an unchanged image when I use cvtColor

转载 作者:行者123 更新时间:2023-12-02 17:50:24 25 4
gpt4 key购买 nike

我有一个 numpy 数组的图像,其形状为灰度(480,640)。

我想在图像上放置一个彩色蒙版,并且需要使图像具有相同的形状才能做到这一点,即 (480,640,3)。

这是我尝试过的:

print str(img.shape) +' '+ str(type(img)) +' '+ str(img.dtype)
# prints: (480, 640) <type 'numpy.ndarray'> uint8

cv2.cvtColor(img, cv2.COLOR_GRAY2BGR, img, 3)
# this line seems to have no effect although I set it to 3 channels

print str(img.shape) +' '+ str(type(img)) +' '+ str(img.dtype)
# prints: (480, 640) <type 'numpy.ndarray'> uint8

rowCounter = 0
while rowCounter < img.shape[0]:
columnCounter = 0
while columnCounter < img.shape[1]:
if img[rowCounter, columnCounter] == 0:
img[rowCounter, columnCounter] = [0, 0, 0]
else:
img[rowCounter, columnCounter] = [255, 255, 255]
columnCounter += 1
rowCounter += 1

好的,代码停在我要分配三个值 ([0, 0, 0]) 而不是单个值 (0) 的行上。错误信息如下:
ValueError: setting an array element with a sequence.

如何从单个值更改为三个值?有没有我找不到的功能?

谢谢!

最佳答案

最主要的是您需要将转换后的图像分配给一个新名称。
我不确定使用提供目标图像作为参数的 c++ 格式是否有效。我会按照通常的 python (cv2) 方式来分配一个名称(相同的名称很好)。
此外,您不需要分配 channel 数。转换类型负责这一点。

#            cv2.cvtColor(img, cv2.COLOR_GRAY2BGR, img, 3)
color_mask = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
这会让你得到你想要的形象吗?
顺便说一句,只要您使用 numpy/opencv,您可能想研究提高效率的方法。如果您对整个图像/numpy 数组进行单个像素访问,那是一个危险信号(对于 python 中的 opencv)。
下面是显示转换但随后忽略它并显示(据我了解)如何应用更有效掩码的代码。
复制粘贴工作(更高效)示例
import cv2
import numpy as np

# setup an original image (this will work for anyone without needing to load one)
shape = (480, 640)
img_gray = np.ndarray(shape, dtype=np.uint8)
img_gray.fill(127)
img_gray[0:40, 100:140] = 0 # some "off" values
cv2.imshow('original grayscale image', img_gray)
cv2.waitKey(0) # press any key to continue

# convert the gray image to color (not used. just to demonstrate)
img_color = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2BGR)
cv2.imshow('color converted grayscale image (not used. just to show how to use cvtColor)', img_color)
cv2.waitKey(0) # press any key to continue

# a simplified version of what your code did to apply a mask
# make a white image.
# and then set it to black wherever the original grayscale image is 0
img_color = np.ndarray(img_gray.shape + (3,), dtype=np.uint8)
img_color.fill(255)
cv2.imshow('base color image', img_color)
cv2.waitKey(0) # press any key to continue

# this is not the fastest way, but I think it's more logical until you need more speed
# the fastest way specifically to black out parts of the image would
# be np.bitwise_and(...)
black_points = np.where(img_gray == 0)
print('found {} points to mask'.format(len(black_points[0])))
img_color[black_points] = (0, 0, 0)

# a more efficient and still straightforward method you could use:
img_color[img_gray==0] = (0, 0, 0)
cv2.imshow('masked color image', img_color)
cv2.waitKey(0) # press any key to continue

# clean up explicitly
cv2.destroyAllWindows()

关于Python openCV : I get an unchanged image when I use cvtColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22990470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com