繁体   English   中英

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

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

我有 3 个通道的图像。 我有 3 个通道的像素值,如果一个像素在其 3 个通道中具有这 3 个值,则它属于“A”类。 例如

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

基本上我想生成一个通道数组,该数组等于类数,每个类在特定通道中分开。 有什么有效的方法可以做到这一点吗?

您可以使用 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]])

这样您就可以创建二进制掩码。 还有那些可以与布尔运算符结合使用的运算符,例如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]])

在您的情况下,您可以遍历像素值的类别,并比较不同的通道:

>>> 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]])

最后一部分有效,因为您可以将数字与布尔值相乘( 4*True == 43*False == 0并且因为我假设您字典中的每个像素值都是唯一的。如果后者不不,您将总结类标识符。

稍微短一点的方法是重塑起始数组:

>>> 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]])

找到另一个解决方案:这里的图像是我们想要的图像(具有 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.

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