counting total pixel numbers of white color in an image

natan :

I'd like to count the number of white pixels in this image. I defined following the HSV value for white color.

# white color
low_white = np.array([0, 0, 0])
high_white = np.array([0, 0, 255])
white_mask = cv2.inRange(hsv_frame, low_white, high_white)
white= cv2.bitwise_and(frame, frame, mask=white_mask)

However due to weather condition and shadow in the image, it does not count the whole white pixels as white. It only considers top part of the cab as white. How can I include maximum amount color range in white color HSV definition?

Rotem :

You may adjust the sensitivity like described in this post

For the solution to work better, you can also increase the brightness first:

import numpy as np

frame = cv2.imread('truck.png')

# Incere frame brighness
bright_frame = cv2.add(frame, 50)

# Conver image after appying CLAHE to HSV
hsv_frame = cv2.cvtColor(bright_frame, cv2.COLOR_BGR2HSV)

sensitivity = 70  # Higher value allows wider color range to be considered white color
low_white = np.array([0, 0, 255-sensitivity])
high_white = np.array([255, sensitivity, 255])

white_mask = cv2.inRange(hsv_frame, low_white, high_white)
white = cv2.bitwise_and(frame, frame, mask=white_mask)

cv2.imwrite('white.png', white)  #Save out to file (for testing).


# Show result (for testing).
cv2.imshow('white', white)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result:

enter image description here

Well...
Lots gray pixels on the background are found as white.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=319309&siteId=1