简体   繁体   English

如何在python中掩盖对象的轮廓

[英]how to mask the outline of an object in python

How to mask the object in an image, eg by outlining the shape of the object or store the array's index of all the pixels that are part of the object? 如何遮罩图像中的对象,例如,通过概述对象的形状或存储作为对象一部分的所有像素的数组索引? Since I will do other further processing only in the regions with the object, I want to 'get rid of' / ignore the background and only focus on the object itself. 由于我将仅在具有对象的区域中执行其他进一步的处理,因此我想“摆脱” /忽略背景,而只关注对象本身。

The two images (the objects are a metal ball and a light-scattering ball) are similar to what I will be working on. 这两个图像(对象是金属球和光散射球)与我将要处理的图像相似。 I will try to improve the contrast between the object and the background, and work on the supporting 'stand' that holds the object when I take any more images, but for the images given here, are there any ways to mask the object? 我将尝试改善对象与背景之间的对比度,并在拍摄更多图像时在支撑对象的支撑“架子”上进行工作,但是对于此处给出的图像,是否有任何方法可以掩盖对象?

Originally, I wanted to threshold the image so that pixels that are 'brighter' than than a certain value will be kept, then use numpy.nonzero(threshold_image) to find out all the index of the pixels that are parts of the image. 最初,我想对图像进行阈值处理,以便保留比特定值“更亮”的像素,然后使用numpy.nonzero(threshold_image)找出属于图像部分的所有像素索引。 However, I find that it doesn't work well for such 'noisy' image. 但是,我发现它对于这种“嘈杂”的图像效果不佳。

thanks 谢谢

金属球散射球

Storing masks for arrays can theoretically be done with Numpy's masked arrays , but I wouldn't recommend it (performance-wise it's quite slow, I'll edit my post with timings later) 从理论上讲,可以使用Numpy的masked数组存储数组的掩码 ,但是我不建议这样做(性能方面它相当慢,稍后我将对定时进行编辑)

The most Pythonic way (thus coolest) is just storing a plain bool array of same size as image, and using the * operator to combine it (because numbers multiplied by False becomes 0, with the notable exception of NaN 最Python化的方式(因此最酷)是存储与图像大小相同的纯布尔数组,并使用*运算符对其进行组合(因为数字乘以False变为0,但NaN明显例外)

# creates an array of bool of same shape as image
maskAboveThreshold = image > 30 

# Show a 'cropped' version on the image
from matplotlib import pyplot as plt
plt.imshow(image * maskAboveThreshold, cmap='gray')

Note that I assumed here that image is a 2D image in grayscale, but as long as your mask shape is identical to the image, it works in any dimension. 请注意,我在这里假设image是灰度的2D图像,但是只要您的蒙版形状与图像相同,它就可以在任何尺寸上使用。

As for the separation of image, I don't know how familiar you are with Image Processing techniques, but in general, in the cases where you find noise to be bothersome, blurring your image a little before anything else is usually effective, as the implied averaging over small area means the noise (spikes) are smoothed. 至于图像的分离,我不知道您对图像处理技术有多熟悉,但是通常,在您发现噪音很烦人的情况下,通常在其他任何方法都没有效果之前先模糊图像,因为在小面积上进行隐式平均意味着可以消除噪声(峰值)。

Sidenote on iterating over ndarrays : you might not want to iterate over the np.nonzero() output, as the pixel positions it will give you might be unsorted, meaning you will end up accessing random places in the image instead of accessing contiguous (following each other) addresses, which is a common "memory access worst case" slowing execution down. 关于遍历ndarrays的注释:您可能不想遍历np.nonzero()输出,因为它给您的像素位置可能是未排序的,这意味着您最终将访问图像中的随机位置,而不是访问连续的位置(下面是彼此)地址,这是一种常见的“内存访问最坏情况”,它会降低执行速度。

For binary image manipulation like removing small isolated group of pixels in the mask, do take a look at Binary Morphology 对于二进制图像处理,例如删除蒙版中的孤立像素小块,请看一下二进制形态

You can start with tresholding each of the color channels(r,g,b) of the input image. 您可以首先对输入图像的每个颜色通道(r,g,b)进行限定。 The object in the lower image should be separable in the green channel and for the top pictures even just the brightness could do good initial job. 下部图像中的对象在绿色通道中应该是可分离的,对于顶部图像,即使亮度也可以很好地完成初始工作。

The second step would be labeling connected components in the mask, perhaps using the function in the link above. 第二步可能是在掩码中标记连接的组件 ,也许使用上面链接中的功能。

Finally, you can select the best component by size/shape. 最后,您可以按尺寸/形状选择最佳组件。

If all your objects are circles, you can start with detecting them by shape - that would mean doing edge detection and then Hough Transfrom 如果您的所有对象都是圆形,则可以从形状检测开始-这意味着先进行边缘检测,然后进行Hough Transfrom

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

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