dcsimg
Results 1 to 21 of 21

Thread: how all find color code and replace color of picture

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    157

    how all find color code and replace color of picture

    hello
    draw picturbox and load picture
    i all find color red-170 , green-173 and blue-164
    and replace all find color red-0 , green-0 and blue-0
    picture attach
    Attached Images Attached Images  

  2. #2
    Hyperactive Member
    Join Date
    Apr 2017
    Posts
    415

    Re: how all find color code and replace color of picture

    I'm not 100% sure I understand what you are saying and your inserted image doesn't give me a clue but I think you want to scan a picture and replace all red, green, and blue colors with black. If this is what you want then the following code will do that

    Make sure Picture1.ForeColor is black

    Code:
    Private Sub Command3_Click()
     For x = 0 To Picture1.ScaleWidth - 1
       For y = 0 To Picture1.ScaleHeight - 1
         If Picture1.Point(x, y) = vbRed Or _
            Picture1.Point(x, y) = vbGreen Or _
            Picture1.Point(x, y) = vbBlue Then
    
               Picture1.PSet (x, y)
    
         End If
       Next y
     Next x
    End Sub

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,998

    Re: how all find color code and replace color of picture

    What about something like this, using GDI and OLE calls?

    Name:  sshot1.png
Views: 133
Size:  8.7 KB

    Name:  sshot2.png
Views: 133
Size:  8.9 KB
    Attached Files Attached Files

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,998

    Re: how all find color code and replace color of picture

    Perhaps I misinterpreted what was being asked for.

  5. #5
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,321

    Re: how all find color code and replace color of picture

    Quote Originally Posted by dilettante View Post
    Perhaps I misinterpreted what was being asked for.
    The attached image in OP is mind-numbing. . .

    cheers,
    </wqw>

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,079

    Re: how all find color code and replace color of picture

    Quote Originally Posted by dilettante View Post
    Perhaps I misinterpreted what was being asked for.
    I didn't look at your code, but my interpretation was he wants to look for a specific color (i.e. c = RGB(170, 173, 164) ), which in this case would be fairly gray, and replace it with black in an image.
    Perhaps your code is along that line?

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,998

    Re: how all find color code and replace color of picture

    Yes, as written it just checks each pixel for one specific color and replaces it with another color.

  8. #8
    Hyperactive Member
    Join Date
    Aug 2016
    Posts
    332

    Re: how all find color code and replace color of picture

    Quote Originally Posted by dilettante View Post
    What about something like this, using GDI and OLE calls?

    Name:  sshot1.png
Views: 133
Size:  8.7 KB

    Name:  sshot2.png
Views: 133
Size:  8.9 KB
    you code can replace bmp ,gif
    but not replace jpg color。because Jpg is a compressed image code

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,998

    Re: how all find color code and replace color of picture

    That's a problem with lossy compression, bits get "rounded" like river stones.

    To deal with that you'd need an algorithm for detecting pixels that approximate a target color. Could be complicated if edge pixels get averaged together.

  10. #10
    Hyperactive Member
    Join Date
    Apr 2017
    Posts
    415

    Re: how all find color code and replace color of picture

    If that's the case, which now that you mentioned it, about the RGB then the little code snippet I posted would work just the same with minor change, wouldn't it.

    Make sure Picture1.ForeColor is black

    Code:
    Private Sub Command3_Click()
     For x = 0 To Picture1.ScaleWidth - 1
       For y = 0 To Picture1.ScaleHeight - 1
         If Picture1.Point(x, y) = RGB(170, 173, 164)  'Find color
    
               Picture1.PSet (x, y) 'Make it black
     
        End If
       Next y
     Next x
    End Sub

  11. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,079

    Re: how all find color code and replace color of picture

    Technically yes. Not the quickest, of course.
    If it were me, I would at least store the return of the RGB function in a Long before starting the loops and compare against that so I wouldn't have the overhead of calling the RGB function, possibly a million times or more.

    One would hope, in this situation of passing literal constants to the function that the compiler would do that optimization for you, storing the value in a register and using efficient looping and test OpCodes, but I wouldn't normally count on it.

    PSet, while convenient, is a fairly slow way of setting pixels in a bitmap.
    Last edited by passel; Jan 16th, 2019 at 11:26 AM.

  12. #12
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,444

    Re: how all find color code and replace color of picture

    Okay, I took a look at Dil's code ... and here's what he's doing: He's moving the picture to a Long array, doing the color swap, and then putting the Long array back into the picture. Basically, this is just circumventing all the overhead associated with PSet. Now, it'd be fairly easy to smarten-up his code. Here's the critical loop:

    Code:
    
        'Replace the color:
        For I = 0 To UBound(PixelValues)
            If PixelValues(I) = FromColor Then
                PixelValues(I) = ToColor
            End If
        Next
    
    

    Basically, if you wanted to do a better job in the JPEG situation, you'd need to search for a small range (different subtle shades) around the FromColor value. There would be several ways to do this. One would be to figure out all the colors in that small range. For instance, the Red, Green, & Blue are in the first three bytes of the Longs. Therefore, you could do something like this ...

    Code:
    
        Dim FromColor1 As Long, FromColor2 As Long, _
            FromColor3 As Long, FromColor4 As Long, _
            FromColor5 As Long, FromCOlor6 As Long
        FromColor1 = FromColor + &H1&
        FromColor2 = FromColor - &H1&
        FromColor3 = FromColor + &H100&
        FromColor4 = FromColor - &H100&
        FromColor5 = FromColor + &H10000
        FromCOlor6 = FromComor - &H10000
    
    

    ... and then compare all these "FromColor?" values (including the original FromColor). If any one color missed by one "shade" value, then it would still match and be replaced.

    However, an even better approach would be to use the CopyMemory into an array of the following Type, rather than array of Longs. These would still be four-bytes per pixel, so the CopyMemory would be the same.

    Code:
    
    Public Type RgbQuadType ' 4 bytes
        Blue        As Byte
        Green       As Byte
        Red         As Byte
        Alpha       As Byte
    End Type
    

    You'd also need to decompose your FromColor value, but that's not terribly difficult. Just create a single RgbQuadType variable, and use CopyMemory to copy FromColor into it. Then, you could build an If statement for the loop that checked for subtle shade differences that may simultaneously happen on the R, G, or B values.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  13. #13
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,998

    Re: how all find color code and replace color of picture

    Here is a reworked version that uses color approximation via a simple Euclidean color distance algorithm. It isn't too awfully slow but it is no longer reversible either by its nature.

    Name:  sshot1.jpg
Views: 78
Size:  35.8 KB

    Here we replace anything approx. yellow by white:

    Name:  sshot2.jpg
Views: 86
Size:  36.9 KB


    Code:
        'Replace the color:
        For I = 0 To UBound(PixelValues)
            With PixelValues(I)
                'Squaring via multiplication, the compiler should optimize the
                'subtraction within subexpressions:
                Delta = (CLng(.rgbRed) - CLng(RGBQUADFromColor.rgbRed)) _
                      * (CLng(.rgbRed) - CLng(RGBQUADFromColor.rgbRed)) _
                      + (CLng(.rgbGreen) - CLng(RGBQUADFromColor.rgbGreen)) _
                      * (CLng(.rgbGreen) - CLng(RGBQUADFromColor.rgbGreen)) _
                      + (CLng(.rgbBlue) - CLng(RGBQUADFromColor.rgbBlue)) _
                      * (CLng(.rgbBlue) - CLng(RGBQUADFromColor.rgbBlue))
            End With
            If Delta <= ToleranceSquared Then
                PixelValues(I) = RGBQUADToColor
            End If
        Next

    The attachment is larger mainly due the bulkier JPEG sample image.
    Attached Files Attached Files

  14. #14
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,998

    Re: how all find color code and replace color of picture

    Maybe somebody who uses GDI and GDI + more than I do knows of some existing API calls for accomplishing such things more quickly.

  15. #15
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,444

    Re: how all find color code and replace color of picture

    Quote Originally Posted by dilettante View Post
    it is no longer reversible either by its nature
    It's sort of reversible. We could certainly convert all white back to a single yellow value. Also, the original wasn't reversible either if we realize that the TO color may have existed in some pixels before we change all our FROM colors to this new TO color. In this case, if we "reversed", more pixels would go back to the FROM color than there originally were.

    Quote Originally Posted by dilettante View Post
    Maybe somebody who uses GDI and GDI + more than I do knows of some existing API calls for accomplishing such things more quickly.
    Personally, I like your Delta approach. And I can't see how some built-in function in the GDI or GDI+ would be much faster, as it's going to require a loop no matter what we do. And looping through and testing Long values is pretty darned fast. If we stick with PSet, we've got to reach into the GPU's memory for each pixel (which is overhead). At least with your approach, you're reaching in once to get all the pixels, and again to put them back, and that's it. Maybe something like Direct2D could be a bit faster at that (as it interfaces more closely with the GPU), but that seems like overkill for this task, and you'd probably still have the same loop.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  16. #16
    Addicted Member
    Join Date
    Nov 2018
    Posts
    134

    Re: how all find color code and replace color of picture

    Attached is a framework for a possible GDI+ solution to the problem.

    I don't understand the math behind the pixel manipulation regarding the shades of a color,
    so I just replaced ONE color with another.

    Maybe this can't be modified to do what dilettante does... not sure.
    Attached Files Attached Files

  17. #17
    Addicted Member
    Join Date
    Nov 2018
    Posts
    134

    Re: how all find color code and replace color of picture

    I revised somewhat to make an array of ColorMaps to match dilettante's 1600 color tolerance range.

    There are no loops, and the speed seems fast.

    I still don't have any math to get the yellow range.
    When this is run, my vbYellow starting point replaces near black pixels.
    Attached Files Attached Files

  18. #18
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,646

    Re: how all find color code and replace color of picture

    Another approach would be: first convert the color to replace and the replacement color to HLS, then find the difference of Hue, Luminance and Saturation that need to be done.

    In the loop, once identified the pixels that are in the range (tolerance) to be replaced, convert the color to HLS and apply the HLS difference, then back to RGB.

    This will allow reversibility.

  19. #19
    Hyperactive Member
    Join Date
    Apr 2017
    Posts
    415

    Re: how all find color code and replace color of picture

    If the point is to change a color to a different color as OP stated why would you want to reverse that

  20. #20
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,998

    Re: how all find color code and replace color of picture

    We got off on a tangent there, I agree. Reversing the process has nothing to do with the problem. An observation blew up into a distraction.

  21. #21
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,646

    Re: how all find color code and replace color of picture

    Quote Originally Posted by Eduardo- View Post
    Another approach would be: first convert the color to replace and the replacement color to HLS, then find the difference of Hue, Luminance and Saturation that need to be done.

    In the loop, once identified the pixels that are in the range (tolerance) to be replaced, convert the color to HLS and apply the HLS difference, then back to RGB.

    This will allow reversibility.
    It is not perfect because some colors get saturated.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width