Extract the object from the image by cutting the edges in C# - c#

I am trying to crop the object from its background on an image using AForge.net. I have detect the edges of the object using cannyEegeDetector, but I don's know how to crop it using the detected edges.
Do you have any ideas to crop the object from the background?

I don't know anything about the data structure that CannyEdgeDetector returns, but you probably want to treat it with an algorithm similar to contour filling in the selected area (kinda like the fill bucket in MS Paint). On each row of the images pixels, start from the left side of the image scanning right, and each time you cross a boundary, you toggle whether it's in the "fill area". Pixels in the fill area are preserved, ones outside of it are cropped. Hope that helps!

Related

Find image within image (object detection)

I do have different images which all have some kind of border around the "real" image. What I would like to achieve is to find the "real" image (size and location in pixels).
For me the challenge is that the border is not always black (can be any kind of black or grey with a lot of noise) and the "real" image (water with shark in this example) can have any combination of color, saturation, ...
Now in general I'm aware of algorithms like Canny, Blob detection, hough lines, ..., but I have just started using them. So far I managed to find the border for a specific image, but as soon as I try to apply the same algorithms and parameters to the next image it doesn't work. My current approach looks like this (pseudo code):
convert to gray CvInvoke.CvtColor(_processedImage, tempMat, CvEnum.ColorConversion.Rgb2Gray)
downsample with CvInvoke.PyrDown(srcImage, targetImage) and CvInvoke.PyrUp(srcImage, targetImage)
blur image with CvInvoke.GaussianBlur(_processedImage, bluredImage, New Drawing.Size(5, 5), 0)
Binarize with CvInvoke.Threshold(_processedImage, blackWhiteImage, _parameters.BinarizeThreshold, 255, CvEnum.ThresholdType.Binary)
Detect Edges with CvInvoke.Canny(_processedImage, imgEdges, 60, 100)
Find Contours with CvInvoke.FindContours(_processedImage, contours, Nothing, CvEnum.RetrType.External, CvEnum.ChainApproxMethod.ChainApproxSimple)
Assume that largest contour is the real image
I already tried different approaches based on for example:
Thresholding saturation channel and bounding box
Thresholding, canny edge and finding contours
Any hint especially on how to find proper parameters (that apply for all images) for algorithms like (adaptive) threshold and canny as well as ideas for improving the processing pipeline would be highly appreciated.
you can try to subtract black image from this image , and you will get the inside image , way to do this:
Use image subtraction to compare images in C# ,
If the border was uniform, this would be easy. Use cv::reduce to find MIN and MAX of each row and column; then count the top,left,bottom,right rows/columns whose MIN and MAX are equal (or very close) to the pixel value in a nearby corner. For sanity, maybe check the border colour is the same on all sides.
In your example the border contains faint red stuff, but a row/column approach might still be a useful way to simplify the problem. Maybe, as Nofar suggests, take an absolute difference with what you think is the background colour; square it, convert to grey, then reduce to Sums of rows and columns. You still need to find edges, but have reduced the data from two dimensions to one.
If there's a large border and lots of noise, maybe iterate: in the second pass, exclude the rows you think comprise the border, from statistics on columns (and vice versa).
EDIT: The above only works for an upright rectangle! If it could be rotated then the row/column projection method won't work. In that case I might go for sum-of-squared differences as above (don't start by converting to grey as it could throw away information), followed by blurring or some morphology, edge detection then some kind of Hough transform to find straight edges.

Win2D draw single image

I'm able to fill a rectangle with an image and i apply a mask on top of the image using this code
args.DrawingSession.FillRectangle(rect, imgRnd, mask);
i need to apply some transform to this image, i'am able to do that with no issue, but i have encounter a strange issue, the last pixel is repeated.
i have used
imgRnd.ExtendX = CanvasEdgeBehavior.Wrap;
imgRnd.ExtendY = CanvasEdgeBehavior.Wrap;
and the image is repeated continuously.
My question is : there is a way to draw one time the image disabling and ExtendX and ExtendY?
FillRectangle will always fill all the pixels within the specified rectangle. The edge behavior enum controls what value they are filled with if the image is positioned such that it does not completely cover the rectangle being drawn.
How exactly are you transforming the image? Can you change that to also transform the rectangle itself, so you won't be trying to fill pixels that aren't covered by the image?
Another option is to use image effects (Microsoft.Graphics.Canvas.Effects namespace) which give much more detailed control than FillRectangle over how multiple images are transformed, combined, etc.

Cropping a Bitmap to an Area of Interest

Similar to my previous question which I as yet have not solved (Comparing Frames of a live Feed) I have another issue.
Scenario
I have an image taken by a camera that contains a rectangle in it. I need to crop the image to only show the rectangle plus a small margin.
My Efforts
I have accomplished this by iterating through the pixels using LockBits and attempting to find potential edges but these seems terribly slow and inefficient
My Thoughts
I was thinking I could take an empty image as a baseline and then remove the differences between the two, however I cannot be sure that the lighting will be exactly the same and that potential contaminants such as an accidental fly getting into the image will not be present which could muck up this process.
Is there any easier way? The rectangle should (usually) be in the bottom left corner, but not always (long story) but this cant be relied upon.
My Environment
Visual Studio 2012 (2010 if neccessary is available)
Ueye camera
C#
The images are of type System.Drawing.Bitmap
The rectangle will often be something like a credit card or an ID card or anything of a similar size and shape
The empty image (background) looks like this:
Using EmguCV you can detect shapes such as a rectangle. Click here for the emgu code. Once you have detected the rectangle it is fairly easy to crop it out using a new Bitmap with the size of the rectangle.
The sample demonstrates how to crop the image from specific Picturebox control into destination Picturebox control using mouse selection or specified coordinates.
1.How to use mouse to select an area (rectangle) in a Picturebox control.
2.How to crop the image by the rectangle.
http://code.msdn.microsoft.com/windowsdesktop/CSWinFormCropImage-d4beb1fa

Making anchor/connection points for lines on bitmaps in picturebox?

I am making a logical gates application and I can currently drag out bitmaps into the picturebox. These bitmaps are the logical gates. Now I need a way to draw the lines to connect inputs with gates. I would like to have the line sort of stick to connection or anchor points on the bitmap that I place but I have no idea how to do this.
Each bitmap dragged out is an object with a size and an x y position.
Thanks for any help! I have been searching for a solution for a while now.
I basically ended up making 10x10 rectangles positioned according to the height/width of the object that would update when the bitmap was moved. The bitmap was also its own rectangle.

Image Co-Ordinate System Design

I'm diving into something without sufficient background, but I feel like there may be simple solutions that don't require me to have in depth knowledge of the topic.
What I am trying to do is have an image co-ordinate system. Basically the user will supply an image, like a house plan. They can then click on points in the image and create markers (like google maps). The next time they retrieve the map, all the markers they added before are there and they can add new ones.
I need to identify the points these markers are located on so I can store that information. I also need to be able to create a layer on the image that contains the markers and renders them in the exact locations they were placed.
I imagine the easiest way to do this is to use pixel co-ordinates...the rub here is that the image won't be a fixed size since there is a web application and an IPad application, so the co-ordinate system needs to work as long as the image is in the same size ratio.
The server size is .NET and as mentioned there is an IPad app, so the solution needs to be viable given that tech stack.
Any ideas?
Instead of using pixel coordinates in absolute terms, you can use the 0 to 1 range. The top left corner is (0,0), bottom right is (1,1) and the center of the image is (0.5,0.5). This way not matter what image size (or zoom level) you have, the markers will always be in the same place.
My suggestion is don't try to figure out the correlation between the actual image and the coordinates. The only thing I would do is use the resolution of the image, aka 800x600 and use that for your grid. Then overlay your markers using that grid on the image. The points you'd remember would just be X and Y values and maybe a tag name/id.

Categories