4 Replies - 3580 Views - Last Post: 20 February 2012 - 02:10 PM Rate Topic: -----

#1 WolvesWorld   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-February 12

Edge and color detecting

Posted 18 February 2012 - 10:20 PM

I am scanning a passport picture or any other image with different sizes. after scanning I want to clear the excess area in the image which is color.
How can I automatic detect the excess white color in the image and delete it to get the scanned area I want??


as you can see in the attached black image.
the black area is the area which I want to get finally , and automatic crop for the white area.

regards...

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Edge and color detecting

#2 janne_panne   User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Edge and color detecting

Posted 19 February 2012 - 03:31 AM

What do you think how to get the actual image? I'm not very good at image detection or stuff like that but what comes to my mind is to check the pixels of the whole image and find the corners of the actual image you need and then calculate the area where the actual image is and only take that area.
Was This Post Helpful? 0
  • +
  • -

#3 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Edge and color detecting

Posted 19 February 2012 - 07:24 AM

I would start by scanning the image from all sides, looking for pixels that don't belong to the margin you want removed; store the position of those pixels and use those to extract that exact portion.

Finding the position wouldn't have to be more complicated than this. (Pseudo code)
int GetLeftBound()
    while x < image width
        while y < image height
            if pixel at (x,y) is not a part of the margin
                retrurn x


The part where you check whether the pixel is a part of the margin or not would be the complex part. At it's simplest, assuming a simple white background as the margin (like in your example image), you could just match it against a specific color. However, if you want this to work on actual photographs you'd have to be a bit more creative. Perhaps calculate the color ratio towards either red, green or blue, and only match pixels that have a ratio closer to a specific color. That should work fine if you are cropping images of brightly colored passport (usually blue) on a white background. (Although a green or red background would probably work better.)

Extracting the specified area of the image wouldn't be complicated. You should be able to do that with this overload of the Graphics.DrawImage method. Something along the lines of:
Image CropImage(Image original)
    Rectangle cropArea
    cropArea.x = GetLeft()
    cropArea.y = GetTop()
    cropArea.Width = GetRight() - cropArea.x
    cropArea.height = GetBottom() - cropArea.y
    
    Image cropped
    using (Graphics g = new Graphics(cropped))
        Rectangle destRect = new Rectangle(0, 0, original.Width, original.Height)
        g.DrawImage(original, destRect, cropArea, GraphicsUnit.Pixel)
    
    return cropped;


Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Edge and color detecting

Posted 19 February 2012 - 07:28 AM

There's no "automatically" about it, meaning no single command that is going to do it. Its just brute force.

  • Loop through rows
    • Loop through columns
      • If the color is your magic color move on.
      • If not, make a note of the location (x,y)
    • end loop
  • end loop
  • crop


You just have to track when you see a change from white to black, that will be the left edge. Then from black to white, that will be the right edge.
When there is no transition that is the top and bottom.

What have to you tried to code so far?

What part of what you have tried isn't working?
Are you getting error messages, or is it just not giving you the results you wanted?

(In other words, you at least have to make and show us your good faith effort to do your own coding. We are here to help you, not do it for you.)
What does this error mean?

Suggestion:
Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

// Try #1 - May 1, 0900hrs
//    code
//    code  
//    code
// Try #2 - May 2, 1700hrs   Okay, plan B.  What if I do it *this* way
//    code
//    code  
//    code
// Try #14 - May 3, 0500hrs after 5 cans of RedBull.  Maybe I should get some sleep. I can't think of anything else but this last idea
    code
    code  
    code


If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.
Was This Post Helpful? 0
  • +
  • -

#5 golegogo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-January 12

Re: Edge and color detecting

Posted 20 February 2012 - 02:10 PM

If your looking for edge detection you want to compare the rbg values and determine a threshold that defines a a large change in color because that would be your edge this threshold might have to be change per image though
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1