[python]Determine the brightness of the image
Overview
I was doing image recognition and wanted to judge the brightness of the image and classify it.
Specifically, I'm shooting indoors, but when the light is off (= no people in the room), it's useless to judge considering the nature of the application, so I wanted to avoid entering the judgment process.
So, I thought of a method to judge the brightness of the captured image.
Method
The procedure is as follows.
Convert image to grayscale and make it binary
Add up all the components in the image
Normalize by image size
When the image is grayscale, the values are higher in bright areas and lower in dark areas. By using this, the standardized value by adding all the components in the image can be used for judgment as the "brightness" of the image.
The code is below.
def judgeLight(file):
img=cv2.imread(file, cv2.IMREAD_GRAYSCALE) #1 load as grayscale
img=img.astype('float')
img/=255
sumGray=img.sum() #2 add up all the component
meanGray=sumGray/(img.shape[0]*img.shape[1]) #3 Normalize
return meanGray
I will try it with some images.
imgNames=glob.glob('*.png')
for imgName in imgNames:
m=judgeLight(imgName)
print(meanGray)
plt.figure()
plt.gray()
plt.imshow(img)
plt.show()
You can see that the darker image has a smaller value.
After that, set the threshold appropriately and remove the dark image.
Lastly
I thought about it myself because it didn't come out even if I googled variously, but maybe there is a more proper method.
Recent Posts
See AllSummary Data analysis is performed using python. The analysis itself is performed using pandas, and the final results are stored in...
Phenomenon I get a title error when trying to import firestore with raspberry pi. from from firebase_admin import firestore ImportError:...
Overview If you want to do fitting, you can do it with scipy.optimize.leastsq etc. in python. However, when doing fitting, there are many...
Comments