I have an input image as given below. enter image description here
I need to get total percentage and count of green and white pixels in each row of image with specific rowwise as output. I have tried code for getting percentage and count for single row. And it is given below:
import cv2
import numpy as np
from PIL import Image
img_s = Image.open('img_15.png')
width, height = img_s.size
total_pixels= width * height
# load image
img = cv2.imread('img_15.png')
# convert to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv)
# create mask for green color in hsv
# green is 60 in opencv
lower = (40,100,100)
upper = (100,255,255)
mask = cv2.inRange(hsv, lower, upper)
# count non-zero pixels in mask
count=np.count_nonzero(mask)
count_z = np.sum(mask == 0)
print("Total pixels:", total_pixels)
print('green count:', count, 'green %:', '{0:.2f}%'.format((count/total_pixels * 100)))
print('white count:', count_z, 'white %', '{0:.2f}%'.format((count_z/total_pixels * 100)))
# save output
cv2.imwrite('img_15_mask.png', mask)
# Display various images to see the steps
cv2.imshow('mask',mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
I need to extend this for each rows instead for a single row.