OpenCV Identifying Lines and Curves - image-processing

I'm just starting to learn OpenCV programming. May I just ask about how can I identify lines and curves in OpenCV? My problem is that I have to identify if the image contains a convex or concave (horizontal or vertical curve) curve, a vertical, diagonal or a horizontal line.
In my code, I used CvSetImageROI to take a particular part of an image, and then I'm trying to identify each according to the said lines/curves.
Are there functions in OpenCV that are available? Thank you very much for the help. By the way, i'm using Linux and C++.

Hough transform http://en.wikipedia.org/wiki/Hough_transform, http://homepages.inf.ed.ac.uk/rbf/HIPR2/hough.htm
is the standard way to do it. In its simple form (as implemented in OpenCV) it can detect lines of arbitray position and angle and line segments.
Look here for an example
http://opencv.itseez.com/modules/imgproc/doc/feature_detection.html?highlight=hough#houghlinesp
For curves, the detection process is a bit more complicated, and you need the general Hough transform It is not yet available in OCV, but you can write it as an exercise or look for a good implementation.
http://en.wikipedia.org/wiki/Generalised_Hough_transform describes it (in short)

Related

Vectorization of binary grayscale image

I have binary grayscale bitmap images (black and white) that contain lines, curves and some simple shapes (ellipses, and polygones), my goal is to describe these elements as formulas.
One of the options is to apply vectorization on the images, but I am not expert in this domain so I need your help in suggesting what can I do. is there any tool or library that is able to provide the formulas that describe these objects?
Thank you
Perhaps cubic bezier is what you need:
This is a project I've done, (1) I use Ramer-Douglas-Peucker to remove noise and (2) represent the curves as cubic bezier I obtain by using least square fitting:
This is original drawing:
Vectorized image:
Since it's already converted to mathematical formula, It can be zoomed infinitely.
Sorry I can't share the code since it's quite enormous but I hope you get the idea.
If you want tracing library you can use this: http://potrace.sourceforge.net/
Also If you're interested to only remove noise you can try CSS: http://www.morethantechnical.com/2012/12/07/resampling-smoothing-and-interest-points-of-curves-via-css-in-opencv-w-code/
If you have nice proper uninterrupted shapes you can just trace their contours using something like findContours().. But if your input (that you did not describe properly) is noisy and sketchy, the approach should rely on a Hough transform, see below. By the same coin, in fitting curves a lot will depend on the level of noise and the presence of outliers (e.g. background elements that aren't shapes or are inaccurate shapes that only approximate, say a proper ellipse). It is hard to imagine proper clean lines and proper shapes in a typical task unless it is a homework.
Hough lines and Hough circles are the most widely used functions in openCV library. Note that fitting ellipses is non-trivial since they have 5 parameters (lines have 2 and circles have 3) and Hough space grows too much. Rectangles can be found either with Hough lines or a special rectangle Hough. Other shapes can be detected using generalized non-parametric Hough.
Fitting curves should use RANSAC to get rid of outliers, and geometric (least square in terms of point distances) fit to minimize pixel noise. The latter procedure typically involves non-linear optimization that should be initialized by a simpler algebraic fit. Luckily, for simple geometric primitives fitting functions have already been written, see fitLine().
The bottom line, given that your shapes are a bit noisy, your task is non-trivial (to the degree you probably don't realize) and thus should be split on several sub-projects like finding shapes, fitting curves, etc.

curve fitting in OpenCV

Is there any opencv function for curve fitting?
I have a set of points (cv::points) and my aim is to fit these points to a closed/open curve.
Right now I am taking a pair of points and drawing lines with them, effectively forming a curve.
It's not quite clear from your question whether you want to smooth the curve by adding more points or to summarise it by using fewer points. If it's the latter, perhaps you should consider cv::approxPolyDP, which is documented here and copied below for reference.
I think you are talking function approximation and interpolation.
As I know, there's not a function directly about curve fitting.
If you just want to get the fitting result, you can use Matlab's curve fitting toolbox, where there is a tool named cftool. cftool is a GUI tool, you can specify the input points and the interpolation method and get the result formula.

Decompose simple shapes from Binary Images with OpenCV

How do I decompose a complex Shape into simple shapes from binary images with OpenCV? I would post an image...but it is not allowed for new users. My example is a box overlaid on another box and both boxes are the same exact color(1 color)....I want to break this into two boxes....what is the best way to do this?
I would suggest using Hough transform for your specific concern as it is powerful to detect overlapping parametric forms.
Beware, wikipedia may not be the best source to introduce Hough transform !
Originally Hough transform was used to detect lines inside images but the generalisation of the concept is easy to implement.

square detection, image processing

I am looking for an efficient way to detect the small boxes around the numbers (see images)?
I already tried to use hough transformation with no success. Any ideas? I need some hints! I am using opencv...
For inspiration, you can have a look at the
Matlab video sudoku solver demo and explanation
Sudoku Grab, an Iphone App, whose author explains the computer vision part on his blog
Alternatively, if you are always hunting for the same grid you could deploy something like this:
Make a perfect artificial template of the grid and detect or save all coordinates from all corners.
In the target image, do the same thing, for example with Harris points. Be creative, you might also be able to use the distinct triangles that can be found in your images.
Using the coordinates from the template and the found harris points, determine the affine transformation x = Ax' between the template and the target image. That transformation can then be used to map the template grid onto the target image. At the very least this will give you some prior information to help guide further segmentation.
The gist of the idea and examples of the estimation of affine matrix A can be found on the site of Zissermans book Multiple View Geometry in Computer Vision and Peter Kovesi
I'd start by trying to detect the rectangular boundary of the overall sheet, then applying a perspective transform to make it truly rectangular. Crop that portion of the image out. If possible, then try to make the alternating white and grey sub-rectangles have an equal background brightness - maybe try adaptive histogram equalization.
Then the Hough transform might perform better. Alternatively, you could then take an approach that's broadly similar to this demonstration by Robert Bemis on MATLAB Central (it's analysing a DNA microarray image rather than Lotto cards, but it's essentially finding bounding boxes of items arranged in a grid). At a high level, the approach is to calculate the autocorrelation along columns and rows of pixels to detect the periodicity of the items in the grid, and use that to impose a bounding box on each item.
Sorry the above advice is mostly MATLAB-based; I'm afraid I'm not an opencv user, but hopefully it will give you some ideas at least.

Detect a pattern highlighted by infrared light with openCV

For a project I've to detect a pattern and track it in space despite rotation, noise, etc.
It's highlighted with IR light and recorded with an IR camera:
Picture: https://i.stack.imgur.com/RJuVS.png
As on this picture it will be only very simple shape and we can choose which one we're gonna use.
I need direction on how to process a recognition of these shapes please.
What I do currently is thresholding and erosion to get a cleaner shape and then a contour detection and a polygon approximation.
What should I do then? I tried hu-moments but it wasn't good at all.
Could you please give me a global approach to recognize and track such pattern in space?
Can you choose which shape to project?
if so I would recomend using few concentric circles. Then using hough transform for circles you can easily find the center of the shape even when tracking is extremly hard (large movement/low frame rate).
If you must use rectangular shape then there is a good open source which does that. It is part of a project to read street signs and auto-translate them.
Here is a link: http://code.google.com/p/signfinder/
This source is not large and it would be easy to cut out the relevant part.
It uses "good features to track" of openCV in module CornerFinder.
Hope it helped
It is possible, you need following steps: thresholding image, some morphological enhancement,
blob extraction and normalization of blob size, blobs shape analysis, comparison of analysis results with pattern that you want to track.
There is many methods for blobs shape analysis. Simple methods: geometric dimensions, area, perimeter, circularity measurement; bit quads and others (for example, William K. Pratt "Digital Image Processing", chapter 18). Complex methods: spacial moments, template matching, neural networks and others.
In any event, it is very hard to answer exactly without knowledge of pattern shapes that you want to track )
hope it helped

Resources