简体   繁体   English

使用2D遮罩遮罩BGR图像

[英]Masking BGR image using a 2D mask

I have a three-dimensional array (image) with shape (480, 640, 3) . 我有一个形状为(480, 640, 3) )的三维数组(图像(480, 640, 3) Here, the 3 refers to BGR color code. 在此,3表示BGR颜色代码。 I would like to place a mask over this image using the data from the array of the Red image. 我想使用红色图像数组中的数据在此图像上放置遮罩。 Depending on its value, certain pixels need to be masked. 根据其值,某些像素需要被遮罩。

Creating the mask works fine. 创建遮罩效果很好。 It behaves exactly as expected. 它的行为完全符合预期。 In order to apply the mask to the original image, I first apply the mask to the Blue and Green image. 为了将蒙版应用于原始图像,我首先将蒙版应用于蓝色和绿色图像。 All is still fine. 一切还好。 Now I stack the three masked arrays, which returns an array with shape (480, 640, 3) . 现在,我将三个蒙版数组堆叠在一起,这将返回形状为(480, 640, 3)的数组。 However, plotting this array using imshow results in the original image. 但是,使用imshow绘制此阵列imshow得到原始图像。 No sign of any mask. 没有任何面具的迹象。

Below I put my code. 下面我放我的代码。 The code works for any image size/shape. 该代码适用于任何图像尺寸/形状。 All you need to do is change the name "Whatever_image_you_like.png" to the name of any image on your pc. 您需要做的就是将名称"Whatever_image_you_like.png"更改为您电脑上任何图像的名称。

import numpy
import numpy.ma
import scipy.misc
import matplotlib.pyplot as plt

pixel_value = 130   #Value in range 0 to 255

image = scipy.misc.imread("Whatever_image_you_like.png")

#Extract Blue, Green, and Red image from original image
image_B = numpy.copy(image[:, :, 0])
image_G = numpy.copy(image[:, :, 1])
image_R = numpy.copy(image[:, :, 2])

#Define mask depending on pixel value in Red image
image_mask = numpy.empty([image.shape[0], image.shape[1]], dtype = bool)
image_mask[image_R < pixel_value] = False

#Apply mask to Blue, Green, and Red images
B_masked = numpy.ma.masked_array(image_B, mask = ~image_mask)
G_masked = numpy.ma.masked_array(image_G, mask = ~image_mask)
R_masked = numpy.ma.masked_array(image_R, mask = ~image_mask)

#Stack masked images together again
masked_image = numpy.ma.dstack((B_masked, G_masked, R_masked))

#Plot original image and masked version
fig = plt.figure()

ax1 = fig.add_subplot(2, 1, 1)
ax1.imshow(image)

ax2 = fig.add_subplot(2, 1, 2)
ax2.imshow(masked_image)

plt.show()

What am I doing wrong? 我究竟做错了什么? Is there a better way to approach this problem? 有没有更好的方法来解决此问题?

Try to use a mask with the same shape as the image (actually, this will be a 3D mask). 尝试使用与image形状相同的蒙版(实际上,这将是3D蒙版)。 After generating your image_mask , do 生成image_mask ,执行

# create mask with same dimensions as image
mask = numpy.zeros_like(image)

# copy your image_mask to all dimensions (i.e. colors) of your image
for i in range(3): 
    mask[:,:,i] = image_mask.copy()

# apply the mask to your image
masked_image = image[mask]

This way I avoid masked arrays in numpy for the time being. 这样,我暂时避免在numpy中使用掩码数组。

在类似情况下,这种替代方法可能会更容易:

image[image_mask,:] = np.nan

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

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