It is currently Mon Sep 23, 2019 7:24 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Get the overall brightness level of a picture?
PostPosted: Mon May 06, 2019 7:49 pm 
Offline
User
User

Joined: Wed Apr 18, 2018 8:24 am
Posts: 98
Hi,

is it possible to load e.g. a .jpg file and analyze it for the level of brightness?
The picture should NOT be displayed!

Reason: I have thousands of pictures that were taken with very! low light and
are 95% just... black.

I'd like to identify those without human interaction :mrgreen:


Top
 Profile  
Reply with quote  
 Post subject: Re: Get the overall brightness level of a picture?
PostPosted: Mon May 06, 2019 8:58 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 520
Read every 2nd pixel and compare it its grayscale value to a defined thereshold.
Get the number (%) of pixels that passed.


Top
 Profile  
Reply with quote  
 Post subject: Re: Get the overall brightness level of a picture?
PostPosted: Mon May 06, 2019 9:13 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jun 25, 2014 5:25 pm
Posts: 359
Location: Germany
Code:
UseJPEGImageDecoder()
UseJPEG2000ImageDecoder()
UsePNGImageDecoder()
UseTIFFImageDecoder()
UseTGAImageDecoder()
UseGIFImageDecoder()

Procedure.d GetBrightnessOfColor(color)
  ; http://fseitz.de/blog/index.php?/archives/112-Helligkeit-von-Farben-des-RGB-Farbraums-berechnen.html
  ProcedureReturn Sqr(0.299 * Pow(Red(color), 2) + 0.587 * Pow(Green(color), 2) + 0.114 * Pow(Blue(color), 2))
EndProcedure

Define.d totalImageBrightness, averageBrightness
Define   x, y, countOfAllfPixels, pixelColor
Define   imageFilePath$

imageFilePath$ = OpenFileRequester("Open image file", "", "", 0)

image = LoadImage(#PB_Any, imageFilePath$)
If Not image
  Debug "Error"
  End
EndIf

countOfAllfPixels = ImageWidth(image) * ImageHeight(image)

If Not StartDrawing(ImageOutput(image))
  Debug "Error"
  End
EndIf

For x = ImageWidth(image) - 1 To 0 Step -1
  For y = ImageHeight(image) - 1 To 0 Step -1
    pixelColor = Point(x, y)
    totalImageBrightness + GetBrightnessOfColor(pixelColor)
  Next
Next

StopDrawing()

FreeImage(image)

averageBrightness = totalImageBrightness / countOfAllfPixels

Debug averageBrightness

_________________
Image
Why OpenSource should have a license
PureBasic-CodeArchiv-Rebirth: Git-Repository / Download -- Any help is welcome!
Manjaro Xfce x64 (Main system) :: WindowsXP/Xubuntu x86 (VirtualBox) :: PureBasic (Linux: x86/x64, Windows: x86) :: All are up to date


Top
 Profile  
Reply with quote  
 Post subject: Re: Get the overall brightness level of a picture?
PostPosted: Mon May 06, 2019 10:35 pm 
Offline
User
User

Joined: Wed Apr 18, 2018 8:24 am
Posts: 98
@Mijikai
Thanks for the hint!

@Sicro
Holy cow, that's amazing!
I've changed the step value to -4 to make it faster
and the matching quote for the dark pictures is ~99% :D


Top
 Profile  
Reply with quote  
 Post subject: Re: Get the overall brightness level of a picture?
PostPosted: Tue May 07, 2019 7:41 am 
Offline
Addict
Addict
User avatar

Joined: Thu Feb 09, 2006 11:27 pm
Posts: 2437
I've used a CustomFilter from time to time in similar cases to speed up things...
...but keep in mind to use a global variable for summarizing the pixel info within the callback routine.

Code:
UseJPEGImageDecoder()
UseJPEG2000ImageDecoder()
UsePNGImageDecoder()
UseTIFFImageDecoder()
UseTGAImageDecoder()

Define   x, y, countOfAllfPixels, pixelColor
Define   imageFilePath$

Global TotalImageBrightness.d

Procedure Brightness(x,y,c,d)

   x=c&$FF
   y=(c>>8)&$FF
   c=(c>>16)&$FF
   TotalImageBrightness+Sqr(x*x*0.299+y*y*0.587+c*c*0.114)

EndProcedure

imageFilePath$=   "IQ.png"
If FileSize(imageFilePath$)<0
   imageFilePath$=   OpenFileRequester("Open image file", "", "", 0)
EndIf

If FileSize(imageFilePath$)>0

   image = LoadImage(#PB_Any, imageFilePath$)
   If image>=0
      countOfAllfPixels = ImageWidth(image) * ImageHeight(image)
      TotalImageBrightness=0
      StartDrawing(ImageOutput(image))
      CustomFilterCallback(@Brightness())
      DrawingMode(#PB_2DDrawing_CustomFilter)
      DrawImage(ImageID(image),0,0)
      StopDrawing()
      FreeImage(image)

      Debug totalImageBrightness / countOfAllfPixels
   EndIf
EndIf


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye