Detect object stored in Mat image opencv - c++

I'm trying to detect an object using opencv and Visual Studio Ultimate using C++. I'm having problems concerning cv::Mat, I cannot find any example of object detection with that kind of variable but just with IplImage. I tried to use an IplImage code and convert it to Mat, but it didn't work. But i don not want to use IplImage, my first part of code is in Mat and I want to keep using it.
What I'm trying to actually do is to detect the BIGGEST rectangle in the image stored from the cam, after thresholding it.
I have already done the threshold part and it's ok, it works and i can se my object (in white) moving in a black background.
Could someone help me with the tracking part? I have seen on the net some blob filtering solutions but they were way too difficult for me! If you can come up with an easy one it would be better.
thank you!

cv::Mat is the new image class in opencv. I think the most algorithms still use IplImage. For this reason I have asked times ago the following:
openCV mixing IplImage with cv::Mat
For recognition of objects I would say watch the cvMatchTemplate function of opencv. There is also the mat version cv::matchTemplate. There are also other object recognition methods but they are a bit more difficult to implement ;)
I dont know if I maybe understood your other question right but I think you wannt to recognze rectangle in your image. Maybe watch this tutorial:
http://docs.opencv.org/trunk/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html
I don t know any standard algorithm for rectangles maybe you will need to code it yourself

cv::Mat encapsulate the lower level IplImage and other formats. Regard detection, there is a sample that you could find useful: squares. I googled for it, and found also this other question, that's more recent and could be of interest to you.

Related

how to use openCV as a motion detector (not looking for video output)

I'm very new to c++ and have been doing a lot of research into openCV.
I'm currently making a motion detector using a webcam and trying to use it to trigger a function (kind of like a listener). I don't need any kind of visual video in my application. I just need to sense motion and give an output like a boolean for instance.
I am much more familiar with java but it has to be written in c++, no wrappers, no converters.
Can someone please help point me in the write direction. maybe with a tutorial or suggestion of something I should look up. Even a different library I should use rather than openCV.
As a starting point, I'd recommend using a basic image subtraction then take it from there.
The basic algorithm is:
firstFrame = readFrame()
firstFrameGrey = convertToGreyScale(firstFrame) // makes the image B&W
while True
secondFrame = readFrame()
secondFrameGrey = convertToGreyScale(secondFrame)
difference = sub(firstFrameGrey, secondFrameGrey)
threshold()
// perform some morphological operations
erosion()
dilation()
findContours()
// loop over contours and try filtering based off of area, perimeter etc.
// This filtration will allow you to detect significant changes.
// update frame
firstFrameGrey = secondFrameGrey
For the morphological operations, experiment with different values and see what results you get.
More info on thresholding
This tutorial explains how to perform the morphological operations.
Contours info can be found here along with the area API
The algorithm should be self explanatory and openCV has all the methods I named.

Is there a SURF_CUDA implementation for colored images?

Recently started playing around with OpenCV, trying that SURF algorithm, that is really slow on CPU, and does not work with color images on GPU (has an assertion that checks for type==CV_8UC1), and converting images to grayscale gives some pretty bad results.
I'm wondering if there is a colored implementation on gpu in OpenCV, somewhere else, or if there is some kinda tricky workaround like doing the algorithm on all 3 channels and then magically merging them?
Thanks.
There's no special handling of color images in OpenCV's non-GPU version of SURF; the code shows that it just calls cvtColor(img, img, COLOR_BGR2GRAY) if it gets an image with more than one channel.
You might try converting the image to HSV and using one or more of the H, S, and/or V channels. More discussion at this question.

Extract object from black background OpenCV C++

First you have to know that I work with OpenCV in C++ in Visual Studio.
I have a picture like : Original image
I want to create a new picture of the hand but with a lot less of black bacground.
So the final image should look like this : Final Image
I know there are some OpenCv functions that could help me but I have really trouble to implement the algorithm because OpenCv can't be used in Debug Mode so it hard to check what I am doing.
Have anyone any idea how to proceed ?
Thanks you very much.
Find contour, find bounding rectangle, crop.
Here is example of finding bounding box: example

FFT based image registration (optionally using OpenCV) in cpp?

I'm trying to align two images taken from a handheld camera.
At first, I was trying to use the OpenCV warpPerspective method based on SIFT/SURF feature points. The problem is the feature-extract & matching process may be extremely slow when the image quality is high (3000x4000). I tried to scale-down the image before find feature-points, the result is not as good as before.(The Mat generated from findHomography shouldn't be affected by scaling down the image, right?) And sometimes, due to lack of good feature point matches, the result is quite strange.
After searching on this topic, it seems that solving the problem in Fourier domain will speed up the registration process. And I've found this question which leads me to the code here.
The only problem is the code is written in python with numpy (not even using OpenCV), which makes it quite hard to re-written to C++ code using OpenCV (In OpenCV, I can only find dft and there's no fftshift nor fft stuff, I'm not quite familiar with NumPy, and I'm not brave enough to simply ignore the missing methods). So I'm wondering why there is not such a Fourier-domain image registration implementation using C++?
Can you guys give me some suggestion on how to implement one, or give me a link to the already implemented C++ version? Or help me to turn the python code into C++ code?
Big thanks!
I'm fairly certain that the FFT method can only recover a similarity transform, that is, only a (2d) rotation, translation and scale. Your results might not be that great using a handheld camera.
This is not quite a direct answer to your question, but, as a suggestion for a speed improvement, have you tried using a faster feature detector and descriptor? In OpenCV SIFT/SURF are some of the slowest methods they have for feature extraction/matching. You could try testing some of their other methods first, they all work quite well and are faster than SIFT/SURF. Especially if you use their FLANN-based matcher.
I've had to do this in the past with similar sized imagery, and using the binary descriptors OpenCV has increases the speed significantly.
If you need only shift you can use OpenCV's phasecorrelate

Object Identification using opencv in a given image

in my project i want to identify objects and then detect what they are(water bottle, ball, etc).
I thought of identifying the objects in the image and then match that object with a object database using SURF method.
But the problem is to identify the whether my image has a object or not and how many objects are there.
I did some search and find out about "contours", a way to track shapes. I want to know whether "contours" will help to solve my problem or any other way to solve this.
Thanks.
First, for identifying the objects you can also use BoW, cascade classifier or latent svm.
Once you have an object classifier, you can use the sliding window approach to search for the object in the image. Take a look at the cascade classifier for an example of the sliding window approach.
EDIT: here's a post blog I wrote about BoW theory and packages in Matlab and openCV
http://gilscvblog.wordpress.com/2013/08/23/bag-of-words-models-for-visual-categorization/