简体   繁体   English

如何在python中计算图像上的红色和黄色多边形?

[英]How to count red and yellow polygons on an image in python?

I have images where some elements are boxed with either red or yellow.我有一些图像,其中一些元素用红色或黄色装箱。 The box is a polygon in general.盒子通常是一个多边形。

The objective is to count the number of elements which are red and yellow.目标是计算红色和黄色元素的数量。

How can I do this count ?我怎么做这个计数?

I have tried to apply a mask that gets the red color or the yellow color but it gave relatively poor results.我曾尝试使用获得红色或黄色的面膜,但效果相对较差。 But I have no idea how to do the count later.但我不知道以后如何计数。

mask_example掩码示例

You will probably be looking at steps like the following:您可能会看到如下步骤:

  • Step 1 - discriminate red from yellow第 1 步 - 区分红色和黄色
  • Step 2 - mask out red and do remaining steps, then mask out yellow and do remaining steps第 2 步 - 遮盖红色并执行剩余步骤,然后遮盖黄色并执行剩余步骤
  • Step 3 - fill shape outlines to make complete and continuous第 3 步 - 填充形状轮廓以使其完整且连续
  • Step 4 - find contours of shapes and count them第 4 步 - 找到形状的轮廓并计算它们

There are many ways of doing each step, and I have not tried them all.每一步都有很多方法,我没有全部尝试过。 As I have numbered them, other folk may wish to make suggestions for improving any step.由于我已经对它们进行了编号,其他人可能希望提出改进任何步骤的建议。


So, for Step 1, you could discriminate red from yellow by calculating each pixel in a new image as:因此,对于步骤 1,您可以通过计算新图像中的每个像素来区分红色和黄色:

new = (Red - Green)/(Red + Green + 1)

The +1 is just to stop division by zero. +1只是停止除以零。 I actually did that with ImageMagick like this, but you could do it with PIL/Pillow + Numpy, or OpenCV just the same:我实际上是用ImageMagick这样做的,但你可以用 PIL/Pillow + Numpy 或 OpenCV 来做到这一点:

convert boxes.png -fx "(u.r-u.g)/(u.r+u.g+1)" -auto-level result1.png

在此处输入图片说明

Hopefully you can see that makes the yellow pixels darker and the red ones lighter.希望你能看到这使黄色像素更暗,红色像素更亮。

That will then give you a threshold that you can use to turn on and off each colour separately.这将为您提供一个阈值,您可以使用该阈值分别打开和关闭每种颜色。 The a channel in Lab colourspace is also a reasonable discriminant, by the way.顺便说一下, Lab色彩空间中的a通道也是一个合理的判别式。


Step 2 is just a thresholding and masking operation since we can see the separating threshold from Step 1.步骤 2 只是一个阈值和屏蔽操作,因为我们可以从步骤 1 中看到分离阈值。


Step 3 means filling out the boxes.第 3 步意味着填写方框。 You could do that with "morphology" , so I could close the gaps in the white horizontal lines with a horizontal 15x1 pixel structuring element like this:你可以用"morphology"来做到这一点,所以我可以用一个水平的 15x1 像素结构元素来关闭白色水平线中的间隙,如下所示:

convert result1.png -threshold 50%  -morphology close rectangle:15x1  result2.png

在此处输入图片说明

And then with a vertical 1x15 structuring element as well like this:然后使用垂直的 1x15 结构元素,如下所示:

convert result1.png -threshold 50%  -morphology close rectangle:15x1 -morphology close rectangle:1x15 result2.png

to get this, where you can see the gaps are all closed and the outline is complete:为了得到这个,你可以看到间隙全部闭合并且轮廓完整:

在此处输入图片说明

Both OpenCV and scikit-image have morphology functions you could use for this. OpenCVscikit-image都有可以用于此的形态学函数。


Step 4 - you can now use OpenCV's findContours() to count the blobs and get their characteristics.第 4 步 - 您现在可以使用 OpenCV 的findContours()来计算 blob 并获取它们的特征。

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

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