简体   繁体   中英

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.
  • Which vertical lines have the most black pixels. i want the top 3 lines.
  • For each of the top 3 horizontal lines, what is the ratio of black compared to white.
  • For each of the top 3 vertical lines, what is the ratio of black compared to white.

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

In opencv you could use Count non Zero for counting the number of black pixels. 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 ).

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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