Jump to content
therks

Suggestions on how to measure general brightness of an image

Recommended Posts

So I don't have any code cooked up yet as this is still in the theoretical stage. Just looking for some advice.

My current idea is to resize the image to 1x1 pixel, probably using _GDIPlus_ImageResize, and read the color of the resulting pixel to obtain an admittedly very general sense of brightness. Does anyone think this could work?

We have a CCTV system running at home using some home made cameras (raspberry pi) and we're trying to automate toggling settings for day/night time. We started with an AutoIt script that checked sunrise and sundown times for our location (calculations pulled from here) and toggled the settings based on that. Unfortunately our weather varies wildly, so it can get light/dark far outside normal sunrise/sundown times. Now we're hoping to periodically grab a still from the camera and toggle the light mode based on that.

Share this post


Link to post
Share on other sites

I would go with something like this :

#include <Constants.au3>
#include <GDIPlus.au3>

_GDIPlus_Startup ()
$hBitmap = _GDIPlus_BitmapCreateFromFile ("Image.jpg")
$hBitMapResized = _GDIPlus_ImageResize ($hBitmap, 20, 20)
$tBitmapData = _GDIPlus_BitmapLockBits ($hBitMapResized, 0, 0, 20, 20, $GDIP_ILMREAD, $GDIP_PXF32ARGB)
$iScan0 = DllStructGetData($tBitmapData, "Scan0")
$tPixel = DllStructCreate("int[400];", $iScan0)
Local $iMean = 0, $iStdDev = 0, $r, $g, $b
For $i = 1 to 400
  Split (DllStructGetData ($tPixel, 1, $i), $r, $g, $b)
  $iMean += $r+$g+$b
Next
$iMean /= 1200
For $i = 1 to 400
  Split (DllStructGetData ($tPixel, 1, $i), $r, $g, $b)
  $iStdDev += ($r-$iMean)^2
  $iStdDev += ($g-$iMean)^2
  $iStdDev += ($b-$iMean)^2
Next
$iStdDev = Sqrt($iStdDev/1200)
MsgBox ($MB_SYSTEMMODAL,"",$iMean & "/" & $iStdDev)
_GDIPlus_BitmapUnlockBits($hBitMapResized, $tBitmapData)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_BitmapDispose($hBitMapResized)
_GDIPlus_Shutdown ()

Func Split ($c, ByRef $r, ByRef $g, ByRef $b)
  $r = bitshift(bitand ($c,0xff0000),16)
  $g = bitshift(bitand ($c,0xff00),8)
  $b = bitand ($c,0xff)
EndFunc

Where the mean should be low for darker image and Standard Deviation also low for darker. I'm applying it for each component RGB, because global color doesn't say much.  Also going with a single (or a few) value, you will loose the StdDev significance.

I don't think anyone will argue that the lower is the mean (closer to 0), it implies that the image is quite dark. 

But even with a somewhat low mean, if you got a high StdDev, it implies that there is high contrast.  I would assume there is some brightness although part of the image is black.  So StdDev must be low as well.  Of course this is untested, and you will have to adjust the threshold of those indicator to fit your requirements.

Hope that make sense.

 

Share this post


Link to post
Share on other sites

@Nine: you're skipping half the outer rim of the image (range 0-20 = 21 pixels per side = 441 squared). Also, you lose one degree of freedom in calculating the mean, so you should divide by (3x441) - 1 = 1,323 -1 = 1322.

Edited by RTFC

Share this post


Link to post
Share on other sites

@RTFC.  It is not a range, it is a width and a height.  Major difference, so my code is good.

If you are still unsure use this code :

$tBitmapData = _GDIPlus_BitmapLockBits ($hBitMapResized, 0, 0, 20, 20, $GDIP_ILMREAD, $GDIP_PXF32ARGB)
;Global Const $tagGDIPBITMAPDATA = "uint Width;uint Height;int Stride;int Format;ptr Scan0;uint_ptr Reserved"

$iScan0 = DllStructGetData($tBitmapData, "Scan0")
$iW = DllStructGetData($tBitmapData, "Width")
$iH = DllStructGetData($tBitmapData, "Height")
$iS = DllStructGetData($tBitmapData, "Stride")
ConsoleWrite ($iW & "/" & $iH & "/" & $iS & @CRLF)

 

Share this post


Link to post
Share on other sites

Well I appreciate the code there @Nine, but I was offline for a few days and cooked this up in the meantime.

Func _GetImageBrightness($sFile) ; Return: 0 black -> 100 white
    Local $hBitmap, $hBitMapResized, $iColor, $R, $G, $B
    $hBitmap = _GDIPlus_BitmapCreateFromFile($sFile)
    $hBitMapResized = _GDIPlus_ImageResize($hBitmap, 1, 1)
    $iColor = BitAND(_GDIPlus_BitmapGetPixel($hBitMapResized, 0, 0), 0xffffff)
    $R = BitShift(BitAND($iColor, 0xff0000), 16)
    $G = BitShift(BitAND($iColor, 0xff00), 8)
    $B = BitAND($iColor, 0xff)

    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_BitmapDispose($hBitMapResized)

    Return SetExtended($iColor, Sqrt( .241 * $R ^ 2 + .691 * $G ^ 2 + .068 * $B ^ 2 ) / 2.55)
EndFunc

It's not as thorough as yours, but in a few simple tests it seems to be working out alright. I'm combining this with a set of thresholds to determine when to activate either day or night mode on our cameras. The calculation for brightness came from here.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Gowrisankar
      Dear members of the forum,
      I'm working on a project in which I have to use Image recognition technique. 
      Due to client restrictions, I couldn't use AutoIt for this project. 
      Is there a way to use this DLL "ImageSearchDLL.dll" (which is used to do image recognition steps in AutoIt) in VB.Net to achieve the same result? 
      I have used this DLL few years before and got good results. If there is a latest version of this DLL and if you can share it, that will be helpful too.
      Any guidance is deeply appreciated.
    • By amosrinat
      Hi,
      i have an AutoIT script that sends emails with two embedded images.
      When users get the email they don't see the images. How should I fix my code?
      Thank you for your support,
      Amos
      Func fSendMail() $TemplateName = StringRegExpReplace($Template, "^.*\\|\..*$", "") $SmtpServer = "smtp.**.com" $FromName = "do-not-reply@*********" $FromAddress = "do-not-reply@******" $ToAddress = $MG1_Mgr & ";"& $Owner $Subject = "ACTION REQUIRED - " & $TemplateName $Body = $sBody5 $IPPort = 25 $ssl = 0 Global $oMyRet[2] Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") $rc = fINetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $IPPort, $ssl) If @error Then MyErrFunc() EndIf EndFunc;fSendMail Func fINetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $IPPort = 25, $ssl = 0) Local $objEmail = ObjCreate("CDO.Message") $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>' $objEmail.To = $s_ToAddress Local $i_Error = 0 Local $i_Error_desciption = "" $objEmail.Subject = $s_Subject If StringInStr($as_Body, "<") And StringInStr($as_Body, ">") Then $objEmail.HTMLBody = $as_Body Else $objEmail.Textbody = $as_Body & @CRLF EndIf $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer If Number($IPPort) = 0 Then $IPPort = 25 $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort $objEmail.Configuration.Fields.Update $objEmail.Fields.Update ;$objEmail.Send **************** If @error Then MyErrFunc() EndIf $objEmail = "" EndFunc;fINetSmtpMailCom  
    • By coronatuss
      Hello everyone,
      Im developing an script to check the size of all .jpg stored in windows folder.
      The problem is that it doesn´t work properly with rotated images. 
      With _GDIPlus_ImageGetWidth() and _GDIPlus_ImageGetHeight()  I get the Width and Height of images as if they had not been rotated, and I need to know how they are rotated (orientation and grades).
      Any help is welcome! If more info is needed, please tell. 😁
    • By nacerbaaziz
      hello autoit team
      please i've a question for you.
      am creating a audio player
      and in this audio player i want to show the current trac info
      such as the total time and the position ... etc
      i know i can show it as label
      but the screen reader for the blind read the text every change
      because it have a screen scan
      what i want is to show this informations but such image or icon
      i mean i need to create
      GUICtrlCreatepic or GUICtrlCreateicon ....
      or some thing as that
      and show this informations as image on it
      i think that i can do that with the 
      _GDIPlus functions
      but i couldn't find the currect way to do it
      i tried the _GDIPlus_GraphicsDrawString
      but i couldn't know how it work
      what i need is a small example that create a GUI
      and add a multy line text to it as graphic or image.
      so i need a simple way because it will changed every sec
      i hope any one can help me to do that
      global $GUI = GUICreate("text", 400, 400) global $label = GUICtrlCreateLabel(GetText(), 10, 10, 380, 380) GUISetState() do sleep(100) until GUIGetMSG() = -3 exit func GetText() return StringFormat("file name is test.mp3 \r\n total time is 00:30:00 \r\n position is 00:05:50") endFunc  
×
×
  • Create New...