top of page

[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.

  1. Convert image to grayscale and make it binary

  2. Add up all the components in the image

  3. 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 All

[Python] Output pandas.DataFrame as json

Summary Data analysis is performed using python. The analysis itself is performed using pandas, and the final results are stored in pandas.DataFrame format. I want to output this result to a file in j

[Python] Conditionally fitting

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 cases where you want to condition the fitting parameters. For

Comments


Let's do our best with our partner:​ ChatReminder

iphone6.5p2.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Let's do our best with our partner:​ ChatReminder

納品:iPhone6.5①.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Theme diary: Decide the theme and record for each genre

It is a diary application that allows you to post and record with themes and sub-themes for each genre.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png
bottom of page