二値画像から白の面積取得

2019/07/04

Python2.7.6, OpenCV3.0.0

white_pixels = cv2.countNonZero(img_binarization)
white_area_ratio = (float(white_pixels)/(im_height*im_width))


画像を2値化して白面積比を算出するサンプル

import cv2

im_path = r'image_path'

img_src = cv2.imread(im_path)
img_gray = cv2.cvtColor(img_src, cv2.COLOR_RGB2GRAY)
threshold = 125
ret, img = cv2.threshold(img_gray, threshold, 255, cv2.THRESH_BINARY)

white_pixels = cv2.countNonZero(img)
im_height,im_width = img.shape[:2]
white_area_ratio = (float(white_pixels)/(im_height*im_width))
print(white_area_ratio)

#表示
cv2.imshow("thresh image", img)
cv2.waitKey()
cv2.destroyAllWindows()


Python,OpenCVで二値画像から白と黒の面積比を算出
https://techtech-sorae.com/pythonopencv%E3%81%A7%E4%BA%8C%E5%80%A4%E7%94%BB%E5%83%8F%E3%81%8B%E3%82%89%E7%99%BD%E3%81%A8%E9%BB%92%E3%81%AE%E9%9D%A2%E7%A9%8D%E6%AF%94%E3%82%92%E7%AE%97%E5%87%BA/