简体   繁体   English

使用 OpenCV 和 Numpy 从另一个图像中提取的像素坐标创建多通道图像

[英]Create a multi-channel image from pixel coordinates extracted from another image with OpenCV and Numpy

I have a list of mask images with the shape of(3100,3100,3),我有一个形状为(3100,3100,3)的蒙版图像列表,

I want to create a new image with 5 channels from each image to represent the我想从每个图像中创建一个包含 5 个通道的新图像来表示

different colours in each mask image.每个蒙版图像中的不同颜色。 I searched for the specific colours in each image我搜索了每张图片中的特定颜色

to get the pixel coordinates of each colour,获取每种颜色的像素坐标,

after that I want to insert these pixels into the new 5 channel image:之后我想将这些像素插入到新的 5 通道图像中:

mask = cv2.imread('path_to_images/mask_0_1.png')
plt.imshow(mask)

蒙版图像

# Define the colours

green= [0,255,0]
blue=[ 0, 0, 255]
yel=[255, 255, 0]
red=[ 255, 0, 0]
white=[255, 255, 255]


# create the new image with 5 channels
new_img=np.zeros( ( np.array(mask).shape[0], np.array(mask).shape[1], 5 ) )


# find each colour coordinate


new_img[0,0,1]=np.where(np.all(mask==green,axis=2))
new_img[0,0,2]=np.where(np.all(mask==blue,axis=2))
new_img[0,0,3]=np.where(np.all(mask==yel,axis=2))
new_img[0,0,4]=np.where(np.all(mask==red,axis=2))
new_img[0,0,5]=np.where(np.all(mask==white,axis=2))

it shows this error, how can I create this 5 channel image?它显示了这个错误,我怎样才能创建这个 5 通道图像?

      > TypeError                                 Traceback (most recent call last)
     TypeError: float() argument must be a string or a number, not 'tuple'

     The above exception was the direct cause of the following exception:

     ValueError                                Traceback (most recent call last)
       <ipython-input-197-5b9c268857b2> in <module>
        19 new_img=np.zeros( ( np.array(mask).shape[0], np.array(mask).shape[1], 5 ) )
          20 
       ---> 21 new_img[0,0,1]=np.where(np.all(mask==benign,axis=2))
         22 new_img[0,0,2]=np.where(np.all(mask==blue,axis=2))
          23 new_img[0,0,3]=np.where(np.all(mask==yel,axis=2))

      ValueError: setting an array element with a sequence.

Python has zero-based indices. Python 具有从零开始的索引。 When using five colors, the first color has index 0 and the last index 4.使用五个 colors 时,第一个颜色的索引为 0,最后一个颜色的索引为 4。

  • new_img[0,0,1] will access first element of the second channel (out of five) new_img[0,0,1]将访问第二个通道的第一个元素(五个)
  • new_img[...,0] will access all elements yx of the first channel new_img[...,0]将访问第一个通道的所有元素 yx

Try the code below instead.请尝试下面的代码。

new_img[...,0]=np.all(mask==green,axis=2)
# ...
new_img[...,4]=np.all(mask==white,axis=2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM