# finding the brightest area

I have a photo that has areas with high brightness. applying different algorithms i made white the brightest points and black the rest points

then i need to determine the center of the bright white surface i do in next way

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # convert

h, s, v = cv2.split(hsv) # split to h s v

limit = v.max () # get max bright in V

hsv_min = np.array((0, 0, limit), np.uint8) # put min and max

hsv_max = np.array((255, 255, limit), np.uint8)

img = cv2.inRange(hsv, hsv_min, hsv_max) # brightness filter

moments = cv2.moments(img, 1) # get moments

x_moment = moments['m01']

y_moment = moments['m10']

area = moments['m00']

x = int(x_moment / area) # x

y = int(y_moment / area) # y

cv2.putText(img, "center_brightness_surface!", (x,y), cv2.FONT_HERSHEY_SIMPLEX, 1, (100,100,100), 2)

cv2.imshow('frame_out', img)

cv2.imwrite("frame_out.jpg" , img)

cv2.waitKey (0)

cv2.destroyAllWindows ()

it is working but x and y not on surface. x y which i got far away from real center of brightness surface

please tell me how can i get center of brightness surface

edit retag close merge delete

Sort by » oldest newest most voted Here is code:

#!/usr/bin/ env python3
#openCV 4.0

import cv2 as cv
import numpy as np

hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV) # convert
h, s, v = cv.split(hsv) # split to h s v
limit = v.max () # get max bright in V

hsv_min = np.array((0, 0, limit), np.uint8) # put min and max
hsv_max = np.array((225, 225, limit), np.uint8)

img1 = cv.inRange(hsv, hsv_min, hsv_max) # brightness filter

moments = cv.moments(img1, 1) # get moments

x_moment = moments['m01']
y_moment = moments['m00']

area = moments['m00']

x = int(x_moment / area) # x
y = int(y_moment / area) # y

cv.putText(img1, "center_brightness_surface!", (x,y+220), cv.FONT_HERSHEY_SIMPLEX,1, (255,255,0),2)
cv.imwrite("frame_out.jpg" , img1)
cv.imshow('Brightest', img)
cv.imshow('frame_out', img1)

cv.waitKey (0)
cv.destroyAllWindows ()


Here is output: more

supra: put a ! in front of the [](link_to_image) to make it visible immediately ;)

Official site

GitHub

Wiki

Documentation