# PureBasic Forum

 It is currently Thu Sep 24, 2020 12:18 am

 All times are UTC + 1 hour

 Page 1 of 1 [ 5 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Get the overall brightness level of a picture?Posted: Mon May 06, 2019 7:49 pm
 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

Top

 Post subject: Re: Get the overall brightness level of a picture?Posted: Mon May 06, 2019 8:58 pm
 Enthusiast

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

Top

 Post subject: Re: Get the overall brightness level of a picture?Posted: Mon May 06, 2019 9:13 pm
 Enthusiast

Joined: Wed Jun 25, 2014 5:25 pm
Posts: 426
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

_________________

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

 Post subject: Re: Get the overall brightness level of a picture?Posted: Mon May 06, 2019 10:35 pm
 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%

Top

 Post subject: Re: Get the overall brightness level of a picture?Posted: Tue May 07, 2019 7:41 am

Joined: Thu Feb 09, 2006 11:27 pm
Posts: 2549
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

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 5 posts ]

 All times are UTC + 1 hour

#### Who is online

Users browsing this forum: Zach and 8 guests

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

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - IDE    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite