简体   繁体   English

在图像中的所有 3 通道中查找具有特定值的像素

[英]Find pixel with specific values in its all 3-channel in an image

I have an image of 3 channels.我有 3 个通道的图像。 I have pixel values of 3 channels that if a pixel has these 3 values in its 3 channels then it belongs to class 'A'.我有 3 个通道的像素值,如果一个像素在其 3 个通道中具有这 3 个值,则它属于“A”类。 For example例如

classes_channel = np.zeros((image.shape[0], image.shape[1], num_classes))
pixel_class_dict={'0': [128, 64, 128], '1': [230, 50, 140]} #num_classes=2
for channel in range(num_classes):
    pixel_value= pixel_class_dict[str(channel)]
    for i in range(image.shape[0]):
        for j in range(image.shape[1]):
            if list(image[i][j])==pixel_value:
                classes_channel[i,j,channel]=1

Basically I want to generate an array of channels equal to number of classes with each class separate in a particular channel.基本上我想生成一个通道数组,该数组等于类数,每个类在特定通道中分开。 Is there any efficient way to do this?有什么有效的方法可以做到这一点吗?

You could use numpy's broadcasting to check more efficiently whether any value in the channels matches another value:您可以使用 numpy 的广播来更有效地检查频道中的任何值是否与另一个值匹配:

>>> a=np.arange(2*3).reshape(2,3)
>>> a
array([[0, 1, 2],
       [3, 4, 5]])
>>> a == 4
array([[False, False, False],
       [False,  True, False]])

This way you can create binary masks.这样您就可以创建二进制掩码。 And those you can combine with boolean operators, like np.logical_and and np.logical_or :还有那些可以与布尔运算符结合使用的运算符,例如np.logical_andnp.logical_or

>>> b = a == 4
>>> c = a == 0
>>> np.logical_and(b, c)
array([[False, False, False],
       [False, False, False]])
>>> np.logical_or(b, c)
array([[ True, False, False],
       [False,  True, False]])

In your case, you could loop over the classes of pixel values, and compare the different channels:在您的情况下,您可以遍历像素值的类别,并比较不同的通道:

>>> pixel_class_dict = {1: [18, 19, 20], 2: [9,10,11]}
>>> a = np.arange(2*4*3).reshape(2,4,3)
>>> b = np.zeros((a.shape[:2]), dtype=np.int)
>>> for pixel_class, pixel_values in pixel_class_dict.items():
...     mask = np.logical_and(*(a[..., channel] == pixel_values[channel] 
...       for channel in range(a.shape[-1])))
...     b += pixel_class*mask
...
>>> b
array([[0, 0, 0, 2],
       [0, 0, 1, 0]])

This last part works because you can multiply a number with a boolean value ( 4*True == 4 and 3*False == 0 and because I'm assuming each of the pixel values in your dictionary is unique. If the latter doesn't hold, you'll sum up the class identifiers.最后一部分有效,因为您可以将数字与布尔值相乘( 4*True == 43*False == 0并且因为我假设您字典中的每个像素值都是唯一的。如果后者不不,您将总结类标识符。

A slightly shorter approach would be to reshape the starting array:稍微短一点的方法是重塑起始数组:

>>> b = np.zeros((a.shape[:2]), dtype=np.int)
>>> a2 = a.reshape(-1, 3)
>>> for pixel_class, pixel_values in pixel_class_dict.items():
...     mask = (np.all(a2 == pixel_values, axis=1)
...             .reshape(b.shape))
...     b += mask * pixel_class
...
>>> b
array([[0, 0, 0, 4],
       [0, 0, 2, 0]])

Found another solution: Here image is the image (with 3 channels) to which we want to找到另一个解决方案:这里的图像是我们想要的图像(具有 3 个通道)

import numpy as np
import cv2
for class_id in pixel_class_dict:
    class_color = np.array(pixel_class_dict[class_id])
    classes_channel[:, :, int(class_id)] = cv2.inRange(image,class_color,class_color).astype('bool').astype('float32')                                  

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

相关问题 OpenCV 将图像读取为 3 通道图像,而 PIL 将图像读取为 1 通道图像 - OpenCV reads an image as a 3-channel image while PIL reads the same image as a 1-channel image 根据另一个图像中的像素值设置图像的 Alpha 通道 - Setting the alpha channel of an image, based on the pixel values in another image Python Pillow:如何从 1 通道图像生成 3 通道图像? - Python Pillow: how to produce 3-channel image from 1-channel image? 灰度图像的 OpenCV 读取返回 3 通道(颜色?)数组而不是单通道 - OpenCV imread of grayscale image returns 3-channel (color?) array instead of single-channel 使用 image_dataset_from_directory 加载灰度 png 返回 3 通道张量 - Loading grayscale pngs with image_dataset_from_directory returns a 3-channel tensor OpenCV Python:3通道float32图像读取的快速解决方案? - OpenCV Python: fast solution for 3-channel float32 image reading? 在 Python 中使用 U-net 与 3 通道输入图像进行图像分割 - Using U-net in Python with 3-channel input images for image segmentation 如何对图像的所有像素值求和 100? - How to sum 100 to all pixel values of an image? 使用具有3通道输入的HoughCircles时出错 - Error using HoughCircles with 3-channel input 如何以编程方式找到图像中特定特征的像素位置? - How do I programmatically find the pixel locations of specific features in an image?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM