Get the overall brightness level of a picture?

Windows specific forum
forumuser
User
User
Posts: 98
Joined: Wed Apr 18, 2018 8:24 am

Get the overall brightness level of a picture?

Post by forumuser »

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:
User avatar
Mijikai
Addict
Addict
Posts: 926
Joined: Sun Sep 11, 2016 2:17 pm

Re: Get the overall brightness level of a picture?

Post by Mijikai »

Read every 2nd pixel and compare it its grayscale value to a defined thereshold.
Get the number (%) of pixels that passed.
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 453
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Get the overall brightness level of a picture?

Post by Sicro »

Code: Select all

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 :: DarkColor-Scheme for PureBasic
PureBasic-CodeArchiv-Rebirth: Git-Repository / Download – Any help is welcome!
Manjaro Xfce x64 (Main system) :: Windows 7 x64 (VirtualBox) :: Newest PureBasic version
forumuser
User
User
Posts: 98
Joined: Wed Apr 18, 2018 8:24 am

Re: Get the overall brightness level of a picture?

Post by forumuser »

@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
User avatar
Michael Vogel
Addict
Addict
Posts: 2571
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Get the overall brightness level of a picture?

Post by Michael Vogel »

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: Select all

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
Post Reply