How does warpPerspective in OpenCV works - java

I have recently started exploring OpenCV and I am having trouble to understand warpPerspective, documentation does not help much. I mean why is it used? And if I need to resize an image, I can use resize method, then what additional benefit will i get using warpPerspective method.

To make it simple, warpPerspective allows you to create a new image from a transformation matrix.
It is much more powerful than a resizing function.
Simple summary :
Example with a very popular application
You have picture 2 above and want to extract the document (result in picture 3).
Get the template of it (picture 1) and compute the homography (transformation matrix between picture 1 and picture 2).
Using this transformation, you will be able to create image number 3 using the transformation matrix in warpPerspective.
Example from an article from learnOpenCV written by Satya Mallick

Related

Skin pores analysis using Blob detection in OpenCV

I am working on a project where I want to detect the pores in a given skin image.
I have tried various methods(HoughCircles, BlobDetection and Contours) from OpenCv using Java, however, I am unable to proceed.
HoughCircles is showing me all false circles and same is the case with contours.
My current code uses blob detection technique which is also not showing what is required. Sample code is written below:
public void detectBlob() {
Mat orig = Highgui.imread("skin_pore.jpg",Highgui.IMREAD_GRAYSCALE);
Mat MatOut= new Mat();
FeatureDetector blobDetector;
blobDetector = FeatureDetector.create(FeatureDetector.SIFT);
MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
blobDetector.detect(orig,keypoints1);
org.opencv.core.Scalar cores = new org.opencv.core.Scalar(0,0,255);
org.opencv.features2d.Features2d.drawKeypoints(orig,keypoints1,MatOut,cores,2);
Highgui.imwrite("PhotoOut.jpg", MatOut);
}
public static void main(String args[]) {
BlobDetection bd = new BlobDetection();
bd.detectBlob();
}
When I tried the same code using FeatureDetector.SIMPLEBLOB instead of FeatureDetector.SIFT it shows almost 0 blobs.
The output and source images are attached for the above code. Source Image
Output Image using SIFT
Is there any other algorithm which can help in achieving the result or what can be the appropriate approach to achieve this?
As you did not ask anything in your question I won't give you an answer. More some general advice.
That you tried to solve that problem using the named algorithms clearly shows that you have absolutely no clue what you are doing. You lack the very basics of image processing.
It's like trying to win vs decent chess player if you don't even know how the figures can move.
I highly recommend that you get yourself a beginners book, read it and make sure you understand its contents. Then do some more research on the algorithms you want to use, befor you use them.
You cannot take some arbitrary image, feed it into some random feature detection algorithm you find on the internet and expect to succeed.
Hough transform for cirles for example is good for finding circle shaped contours of a roughly known radius. If you know how it works internally you will know why it is not a good idea to use it on your image.
https://en.wikipedia.org/wiki/Circle_Hough_Transform
Blobdetection and contour based algorithms might work, but only after a lot of pre-processing. Your image is not very "segmentation-friendly"
https://en.wikipedia.org/wiki/Image_segmentation
https://en.wikipedia.org/wiki/Blob_detection
A SIFT detector usually has to be taught using reference images and reference keypoints. I don't see this either in your code.
https://en.wikipedia.org/wiki/Scale-invariant_feature_transform
Please note that reading those wikipedia articles will only give you a first idea of what's going on. You have to read a lot more.
Always start at the beginning of your processing chain. Can you get better images? (Better means more suitable for what you want to detect). This is like 10% camera and 90% illumination. I don't think detecting skin pores is a classical task for shitty cellphone pictures so why not put a bit effort into your imaging setup?
First rule of image processing: crap in = crap out. You should at least change the angle of illumination or even better approach like shape from shading.
An image optimized for the detection you have to do is cruicial. It will make image processing so much easier.
Then pre-processing: How can you transform the image you have into something you can easily extract features from?
And so on...

Object matching in OpenCV 3.1 for Java

I found out that there is Chamfer Matching available in OpenCV. I can see there is a function chamferMatching() for C++ and there seems to be a way to use it in Python, too. However, I was unable to find how to use this feature in Java. I am using OpenCV 3.1 Where can I find it in the Java interface? Is it even available there?
If not, what can I use instead? I am trying to recognize fruits. For now apples in particular. I want to match precomputed apple contour to found contours in the image (chamfer matching). After finding a possible apple I am planning to use a classifier to make sure the color and texture are correct.
Template matching seems to be a bad choice because it doesn't work with recognizing rotated objects. I am wondering if I can use Feature Descriptors. Note that I am not trying to recognize a particular apple and I don't know if Feature Descriptors are good for this.
Any thoughts?
EDIT: Ok, I decided to use the findContours() function to get all of the contours in the image, then filter them by area and compare each of the filtered contours with others, designated as templates from training, using matchShapes(). I implemented this and it is not working right (because findContours() is not detecting the apple contours) but I'll post another question with this specific problem. Here I want to ask if this way sounds ok and if there is a better way to detect and compare contours.
Ok, I figured it out. There seems to be no Chamfer Matching in OpenCV. It is implemented in JavaCV and there is no sign of it in the native code. Since I'm using OpenCV for Java it is not a good solution for me.
This answer helped me a lot. It is in C++ but it can easily be written in Java.
Initially, I am training the program using a database of 100 images of green apples. The training is actually just storing the largest contour of every photo in a file.
The key to my problem was dividing the image into the 3 different channels resulting in 3 different grayscale images. I transform them using Canny and dilate. Now I check every one of them for contours and it is very likely I will detect the contours of the apple in at least one of them. Once I have all the contours from the 3 images, I filter them by size and then comparing them with every single contour from the training data. If the contour is close enough to one of them I assume it is a contour of an apple.
There seems to be quite a lot of false positives but they will be filtered out when my coleague implements the module doing checks for color and texture of the selected contours (their content).
Here's our project's repository if it would be of help to anyone.

Java image library - turn grid image into array

If I have an image of a table of boxes, with some coloured in, is there an image processing library that can help me turn this into an array?
Thanks
You can use a thresholding function to binarize the image into dark/light pixels so dark pixels are 0 and light ones are 1.
Then you would want to remove image artifacts using dilation and erosion functions to remove noise (all these are well defined on Wikipedia).
Finally if you know where the boxes are, you can just get the value in the center of each box to determine the array value, or possibly use an area near the center and take the prevailing value (i.e. more 0's is a filled in square, more 1's is and empty square).
If you are scanning these boxes and there is a lot of variation in the position of the boxes, you will have to perform some level of image registration using known points, or fiducials.
As far as what tools to use to do this, I'd recommend first trying this manually using a tool like ImageJ, which has a UI and can also be used programatically since it is written all in Java.
Other good libraries for this include OpenCV and the Java Advanced Imaging API.
Your results will definitely vary depending on the input images and how consistenly lit and positioned they are.
The best way to see how it will do for your data is to try applying these processing steps manually to see where your threshold value should be, how much dilating/eroding you need to get consistent results.

Object detection with a generic webcam

Here’s my task which I want to solve with as little effort as possible (preferrably with QT & C++ or Java): I want to use webcam video input to detect if there’s a (or more) crate(s) in front of the camera lens or not. The scene can change from "clear" to "there is a crate in front of the lens" and back while the cam feeds its video signal to my application. For prototype testing/ learning I have 2-3 images of the “empty” scene, and 2-3 images with one or more crates.
Do you know straightforward idea how to tackle this task? I found OpenCV, but isn't this framework too bulky for this simple task? I'm new to the field of computer vision. Is this generally a hard task or is it simple and robust to detect if there's an obstacle in front of the cam in live feeds? Your expert opinion is deeply appreciated!
Here's an approach I've heard of, which may yield some success:
Perform edge detection on your image to translate it into a black and white image, whereby edges are shown as black pixels.
Now create a histogram to record the frequency of black pixels in each vertical column of pixels in the image. The theory here is that a high frequency value in the histogram in or around one bucket is indicative of a vertical edge, which could be the edge of a crate.
You could also consider a second histogram to measure pixels on each row of the image.
Obviously this is a fairly simple approach and is highly dependent on "simple" input; i.e. plain boxes with "hard" edges against a blank background (preferable a background that contrasts heavily with the box).
You dont need a full-blown computer-vision library to detect if there is a crate or no crate in front of the camera. You can just take a snapshot and make a color-histogram (simple). To capture the snapshot take a look here:
http://msdn.microsoft.com/en-us/library/dd742882%28VS.85%29.aspx
Lots of variables here including any possible changes in ambient lighting and any other activity in the field of view. Look at implementing a Canny edge detector (which OpenCV has and also Intel Performance Primitives have as well) to look for the outline of the shape of interest. If you then kinda know where the box will be, you can perhaps sum pixels in the region of interest. If the box can appear anywhere in the field of view, this is more challenging.
This is not something you should start in Java. When I had this kind of problems I would start with Matlab (OpenCV library) or something similar, see if the solution would work there and then port it to Java.
To answer your question I did something similar by XOR-ing the 'reference' image (no crate in your case) with the current image then either work on the histogram (clustered pixels at right means large difference) or just sum the visible pixels and compare them with a threshold. XOR is not really precise but it is fast.
My point is, it took me 2hrs to install Scilab and the toolkits and write a proof of concept. It would have taken me two days in Java and if the first solution didn't work each additional algorithm (already done in Mat-/Scilab) another few hours. IMHO you are approaching the problem from the wrong angle.
If really Java/C++ are just some simple tools that don't matter then drop them and use Scilab or some other Matlab clone - prototyping and fine tuning would be much faster.
There are 2 parts involved in object detection. One is feature extraction, the other is similarity calculation. Some obvious features of the crate are geometry, edge, texture, etc...
So you can find some algorithms to extract these features from your crate image. Then comparing these features with your training sample images.

Auto scale and rotate images

Given:
two images of the same subject matter;
the images have the same resolution, colour depth, and file format;
the images differ in size and rotation; and
two lists of (x, y) co-ordinates that correlate the images.
I would like to know:
How do you transform the larger image so that it visually aligns to the second image?
(Optional.) What are the minimum number of points needed to get an accurate transformation?
(Optional.) How far apart do the points need to be to get an accurate transformation?
The transformation would need to rotate, scale, and possibly shear the larger image. Essentially, I want to create (or find) a program that does the following:
Input two images (e.g., TIFFs).
Click several anchor points on the small image.
Click the several corresponding anchor points on the large image.
Transform the large image such that it maps to the small image by aligning the anchor points.
This would help align pictures of the same stellar object. (For example, a hand-drawn picture from 1855 mapped to a photograph taken by Hubble in 2000.)
Many thanks in advance for any algorithms (preferably Java or similar pseudo-code), ideas or links to related open-source software packages.
This is called Image Registration.
Mathworks discusses this, Matlab has this ability, and more information is in the Elastix Manual.
Consider:
Open source Matlab equivalents
IRTK
IRAF
Hugin
you can use the javax.imageio or Java Advanced Imaging api's for rotating, shearing and scaling the images once you found out what you want to do with them.
For a C++ implementation (without GUI), try the old KLT (Kanade-Lucas-Tomasi) tracker.
http://www.ces.clemson.edu/~stb/klt/

Categories

Resources