Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cropping the borders of image based on color in windows phone

enter image description here

Above is the image i am using. What i am trying to achieve is removing the red portion of the border from the image. How can I achieve this programmatically in windows phone? I found WriteableBitmapExtensions.Crop() method, but I am confused with the arguments (how i can find the x,y position of the image, as well as the size and the width?)

Also another issue I am facing is: I will get the images with differently sized borders, so I can't hardcode the x or y values.
Can anyone suggest a solution, or guide me to solve the issue?

like image 916
StezPet Avatar asked Jun 06 '13 12:06

StezPet


People also ask

How do I crop a picture using paint?

Right mouse click on the selection and choose Crop. Right mouse click on the image and choose Resize. Click on Pixels. Enter horizontal or vertical size (the other side will self adjust) • Save and name.


1 Answers

This is not such a trivial thing and you haven't shared any code with us, so I can give you a few suggestions. Every WriteableBitmap has width and height defined. You should be able to access it via

wb.PixelWidth;
wb.PixelHeight;

where wb is your WriteableBitmap (the picture)

Having said that, it's trivial to crop a WriteableBitmap using WriteableBitmapEx library

var croppedBmp = wb.Crop(10, 10, 300, 220);

If your wb was 320x240 and the border was of width 10, then the above Crop call will do the trick - you will take the inner rectangle starting from point (10,10) and ending at (310, 230)

Now to your second issue - not knowing the width of the border. It would help if you know that

  1. Border is of the same thickness on every side of the picture
  2. Border is always in one color only

Assuming that's true, you could think of a simple algorithm (that may not be correct every time, but you can test it and adjust) which would take a few random points, for example

(0,randNumber < wb.PixelHeight), (randNumber < wb.PixelWidth, 0), (wb.PixelWidth, randNumber < wb.PixelHeight), (randNumber < wb.PixelWidth, wb.PixelHeight)

and then move towards the inner part of the picture as long as the neighbour pixel is the same color as the starting pixel. The more points you take randomly, the better chances you have of getting it right. The obvious problem with this is that it may happen that something on the picture is the same color as the border (exactly the same) which will make it seem like the border is wider than it really is. That's why you should take more points.

If you showed some code, I'd be happy to expand the answer.

like image 123
Igor Ralic Avatar answered Nov 14 '22 22:11

Igor Ralic