Jump to content

Getting color information from a picture


Recommended Posts

Hello,

I developed a simple and basic software that checks using a webcam if a LED is turning ON or OFF on some parts.

My actual algorithm make a picture 200x200 px and then resize the picture to 1x1px. I am reading the decimal value(color) of that 1x1 px and if is more then 212121 the LED is ON, if is less then is OFF.

In practice this is working ok but is not so sensitive when the LED brightness is not standard. 

Now I want to do a improvement and I want to check how much white or yellow a picture contains. Do you know how I can do this?

For example having a picture 200x200px I want to know how much with that picture contains or how many pixels are white from all 40000.

Link to comment
Share on other sites

  • Moderators

@rony2006 did you search as instructed? Even a simple search of the forum turns up 100 threads on findbmp, many with the examples you're asking for.

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

and you are sure you're searching all content, not this thread right? :)

https://www.autoitscript.com/forum/search/

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

I am sure what follows has be written some where on these forums. They must have been.  The AutoIt forums is where I mostly learnt this stuff.

When using export functions from Windows GdiPlus.dll file which are bundle into AutoIt's "GDIPlus.au3" include file, 32 bit colours can be used, consisting of four eight bit channels.   An easy pixel colour format of hexidecimal 0xAARRGGBB can be used.  Where the:-
"AA" is the transparency Alpha channel of the pixel.  0x00 if fully transparent, 0xFF (255 decimal,  8 bit, (2^8) - 1) is fully opaque. Any number between 0 and 0xFF would make the pixel partically transparent.
"RR" is the Red channel, 0xFFFF0000 is fully opaque red.
"GG" is the Green channel, 0xFF00FF00 is fully opaque green.
"BB"  is the Blue channel, 0xFF0000FF is fully opaque blue.
0xFFFFFF00 is fully opaque yellow.

You will find other functions use colours are in RGB 24 bit colour format, and the hexidecimal colour format 0xRRGGBB can be used. I have also seen BGR and BGRA colour formats.
See SciTE's output pane for number of pixels in each category.

#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>
#include <Array.au3>
#include <GDIPlus.au3>

_ExaminePixels()


Func _ExaminePixels()
    _GDIPlus_Startup() ;initialize GDI+
    Local $iWidth, $iHeight, $aCol, $iTransparent = 0, $iYellow = 0, $iWhite = 0, $iOther = 0
    Local $iColor
    Local $hBitmap = _GDIPlus_BitmapCreateFromFile("TranBkGndY-R.bmp") ; <--------- Enter image file name here.
    $iWidth = _GDIPlus_ImageGetWidth($hBitmap)
    $iHeight = _GDIPlus_ImageGetHeight($hBitmap)

    ConsoleWrite($iWidth & "x" & $iHeight & @CRLF) ; Debug purposes only
    Local $aArr[$iWidth * $iHeight][2] ; Array to hold pixel position, $aArr[n][0], and, pixel colour, $aArr[n][1].

    For $iY = 0 To $iHeight - 1
        For $iX = 0 To $iWidth - 1
            $iColor = Hex(_GDIPlus_BitmapGetPixel($hBitmap, $iX, $iY), 8) ; Get current pixel color in hex format 0xAARRGGBB
            ;ConsoleWrite($iColor & @CRLF) ; Debug purposes only
            $aCol = StringRegExp($iColor, "(..)", 3) ; Split $iColor into array with 2 characters only counting from left to right of $iColor in each array element.
            $iAlpha = $aCol[0]
            $Red = "0x" & $aCol[1]
            $Green = "0x" & $aCol[2]
            $Blue = "0x" & $aCol[3]
            ;If $iAlpha = "FF" Then _ArrayDisplay($aCol) ; Debug purposes only. Shows StringRegExp result.
            If $iAlpha <> "FF" Then ; Partical or completely transparent
                $iTransparent += 1
                ;_GDIPlus_BitmapSetPixel($hBitmap, $iX, $iY,"0xFF0000FF") ; Change transparent pixels to fully opaque blue.
            ElseIf $iAlpha = "FF" And $Red > $Blue And $Green > $Blue And $Red >= 0xB0 And $Green >= 0xB0 Then ; Yellow
                $iYellow += 1
            ElseIf $iAlpha = "FF" And $Red > 0xE0 And $Green > 0xE0 And $Blue > 0xE0 Then ; White
                $iWhite += 1
            Else
                $iOther += 1
            EndIf
            $aArr[$iX + ($iY * $iWidth)][0] = $iX & "x" & $iY
            $aArr[$iX + ($iY * $iWidth)][1] = $iColor
        Next
    Next
    ConsoleWrite("Transparent:  " & $iTransparent & @CRLF)
    ConsoleWrite("Yellow:  " & $iYellow & @CRLF)
    ConsoleWrite("White:  " & $iWhite & @CRLF)
    ConsoleWrite("Other:  " & $iOther & @CRLF)

    ;_GDIPlus_ImageSaveToFile($hBitmap, "ChangedImage.BMP") ; If _GDIPlus_BitmapSetPixel() is used, this will save the changed $hBitmap to file

    ; Display BMP Image
    Local $hGUI = GUICreate("GDI+ Example (" & @ScriptName & ")", $iWidth, $iHeight) ;create a test GUI
    GUISetState(@SW_SHOW)

    Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) ;create a graphics object from a window handle
    _GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap, 0, 0) ;copy negative bitmap to graphics object (GUI)

    $aArr[0][0] = "   " & $aArr[0][0] & "   " ; Widen this column (For _ArrayDisplay purposes only).
    $aArr[0][1] = "       " & $aArr[0][1] & "       " ; Widen this column (For _ArrayDisplay purposes only).
    _ArrayDisplay($aArr, "Fully Opaque Pixels", "", 4, "|", "Pos XxY|AARRGGBB Color", 200)
    ;Do
    ;Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ;cleanup GDI+ resources
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_Shutdown()
    GUIDelete($hGUI)
EndFunc   ;==>_ExaminePixels


And this is a tool I used to tweak the values of the colour channels to return shades of a colour, like yellow ( trial and error method).

Opt("MouseCoordMode", 1) ;1=absolute, 0=relative, 2=client

HotKeySet("{ESC}", "Terminate")

Local $exit = 1

While $exit
    Sleep(10)
    TtColXY()
WEnd

; TtColXY() Tooltip shows colour, and cursor position relative to screen and window.
Func TtColXY()
    Local $sCol
    $aPos = MouseGetPos()
    $sColor = "0x" & Hex(PixelGetColor($aPos[0], $aPos[1]), 6)
    $iPosX = ($aPos[0] - 120) * ($aPos[0] > @DesktopWidth * 0.90) + ($aPos[0] <= @DesktopWidth * 0.90) * ($aPos[0] + 3)
    $iPosY = ($aPos[1] - 50) * ($aPos[1] > @DesktopHeight * 0.96) + ($aPos[1] <= @DesktopHeight * 0.96) * ($aPos[1] + 3)
    $Red = Int("0x" & StringRegExpReplace($sColor, "(..)(..)(..)(..)", "\2"))
    $Green = Int("0x" & StringRegExpReplace($sColor, "(..)(..)(..)(..)", "\3"))
    $Blue = Int("0x" & StringRegExpReplace($sColor, "(..)(..)(..)(..)", "\4"))

    If $Green > $Blue And $Red > $Blue And $Green >= 0xB0 And $Red >= 0xB0 Then
        $sCol = "Yellow"
    ElseIf $Blue > 0xE0 And $Green > 0xE0 And $Red > 0xE0 Then
        $sCol = "White"
    ElseIf $Blue > 0x50 And $Blue = $Green And $Blue = $Red Then
        $sCol = "Grey"
    ElseIf $Red > $Green And $Red > $Blue And $Red > 0x70 Then
        $sCol = "Red"
    ElseIf $Green > $Red And $Green >= $Blue And $Green > 0x70 Then
        $sCol = "Green"
    ElseIf $Blue > $Red And $Blue > $Green And $Blue > 0x70 Then
        $sCol = "Blue"
    Else
        $sCol = "Nil"
    EndIf

    ToolTip("Color is: " & $sColor & @CRLF & _
            " R:" & $Red & _
            " G:" & $Green & _
            " B:" & $Blue & @CRLF & _
            "Color - " & $sCol, _
            $iPosX, $iPosY)
    Return
EndFunc   ;==>TtColXY

Func Terminate()
    $exit = 0
EndFunc   ;==>Terminate


Also, this tool works best with a magnifier.  I use a compile version of Siao's script found here.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...