How exactly do I use OpenCV and homographies to undo perspective warp? - java

I need to use Java and OpenCV to do the process shown on http://en.wikipedia.org/wiki/Perspective_control
I know exactly where the 4 points of my trapezoid are, and I just need them to square up. I've heard I need to use a homography and a warpPerspective command, but I don't know what parameters these use. I don't know HOW to employ them, only that I do need to use them. Can anyone fully explain this? I've never done anything like this, so starting from a bare minimum would be awesome. I'd really like to get into the world of computer vision so having someone to help me out is greatly appreciated.

Their are two methods for doing this:
1) Geometric warping where you use perspective geometry to create the H matrix. OR
2) Using feature matches to create that same H matrix.
Opencv has a nice function called findHomography (which is the second method) that takes in 2 arrays. Both arrays should be matching features from the 2 different images.
Once you have the H matrix creating the warped image is also easy and you can just call warpPerspective which you store in a matrix which you then imshow to see.

Related

Curve matching in Java

First question here so sorry if anything I ask is completely stupid.
I'm working in a shape recognition project where it is supposed for me to develop an application that receives two images: an original one and a sketch made by a user. I am supposed to detect contours of the two images and find the best match in the original image corresponding to the sketch made by the user.
I am already learning some basics about the Canny edge detection and was able to get the contours of several images. After having the contours, I need to analyze all contours in the image and find the best match, disregarding translation, rotation, scaling and occlusion.
Then, I found this code that does exactly what I want:
http://www.morethantechnical.com/2012/12/27/2d-curve-matching-in-opencv-w-code/ but is in C++.
Do you know any alternative for similar code in Java or any algorithm that could be useful to me? I also discovered BoofCV but it seems that such task is not implemented.
Thank you for your patience.
EDIT:
I've been searching for other ways of doing this, and I found the Hausdorff distance:
http://cgm.cs.mcgill.ca/~godfried/teaching/cg-projects/98/normand/main.html
Is it possible to modify this algorithm to be rotation invariant? They only talk about translation and scaling.
As you mentioned you already have the source code in C++ and can't find a Java version - possibly your best bet might be to convert the C++ code into Java code. If you don't need all parts of the C++ program, you might want to covert only the parts (classes) that you need.
Conversion from C++ to Java might not be always trivial but I am guessing it might be easier if you know exactly what you want and how you want your program to behave. Below is a link to some conversion tools - although they might not be free.
http://www.researchgate.net/post/How_to_convert_the_C_C_code_to_java2

How to calculate eigenvalues without using external packages

I'm trying to create an application in java which does several matrix modifications like calculating the invereses and determinants.
Now I would also like to include the option for the application to calculate the eigenvalues and the eigenvectors for matrices.
Since the only 'solid' way to calculate eigenvalues, by my knowledge, is by using the characteristic formula given by:
det(A-λI) = 0
Where A is an nxn matrix and λ a real number.
To my knowledge, there is no simple, maybe none at all, way to use algebra in Java. Also I would like to program this myself, so I would like not to use external packages like Jama or others.
Can someone explain me how I can program this equation in Java or maybe tell me another way of doing it?
One way you could do it is have a look at Jama and see how it is calculated in there and interpret that. And don't just Copy and Paste :P we all know who tempting that can be.
Finding eigenvalues and eigenvectors is a bit tricky, and there are many algorithms with varying positives and negatives. I'll suggest a few that are quite good and that are not that difficult to implement.
First, compute the characteristic polynomial and then find the roots using. Then you have the eigenvalues. Then you can solve a set of equations to find the eigenvectors given the eigenvalues.

Output of 2D DFT in jTransforms

I'm trying to write a high/low pass image filter using jtransforms. Everything is working very nicely in the sense that I can transform an image using the complexForward method of the FloatFFT_2D class, and then come back to exactly the same picture using the complexInverse method. I'm using a float[] input rather than a float[][].
However, to apply the filter I need to remove some of the frequency components in between these two stages. My problem is that I don't know what the output looks like, or in other words, where within the output array the different spatial frequencies are stored. Is a[0] the DC value, for example?
The documentation isn't particularly forthcoming on this, so I'd be grateful if anyone knew the answer!
Figured it out - the low frequency components are in the corners. So if you need the low frequency components to be at the centre, a java version of fftshift needs to be implemented as explained in the link below:
http://www.mathworks.co.uk/help/matlab/ref/fftshift.html

How to determine the position of a car inside an image?

Is it possible to analyse an image and determine the position of a car inside it?
If so, how would you approach this problem?
I'm working with a relatively small data-set (50-100) and most images will look similar to the following examples:
I'm mostly interested in only detecting vertical coordinates, not the actual shape of the car. For example, this is the area I want to highlight as my final output:
You could try OpenCV which has an object detection API. But you would need to "train" it...by supplying it with a large set of images that contained "cars".
http://docs.opencv.org/modules/objdetect/doc/objdetect.html
http://robocv.blogspot.co.uk/2012/02/real-time-object-detection-in-opencv.html
http://blog.davidjbarnes.com/2010/04/opencv-haartraining-object-detection.html
Look at the 2nd link above and it shows an example of detecting and creating a bounding box around the object....you could use that as a basis for what you want to do.
http://www.behance.net/gallery/Vehicle-Detection-Tracking-and-Counting/4057777
Various papers:
http://cbcl.mit.edu/publications/theses/thesis-masters-leung.pdf
http://cseweb.ucsd.edu/classes/wi08/cse190-a/reports/scheung.pdf
Various image databases:
http://cogcomp.cs.illinois.edu/Data/Car/
http://homepages.inf.ed.ac.uk/rbf/CVonline/Imagedbase.htm
http://cbcl.mit.edu/software-datasets/CarData.html
1) Your first and second images have two cars in them.
2) If you only have 50-100 images, I can almost guarantee that classifying them all by hand will be faster than writing or adapting an algorithm to recognize cars and deliver coordinates.
3) If you're determined to do this with computer vision, I'd recommend OpenCV. Tutorial here: http://docs.opencv.org/doc/tutorials/tutorials.html
You can use openCV latentSVM detector to detect the car and plot a bounding box around it:
http://docs.opencv.org/modules/objdetect/doc/latent_svm.html
No need to train a new model using HaarCascade, as there is already a trained model for cars:
https://github.com/Itseez/opencv_extra/tree/master/testdata/cv/latentsvmdetector/models_VOC2007
This is a supervised machine learning problem. You will need to use an API that features learning algorithms as colinsmith suggested or do some research and write on of your own. Python is pretty good for machine learning (it's what I use, personally) and has some nice tools like scikit: http://scikit-learn.org/stable/
I'd suggest for you to look into HAAR classifiers. Since you mentioned you have a set of 50-100 images, you can use this to build up a training dataset for the classifier and use it to classify your images.
You can also look into SURF and SIFT algorithms for the specified problem.

Simple Square Recognition in BufferedImage Java

I'm currently working on an image recognition software for the robotics club at my school, and one part really has me stumped: shape recognition. I need to be able to detect the squares in this image before I can try to detect the shapes in the arena.
I've looked up some libraries like JavaCV, but I couldn't really find something that suited my taste. As a reference, here is the image from which I'm trying to determine shapes
Have you tried applying the Hough transform?
That seems to be what you need, as your squares have straight edges.
I was doing something similar to your task but I needed to recognize classes (resistors, capacitors, etc.) of objects and what are their boundaries in a real black&white photo:
Basically, the method was something like this:
Preprocessing - correct contrast, brightness, erosion, dilation, median, etc. - this step can be adaptive to whole/part of the photo.
Segmentation - now find parts of the photo where there could be "something" with some threshold for area, pixel intensity, etc.
Characterize - for every found potential segment calculate some characteristcs - max length, area, W, M - determinants, etc.
Classify - There're several classifiers that checked if the given characteristic can be of this class, and if the answer is yes - what is the "distance" of the given characteristic to the ideal model characteristic. Classification was done using fuzzy logic inference.
And of course - for every successful classification take the best matches if they exist.
In your case - the simplest characterization of a square is to find out its area and the max distance between two points that belongs to found segment. And before it you should preprocess the image with "closing" operation (dilation->erosion)
You could also create nice algorithm to recognize if a square is cut by a line (and remove that line - then recognize again) and check if a square is overlapped by other square, etc.
Personally, I don't know any library that do such complex things as library.recognizeSquaresOnImage(params). You are provided with some useful methods to prepare an image for recognition - the core of your task - you must do by yourself.
Every recognition problem has its own peculiar features that can be used to narrow uncertain results in every step in "recognition pipelie". For example, in my task, I knew that objects are black on a fairly white background, and are more or less separated from each other, etc.
My project was written in C++ using OpenCV library and I was using OpenCV library for only reading/writing image and displaying it in the window - I wasn't allowed to used any other methods of the library.
As a reference how you could do it - HERE is the whole project. Even now it doesn't work perfectly - it needs some calibration of classificators.
To have a better grasp how it works on a higher level - take a look at main.cpp file.

Categories

Resources