Question Related Pixel Colors Of BMP/Jpeg, After Getting It Into The Handle

Hello Experts,

I have done a quite bit of programing with bmp's/jpegs using win32 sdk in VC++. Opening the files mixing two images, writing text over it, saving them back etc. types of manipulations I have done, after opening file in HDC handle.

Now I require one more feature for my Image-Related win32 application, for which as far as I am concerned I am not able to work out Any Algorithm(s) Or Any Concept(s), and I need your Help. Here it is ::

After the Bitmap/Jpeg is opened and shown to the user, the user can click on any point on it. I want to get the color of that pixel, no problem I will get it.

Now what I want more is, I have to Trace all those Pixels All-Around this very particular PIXEL, moving in any direction from it HAVING THE SAME COLOR.
The search should stop in any direction  when a pixel of different color is found.

Basically, what I have to do is to make the AREA black in the Bitmap, where a user pin-points and all the surrounding pxiels having the same pixel-color as the pin-pointed pixel color.

How to do that, please help me as soon as possible.

Best Regards
Robert

robert_9999Asked:
Who is Participating?
 
FengYuanConnect With a Mentor Commented:
Reducing stack depth in floodfill:

bool CloseEnough(int x, int y, COLORREF crSeed)
{
    if ( (x<0) || (x>=width) )
        return false;

    if ( (y<0) || (y>=height) )
        return false;

    return Close(GetPixel(x, y), crSeed);
}

void FloodFill(int x, int y, COLORREF crSeed, COLORREF crFill)
{
repeat:

   int minx = x;
   
   while ( CloseEnough(minx-1, y, crSeed) ) minx --;

   int maxx = x;

   while ( CloseEnough(maxx+1, y, crSeed) ) maxx ++;

   FillScan(minx, maxx, y, crFill);

   if ( CloseEnough(x, y-1, crSeed) )
       FloodFill(x, y-1, crSeed, crFill);

   if ( CloseEnough(x, y+1, crSeed) )
   {
       y ++;
       goto repeat;
   }
}

www.fengyuan.com
       
0
 
robpittCommented:
Sounds like you need a flood fill algorithm.
There are plenty of these on the net, heres the first one I found...
http://cs1.mcm.edu/~tmiller/courses/csc-4330/code/areaFill2.html

0
 
NickRepinCommented:
Why not FloodFill() and ExFloodFill()?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
robpittCommented:
Oh yeah, I'd forgotten about that one! :-)
0
 
FengYuanCommented:
1) Find a good flood-fill algorithm. A simple flood-fill algorithm may have a maximum stack depth of n x m.
2) Change the algorithm to generate a set or region of all the pixels have the same color starting from a seed point.
3) Use FrameRgn to draw an outline around these pixels.
4) Use FillRgn to clear the area covered by these piexels, and move somewhere else using FillRgn again.
0
 
robert_9999Author Commented:
Thanks all for the instant support especially NickRepin. He was really very straight in finding the solution. But I have a  bit more query regarding my problem, please see to it, and for that fairly enough I have increased the points also. Here it is ::

You must have seen the Fuzziness (Tolerance) ranging from 0-255, in different graphical packages say most popular Photoshop, everything is OK in ExFloodFill(), it exacly does what I required, It goes in all outward directions until discontinued, relative to a pixel color selected.

But if I increase Fuzziness or Tolerance in PH. it does same but the range of colors selected increases. For example if I PICK a point in Image whose RGB is 245,200,118 and used EXFloodFill() it will go for exact 245,200,118 color in all directions but if I increase some value just like Tolerance in PH., the selection increases.

WHAT PHOTOSHOP SAYS IS may be that can help you ::

"Enter a low value to select colors very similar to the pixel you click or a higher value to select a broader range of colors.", in Tolerance(Fuzziness).

I think I have Asked you much and I guess this concept requires a prepared algo.

Best Regards
Robert
0
 
robpittCommented:
Yeah I think you'll definitely need a custom flood fill algortithm of some kind.

The crude one I point you too could easily be modified to allow a tolerance.

However as pointed out by FengYuan this kind of simple algorithm can potentially recurse upto WxH times which be too much for a large bitmap.

How big is your bitmap?
0
 
robpittCommented:
PS
Off the top of my head a way to adapt the basic algorthim reducing the potentail stack requirement would be to apply it to smaller rectangular sections at a time.
0
 
robert_9999Author Commented:
W-1800 and H-1200 that is 6 by 4 images at 300 DPI.
0
 
robpittCommented:
Thats certainly big enough to cause trouble in the event of an expensive fill scenario.

Lets see if the other guys have anything to say...
0
 
robert_9999Author Commented:
Lets See.
0
 
robert_9999Author Commented:
robpitt, I have worked out the size can reduced to 1200*1200 pixels, wothout much Effecting my program efficiency and working. Thanks

Best Regards
Robert

0
 
robert_9999Author Commented:
robpitt, I have worked out the size can reduced to 1200*1200 pixels, wothout much Effecting my program efficiency and working. Thanks

Best Regards
Robert

0
 
robert_9999Author Commented:
Waiting for ur comments experts.

Best Regards
Robert


0
 
robert_9999Author Commented:
I do not understand the above code FengYuan, please if possible explain it. Also please see the latest of my comments.

Anyway I went to ur site, I liked the book. What is the cost ??

Best Regards
Robert

0
 
robert_9999Author Commented:
What happened to u robpitt, I have considerable reduce the size of the image and also that can be broken ito smaller segments, Now I think your algo which U quoted above as U have can be applied for FLOODFILL With Tolerance.
0
 
robert_9999Author Commented:
No comment/suggestions, please reply FengYuan, robpitt NickRepin.

Best Regards
Robert
0
 
robpittCommented:
I'm afraid I have litle extra to add.

Is there a problem with using something based on Feng's post with its reduced stack requirement?

Rob
0
 
FengYuanCommented:
A simple flood-fill algorithm would check four neighboring pixels, and recursively call with these coordinates.

This changed algorithm always tries to find a scanline, fill the scanline, and then only goes up and down with recursion. The down recursion is also optimized.

Try it, write a working code, and step through it.

www.fengyuan.com
0
 
robert_9999Author Commented:
Thanks FengYuan,

But what about Tolerance (Fuzziness) in it.

Best Regards
Robert

0
 
FengYuanCommented:
That is for: "Fuzziness (Tolerance) ranging from 0-255"
0
 
robert_9999Author Commented:
Sorry Everyone Specially FengYuan, I was out of town without informing you all. I am very sorry for that. But here I am again to work practically on this. You were right, I was given this work by my boss and I was talking everything theoritically, but now the time has come and I will write the Working-Code and see.
Let's see how much time it takes.

Best Regards
Robert

0
 
robert_9999Author Commented:
Thanks All,

I am succeded in what I want.
Meanwhile I have two more questions, which I will ask one by one. I appreciate again your valuable help.

Best Regards
Robert

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.