From an image, i want to calculate as accurately as possible:
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.