Sign in to follow this  
Followers 0
youknowwho4eva

getpixelcolor dark and light

8 posts in this topic

In my program I get the pixel colors, The problem is I need to differentiate between white and black. Here's the kicker, it's not light and day white and black. Plus with pixelation you get the blur. I'm wondering if there is a half way point in the decimal or in the hex that I can say if < this is white, if > then this is black. Any other suggestions would be appreciated.


Giggity

Share this post


Link to post
Share on other sites



When I used to color detect, I would break the pixel color into the "RGB". I would analyze the brightest color and darkest color and some other shade variations to figure out what my tolerances were. Then in code, when I PixelGetColor, I would break into RGB and check each value for the tolerances that I learned through analyzing reference pixels. Good luck... lots of trial and error.

Lar.


f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Share this post


Link to post
Share on other sites

I've tried looking up the decimal rgb code, I don't see how my results fit into it. For example, the first color it sees is 16777215. Which is almost white. How is that RGB? From what I've found so far that is a purpleish color. I haven't searched on here yet for ways to translate it. When I do the hexadecimal option, they all come out as the proper color to the charts I've found. I figured I'd probably have to may my own chart :mellow: . Oh well nothing to it but to do it. Thanks for the help.


Giggity

Share this post


Link to post
Share on other sites

See the UDF... Color.au3

in there are functions to break out the individual R G and B colors of a PixelGetColor number.

$Red = _ColorGetRed( $pixel ) ;Red

$Green = _ColorGetGreen ( $pixel ) ;Green

$Blue = _ColorGetBlue ( $pixel ) ;$Blue

Lar.


f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Share this post


Link to post
Share on other sites

Ah many thanks Larry, I will check this out.


Giggity

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Ok, now I'm real lost. This is what I have, when I run it across the barcode, the white returns 0 0 0. Which is black. and the other ones come out weird colors that are neither white nor black. Maybe you can see something I'm doing wrong. Right now it only works up to the array display. converting it from only working on my bar code maker to work on all bar codes.

#include <misc.au3>
#include <array.au3>
#include <GUIConstantsEx.au3>
#include <Math.au3>
#include <color.au3>
Dim $color[100000]
Dim $bar[1000]
dim $counter[1000]
dim $counter2[1000]
dim $num[1000]
$dll = DllOpen("user32.dll")
$start = 0
GUICreate("UPC Scanner",400,400)
GUICtrlCreateLabel("Once you click on start" & @LF & "Click once on the left of the bar code" & @LF & "And once on the right of the bar code.",20,20)
GUICtrlCreateLabel("UPC:",20,80)
$upc = GUICtrlCreateInput("000000000000000",20,100,100,20)
$go = GUICtrlCreateButton("Start",20,150,75,20)
GUICtrlSetState($upc,$gui_disable)
If $start = 1 Then $msg = $go

GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Select
    Case $msg = $gui_event_close
        Exit
    Case $msg = $go
        Sleep(100)
        Do
            Sleep(100)
            Until _IsPressed("01",$dll) = 1
    
        $mouse = MouseGetPos()
        $start = 1
        Sleep(200)
    
    Do
            Sleep(100)
            Until _IsPressed("01",$dll) = 1
    
        $mouse2 = MouseGetPos()
        $start = 0
        For $n = 1 to ($mouse2[0] - $mouse[0]) + 1
        $color = PixelGetColor($mouse[0] + $n,$mouse[1])
        $color = Hex($color)
        
        $blue = _ColorGetBlue($color)
        $green = _ColorGetGreen($color)
        $red = _colorgetred($color)
;~      $color = StringLeft($color,3)
;~      $color = StringRight($color,1)
;~      MsgBox(0,"",$red & "  " & $green & "  " & $blue)
;~      If StringIsInt($color) = 1 Then 
;~          $bar[$n] = 0
;~      Else
;~          $bar[$n] = 1
;~      EndIf
    $color = $red & "  " & $green & "  " & $blue
        $bar[$n] = $color
        
    Next
    _ArrayDisplay($bar)
    $bar[0] = $n
    For $i = 1 to $bar[0]
        if $bar[1] <> "0" Then
            _ArrayDelete($bar,1)
        Else
            ExitLoop
        EndIf
    Next
    _ArrayReverse($bar)
    For $i = 0 to 1000
        if $bar[0] <> "0" Then
            _ArrayDelete($bar,0)
        Else
            ExitLoop
        EndIf
    Next
    _ArrayReverse($bar)
        If $bar[1] = 0 And $bar[2] = 0 And $bar[3] = 0 And $bar[4] = 1 And $bar[5] = 1 And $bar[6] = 1 And $bar[7] = 0 And $bar[8] = 0 And $bar[9] = 0 Then
        Else
            MsgBox(0,"","Read Error")
        EndIf
For $r = 1 to 100
    If $r <> 1 Then
        If $bar[$r] <> $bar[$r-1]Then
            ExitLoop
        EndIf
    EndIf
Next
    $r -= 1
    $bar2 = _ArrayToString($bar)
;~  MsgBox(0,"",$bar2)
    $count = 6 * $r
    $bar2 = StringTrimLeft($bar2, $count + StringLen($bar[0]) + 1)
    $bar2 = StringTrimRight($bar2, $count - 1)
        $count1 = StringLen($bar2)
;~      MsgBox(0,"",$bar2 & @lf & $count1)
        $bar3 = StringTrimLeft($bar2,$count1 / 2)
        $bar4 = StringTrimRight($bar2,$count1 / 2)
        $bar3 = StringTrimLeft($bar3,5 * $r)
        $bar4 = StringTrimright($bar4,5 * $r)
        $bar5 = $bar4
        $bar6 = $bar3
;~      MsgBox(0,"",$bar5)
;~      MsgBox(0,"",$bar6)
        For $e = 1 to 100
            If _MathCheckDiv($e,2) = 1 Then
            $count = stringlen(StringLeft($bar5,(stringinstr($bar5,"0"))-1)) / 2
            $count = $count / $r
        Else
            $count = stringlen(StringLeft($bar5,(stringinstr($bar5,"1"))-1)) / 2
            $count = $count / $r
            
            EndIf

        If $count = 1 Then
            $counter[$e] = 1
        ElseIf $count = 2 Then
            $counter[$e] = 2
        ElseIf $count = 3 Then
            $counter[$e] = 3
        ElseIf $count = 4 Then
            $counter[$e] = 4
        Else
            $counter[$e] = StringLen($bar5) / $r / 2
            ExitLoop
            EndIf
            
            $bar5 = StringTrimLeft($bar5,$counter[$e] * $r * 2)
;~          MsgBox(0,$e,$counter[$e] & @lf & $bar5)
        Next
        For $e = 1 to 100
            If _MathCheckDiv($e,2) = 1 Then
            $count = stringlen(StringLeft($bar6,(stringinstr($bar6,"1"))-1)) / 2
            $count = $count / $r
        Else
            $count = stringlen(StringLeft($bar6,(stringinstr($bar6,"0"))-1)) / 2
            $count = $count / $r
            
            EndIf

        If $count = 1 Then
            $counter2[$e] = 1
        ElseIf $count = 2 Then
            $counter2[$e] = 2
        ElseIf $count = 3 Then
            $counter2[$e] = 3
        ElseIf $count = 4 Then
            $counter2[$e] = 4
        Else
            $counter2[$e] = StringLen($bar6) / $r / 2
            ExitLoop
            EndIf
            
            $bar6 = StringTrimLeft($bar6,$counter2[$e] * $r * 2)
;~          MsgBox(0,$e,$counter2[$e] & @lf & $bar6)
        Next
        $msgbx = _ArrayToString($counter)
        For $w = 1 to 100
            If StringLen($counter[$w]) <> 1 then ExitLoop
            Next
            $v = 0
            For $g = 1 to $w + 1 step +4
                $v += 1
                If $counter[$g] = 3 and $counter [$g +1] = 2 and $counter[$g +2] = 1 and $counter[$g + 3] = 1 Then $num[$v] = 0
                If $counter[$g] = 2 and $counter [$g +1] = 2 and $counter[$g +2] = 2 and $counter[$g + 3] = 1 Then $num[$v] = 1
                If $counter[$g] = 2 and $counter [$g +1] = 1 and $counter[$g +2] = 2 and $counter[$g + 3] = 2 Then $num[$v] = 2
                If $counter[$g] = 1 and $counter [$g +1] = 4 and $counter[$g +2] = 1 and $counter[$g + 3] = 1 Then $num[$v] = 3
                If $counter[$g] = 1 and $counter [$g +1] = 1 and $counter[$g +2] = 3 and $counter[$g + 3] = 2 Then $num[$v] = 4
                If $counter[$g] = 1 and $counter [$g +1] = 2 and $counter[$g +2] = 3 and $counter[$g + 3] = 1 Then $num[$v] = 5
                If $counter[$g] = 1 and $counter [$g +1] = 1 and $counter[$g +2] = 1 and $counter[$g + 3] = 4 Then $num[$v] = 6
                If $counter[$g] = 1 and $counter [$g +1] = 3 and $counter[$g +2] = 1 and $counter[$g + 3] = 2 Then $num[$v] = 7
                If $counter[$g] = 1 and $counter [$g +1] = 2 and $counter[$g +2] = 1 and $counter[$g + 3] = 3 Then $num[$v] = 8
                If $counter[$g] = 3 and $counter [$g +1] = 1 and $counter[$g +2] = 1 and $counter[$g + 3] = 2 Then $num[$v] = 9
                If $counter[$g] <> 1 and $counter [$g] <> 2 and $counter[$g] <> 3 and $counter[$g] <> 4 Then ExitLoop
                Next
                $v -= 1
                For $g = 1 to $w + 1 step +4
                $v += 1
                If $counter2[$g] = 3 and $counter2 [$g +1] = 2 and $counter2[$g +2] = 1 and $counter2[$g + 3] = 1 Then $num[$v] = 0
                If $counter2[$g] = 2 and $counter2 [$g +1] = 2 and $counter2[$g +2] = 2 and $counter2[$g + 3] = 1 Then $num[$v] = 1
                If $counter2[$g] = 2 and $counter2 [$g +1] = 1 and $counter2[$g +2] = 2 and $counter2[$g + 3] = 2 Then $num[$v] = 2
                If $counter2[$g] = 1 and $counter2 [$g +1] = 4 and $counter2[$g +2] = 1 and $counter2[$g + 3] = 1 Then $num[$v] = 3
                If $counter2[$g] = 1 and $counter2 [$g +1] = 1 and $counter2[$g +2] = 3 and $counter2[$g + 3] = 2 Then $num[$v] = 4
                If $counter2[$g] = 1 and $counter2 [$g +1] = 2 and $counter2[$g +2] = 3 and $counter2[$g + 3] = 1 Then $num[$v] = 5
                If $counter2[$g] = 1 and $counter2 [$g +1] = 1 and $counter2[$g +2] = 1 and $counter2[$g + 3] = 4 Then $num[$v] = 6
                If $counter2[$g] = 1 and $counter2 [$g +1] = 3 and $counter2[$g +2] = 1 and $counter2[$g + 3] = 2 Then $num[$v] = 7
                If $counter2[$g] = 1 and $counter2 [$g +1] = 2 and $counter2[$g +2] = 1 and $counter2[$g + 3] = 3 Then $num[$v] = 8
                If $counter2[$g] = 3 and $counter2 [$g +1] = 1 and $counter2[$g +2] = 1 and $counter2[$g + 3] = 2 Then $num[$v] = 9
                If $counter2[$g] <> 1 and $counter2 [$g] <> 2 and $counter2[$g] <> 3 and $counter2[$g] <> 4 Then ExitLoop
                Next
                $v -= 1
                For $u = 1 to $v
                    if $u = 1 then
                        GUICtrlSetData($upc,$num[$u])
                    Else
                        GUICtrlSetData($upc,guictrlread($upc) & $num[$u])
                    EndIf
                Next        
Dim $color[100000]
Dim $bar[1000]
dim $counter[1000]
dim $counter2[1000]
dim $num[1000]
$start = 0

        EndSelect
WEnd

Edit, I just ran it on my bar code maker program and it returned 0 0 0 for everything.

Edited by youknowwho4eva

Giggity

Share this post


Link to post
Share on other sites

I can't wade through all the code right now... but I don't think this is necessary...

$color = Hex($color)

Lar.


f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Share this post


Link to post
Share on other sites

#include <misc.au3>
#include <array.au3>
#include <GUIConstantsEx.au3>
#include <Math.au3>
#include <color.au3>
Dim $color[100000]
Dim $bar[1000]
dim $counter[1000]
dim $counter2[1000]
dim $num[1000]
$dll = DllOpen("user32.dll")
$start = 0
GUICreate("UPC Scanner",400,400)
GUICtrlCreateLabel("Once you click on start" & @LF & "Click once on the left of the bar code" & @LF & "And once on the right of the bar code.",20,20)
GUICtrlCreateLabel("UPC:",20,80)
$upc = GUICtrlCreateInput("000000000000000",20,100,100,20)
$go = GUICtrlCreateButton("Start",20,150,75,20)
GUICtrlSetState($upc,$gui_disable)
If $start = 1 Then $msg = $go

GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Select
    Case $msg = $gui_event_close
        Exit
    Case $msg = $go
        Sleep(100)
        Do
            Sleep(100)
            Until _IsPressed("01",$dll) = 1
    
        $mouse = MouseGetPos()
        $start = 1
        Sleep(200)
    
    Do
            Sleep(100)
            Until _IsPressed("01",$dll) = 1
    
        $mouse2 = MouseGetPos()
        $start = 0
        For $n = 1 to ($mouse2[0] - $mouse[0]) + 1
        $color = PixelGetColor($mouse[0] + $n,$mouse[1])
        $color = Hex($color)
        
        $blue = _ColorGetBlue($color)
        $green = _ColorGetGreen($color)
        $red = _colorgetred($color)
;~      $color = StringLeft($color,3)
;~      $color = StringRight($color,1)
;~      MsgBox(0,"",$red & "  " & $green & "  " & $blue)
;~      If StringIsInt($color) = 1 Then 
;~          $bar[$n] = 0
;~      Else
;~          $bar[$n] = 1
;~      EndIf
    $color = $red & "  " & $green & "  " & $blue
        $bar[$n] = $color
        
    Next
    _ArrayDisplay($bar)

Thats all that it works up too. According to the help file it only works with hexadecimal code.

*scratches head* I commented that line out, and it works. Now my brain really hurts lol. Thank you for your help I should be able to do something from here.


Giggity

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
Sign in to follow this  
Followers 0