简体   繁体   English

计算黑色像素比

[英]Calculate the black pixel ratio

From an image, i want to calculate as accurately as possible:从图像中,我想尽可能准确地计算:

  • The overall ratio of black compared to white黑与白的整体比例
  • Which horizontal lines have the most black pixels.哪些水平线的黑色像素最多。 i want the top 3 lines.我想要前 3 行。
  • Which vertical lines have the most black pixels.哪些垂直线的黑色像素最多。 i want the top 3 lines.我想要前 3 行。
  • For each of the top 3 horizontal lines, what is the ratio of black compared to white.对于前 3 条水平线中的每一条,黑色与白色的比例是多少。
  • For each of the top 3 vertical lines, what is the ratio of black compared to white.对于前 3 条垂直线中的每一条,黑色与白色的比例是多少。

Example of the images:图像示例: 在此处输入图像描述

In opencv you could use Count non Zero for counting the number of black pixels.在 opencv 中,您可以使用非零计数来计算黑色像素的数量。 Having the total number of pixels you can easily find the ratio.拥有像素总数,您可以轻松找到比率。

For the number of pixels in an area, I would recommend using the same function as above, but on a specific area ( ROI ).对于一个区域中的像素数,我建议使用与上述相同的 function,但在特定区域( ROI )上。

I would use threshold before to be accurate.为了准确起见,我之前会使用阈值。 Threshold 临界点

Use whatever line scan system you're going for to get the number of white pixels and the number of total pixels (and black, too, if you're scanning an image with many colors).使用您要使用的任何线扫描系统来获取白色像素的数量和总像素的数量(如果您正在扫描具有多种颜色的图像,也可以使用黑色)。 You can store each line as an object or a tuple or whatever, then add each to a list.您可以将每一行存储为 object 或元组或其他任何内容,然后将每行添加到列表中。 Sort the list by black pixels, and you can easily find your top three.按黑色像素对列表进行排序,您可以轻松找到前三名。

Repeat creating a row list.重复创建行列表。

Ratio is simple division performed on each of the top three lines.比率是对前三行中的每一行执行的简单除法。 You can probably even store it as a property.您甚至可以将其存储为属性。

@property
def ratio_of_black_to_white(self):
  return self.black_px / self.white_px

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

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