C# Image Processing Whitespace - c#

I have a ton of pdfs scans that I have converted to images. Most of these scans contain a lot of whitespace around the edges.
What is the best way to go about finding a boundingbox for the actual content and then subsequently removing the whitespace?
I've thought about writing a program that just displays the image, then you drag a box and its saves the image, and moves on to the next one. This would be VERY time consuming, but it would get the job done. I'd like to be able to automate this process somehow using C#.
Either buy just cropping the image or by perhaps by suggesting a bounding box.

Emgu CV (on SourceForge) is a .NET wrapper around OpenCV, which has numerous image manipulation capabilities, including image filters and a bounding box algorithm that could solve this pretty easily.

http://code.google.com/p/aforge/
Aforge is a complete C# library Not a wrapper. OpenCV is very professional tool in compare of AForge.

Are you talking about scanned documents or scanned photos ? What format are your images in ? It sounds like you need an AutoCrop function.
Here is a freeware C# component that has an autocrop function. It should work well on B/W documents. You will need to see if it works the way you want if you are using photos.
http://www.hi-components.com/nievolution_features.asp
This component would also allow you to write code to load your images, draw a bounding box and and then save the cropped images as needed.

Related

Separation and analysis of an image

Here's the scenario:
I am using Visual Studio 2008 with .NET framework 3.5. I am using C#. And for database I am using MySQL. I have a picturebox on a form and 10-12 buttons (each with some image manipulation function). On clicking one of the buttons openfiledialog box is shown up where the user can select the specific file to provide to the program. On clicking another button the program should perform the actions as explained below.
I have an image of a circuit. Suppose this is the image which is provided to the program. e.g.
What I intend to do is that - the program should hypothetically label the circuit as follows:
and then it should separate the image and store the information in a database.
Is there any way to do that. Can anyway tell me the approach to do that? Any help or suggestions please.
Thanks.
In image processing, the problem of finding the 'parts' of the circuit is known as connected component labeling. If you are using C#, I believe that you can use EmguCV (a wrapper to the OpenCV library) to solve the first part of the problem. To do that, you have to consider that the white pixels are the background and that the black pixels are objects.
Now that you have the separated traces, the problem is reduced to finding and labeling the white dots. Again, you can solve it by connected component labeling, but now the objects are represented by white pixels and the background are the black pixels.
At least for your example case, a very simple algorithm would work.
Find a black pixel from the image
Using a flood-fill algorithm, find all the pixels connected to it, and separate it. That's one of your traces.
Working with the separated trace, find a white pixel and use a flood-fill algorithm to find all the pixels connected to it. If you run to the edge of the image, it's not a hole. If you don't, it might be a hole, or a loop in the trace. Use a threshold for the hole size to determine if it's a terminal hole or a loop.
Label the hole and remove it from consideration. Repeat until there are no more unprocessed white pixels.
Remove the whole trace from consideration, and jump to 1.
When there are no more black pixels in consideration in step 1, you're done.
You should probably get pretty far with a basic image editing library that has a flood-fill function, a function to separate a certain color into a new image, and a function to replace colors (the last two are trivial to implement, and there are plenty of flood-fill algorithms available online). You can use different colors to mark different things, for instance, color everything "no in consideration" red. It also makes for an interesting visualization if you look at it in real time!

Freely transforming an image to correct perspective

C# newbie here so please forgive me if my terminology isn't quite correct.
As part of this project, I have a user hold up a piece of paper to a webcam so I can capture, isolate and then eventually display back what they've drawn on it. I've put some restrictions on where the corners of the paper have to be in order for the program to accept it, but there's still the chance that it's distorted by perspective.
Here's an example image that I've captured and isolated the paper out of: image
What I want to be able to do is to distort this image so that the corners of the piece of paper are turned back into a 8.5x11-proportioned rectangle (as if the user had scanned it rather than held it up to the webcam). Rotation and skewing can only get me so far, ideally I would be able to freely transform the image, like in Photoshop. I found this example, I am basically trying to do the opposite. Curious if anyone's had to do this, before I start trying to reverse that four-point image distortion example.
This is sometimes called a Quadrilateral warp.
Disclaimer: I work for Atalasoft.
Our DotImage Photo SDK can do this and it's free. Look at QuadrilateralWarpCommand. You need to know the source and destination quadrilateral.

Capture a single pixel row from each frame of video and compile them together

I'm working on a project where I need to take a single horizontal or vertical pixel row (or column, I guess) from each frame of a supplied video file and create an image out of it, basically appending the pixel row onto the image throughout the video. The video file I plan to supply isn't a regular video, it's actually just a capture of a panning camera from a video game (Halo: Reach) looking straight down (or as far as the game will let me, which is -85.5°). I'll look down, pan the camera forward over the landscape very slowly, then take a single pixel row from each frame the captured video file (30fps) and compile the rows into an image that will effectively (hopefully) reconstruct the landscape into a single image.
I thought about doing this the quick and dirty way, using a AxWindowsMediaPlayer control and locking the form so that it couldn't be moved or resized, then just using a Graphics object to capture the screen, but that wouldn't be fast enough, there would be way too many problems, I need direct access to the frames.
I've heard about FFLib, and DirectShow.NET, I actually just installed the Windows SDK but haven't had a chance to mess with and of the DirectX stuff yet (I remember it being very confusing for me a while back when I messed with it). Hopefully someone can give me a pointer in the right direction.
If anyone has any information they think might help, I'd be super grateful for it. Thank you!
You could use a video rendered in renderless mode (E.g. VMR9, EVR), which allows you to process every frame yourself. By using frame stepping playback you can step one frame each time and process the frame.
DirectShow.NET can help you to use managed code where possible, and I can recommend it. It is however only a wrapper to DirectShow, so it might be worthwhile to look for more advanced libraries as well.
A few sidenotes: wouldn't you experience issues with lighting which differs from angle to angle? Perhaps it's easier to capture some screenshots and use existing stitching algorithms?

Simple component to crop a picture with rectangle box in c#.net windows application

I want a simple component to crop a picture with Rectangle box in c#.net windows application.what component can i use?(free or purchase)
Here's some code I wrote to resize an image. The code will also trim the image if needed.
It provides an example of cropping and could easily be modified to be a more general "cropper" class.
I used ClearImage for image manipulation at a previous job. I found it very easy to use, with good performance and it would serve your purpose perfectly.
There is also AForge, a open source image manipulation library. A little more powerfull, with a lot of features, but also a little more complicated.
Or you could design your own using the PictureBox and the Bitmap class. A lot of work, and probably not very performant if you don't know what you're doing...

how to detect blobs and crop them into png files?

i've been working on a webapp. i got stuck here in a problematic issue.
i'll try to explain what im trying to do.
here you see first big image which has green shapes in it.
what i want to do is to crop those shapes into different png files and make their background transparent like the example cropped images below the big one.
The first image will be uploaded by user and i want to crop into pieces like the example cropped images above.it can be done with GD library of php or by a server-side software written in python or c#. but i dunno what this operation called so i dunno what to google to find information. it is something to do with computer vision detecting blobs and cropping them into pieces etc.
any keywords,links would be helpful.
thanks for helps
A really easy way to do this is to use Flood Fill/Connected Component Labeling. Basically, this would just be using a greedy algorithm by grouping any pixels that were the same or similar in color.
This is definitely not the ideal way to detect blobs and is only going to be effective in limited situations. However, it is much easier to understand and code and might be sufficient for your purposes.
Opencv provides a function named cv::findContours to find connected components in an image. If it's always green vs white, You want to cv::split the image into channels, use cv::threshold on the blue or the red channel (those will be white in the white regions and near black in the green region) with THRESH_BINARY_INV (because you want to extract the dark regions), then use cv::findContours to detect the blobs. You can then compute the bounding rectangle with cv::boundingRect, create a new image of that size, and use the contour you got as a mask to fill the new image.
Note: These are links to the C++ documentation, but those functions should be exposed in the python and C# wrappers - see http://www.emgu.com for the latter.
I believe this Wikipedia article covers the problem really well: http://en.wikipedia.org/wiki/Blob_detection
Can't remember any ready-to-use solutions though (-:
It really depends on what kinds of images you will be processing.
As Brian mentioned, you could use Connected Component Labeling, which usually is applied to binary images, where foreground is denoted by white pixels and background by black pixels (or the opposite). The problem is then how to transform the original image to a binary one. If all images are like the example you provided, this is straightforward and can be accomplished with thresholding. OpenCV provides useful methods:
Threshold
FindContours for finding contours of connected components
DrawContours for extracting each component individually into a separate image
For more complex images, however, all bets are off.

Categories