How should one GpuMat in OpenCV - c++

In OpenCV to create an image Mat I'd usually do something along the lines of: cv::Mat myImage however when I use this method cv::gpu::GpuMat myImage to create a GpuMat I get undefined reference errors. I have noticed that a lot of people simply declare GpuMat myImage however using namespace cv::gpu leads to the same errors.
In short, how do I create a GpuMat in OpenCV properly?
Note: I'm still learning C/C++, so it's likely I'm missing something obvious (or not accessing the method correctly).

The problem occurred as I had not included the opencv_gpu231 lib, thanks for the help!

Related

OpenCV C++ using cv::UMat instead of cv:Mat and pointers

I want to change some code in OpenCV to use cv::UMat instead of cv::Mat to get extra speed with OpenCL/GPU when working with images.
But what I used to access image data directly by pointer doesn't work anymore, e.g:
cv::Vec3b* imageP = image.ptr<cv::Vec3b>(y)
where image is a cv::Mat and y is the image line offset, because the Ptr() method doesn't exist for UMat.
What is the correct syntax with a UMat? Is it even possible ?
Thank you.
I searched a lot and I understand there is no simple solution, but copying back and forth between Mat and UMat is soooo slow :(

function cv::findChessboardCorners corners cannot be std::vector<cv::Point2f>

I have a question, when I run findChessboardCorners in the Qt-MinGW platform, I can get the correct board_feature_locations_xy results, but when I run the same code on the VS2015 platform, I get the wrong results, but if I change the definition std::vector<cv::Point2f> board_feature_locations_xy; to
cv::Mat board_feature_locations_xy;
I can get the correct results.
So anyone can solve this problem?
I use opencv-3.1.0.
std::vector<cv::Point2f> board_feature_locations_xy;
cv::Size board_feature_size(8, 6);
cv::findChessboardCorners(calibration_image_cv, board_feature_size, board_feature_locations_xy, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);
I had similar problem and it got fixed when I built Opencv locally on my system instead of using pre-built libraries. You can refer to this thread for building Opencv on your system Where is the lib folder (or its replacement) in the current OpenCV?
Also I changed the camera calibration code to work for cv::Mat point buffer type instead of vector.If you are doing camera calibration and you encountered this problem then only change is you use imagePoints1.push_back(board_feature_loactions_xy) for the corner that were detected for a chessboard .

UIImage to cv::Mat CV_8UC3

There are a ton of questions about converting a UIIMage to a cv::Mat with CV_8UC4 encoding. There's even the tutorial on the OpenCV iOS site.
However, for the life of me I can't figure out how to convert a UIImage to 8UC3 correctly. Using the stock OpenCV example, trying to convert to 8UC1 also breaks, as the cv:Mat has a ton of null pointers after initialization.
Any insight into how I might be able to do this correctly? Thanks!
For me when I want to convert from UIImage into Mat simply I use the predefined function
UIImageToMat like this:
UIImageToMat(extractedUIImage, matImage);
But first you need to include this header file from openCV:
#import <opencv2/highgui/ios.h>
Same to convert from Mat into UIImage:
UIImage *extractedImage = MatToUIImage(matImage);
Just give it a try it works fine for me
Try calling the following after converting UIImage to cv::Mat...
cvtColor(srcMat, destMat, CV_RGBA2BGR);

Capture image frames from Kinect and save to Hard drive

My aim is to capture all the frames (RGB) from Kinect at 30 fps and save them to my hard drive. For doing this I took the following approach.
Get the frames from Kinect and store them in an array buffer. Since writing to disk (using imwrite()) takes a bit of time and I may miss some frames while doing so, so instead of directly saving them to the disk, I store them in an array. Now, I have another parallel thread that accesses this array and writes the individual frames to the disk as images.
Now I have used a static array of size 3000 and type Mat. This will suffice since I need to store frames for 1.5 minute videos (1.5 minutes = 2700 frames). I have declared the array as follows :
#define NUM_FRAMES 3000
Mat rgb[NUM_FRAMES];
I have already tested this limit by reading images and saving them to the array using the following code :
for(int i=0; i<NUM_FRAMES; i++)
{
Mat img = imread("image.jpg", CV_LOAD_IMAGE_COLOR);
rgb[i] = img;
imshow("Image", img);
cvWaitKey(10);
}
The above code executed flawlessly.
But one problem is that the code I am using for capturing image using Kinect, captures the image in an IplImage. Thus I need to convert the image to cv::Mat format before using it. I convert it using the following command:
IplImage* color = cvCreateImageHeader(cvSize(COLOR_WIDTH, COLOR_HEIGHT), IPL_DEPTH_8U, 4);
cvSetData(color, colorBuffer, colorLockedRect.Pitch); // colorBuffer and colorLockedRect.Pitch is something that Kinect uses. Not related to OpenCv
rgb[rgb_read++] = Mat(color, FLAG);
Now here lies my problem. Whenever I am setting #define FLAG true, it causes memory leaks and gives me OpenCv Error: Insufficient memory (failed to allocate 1228804 bytes) error.
But if I use #define FLAG false it works correctly, but the frames that I am getting is erroneous as shown below. They are three consecutive frames.
I was moving around my arm and the image got cut in between as can be seen from above.
Can someone please point out the reason for this weird behavior or any other alternate way of obtaining the desired result. I have been struggling with this since a few days now. Please ask for if any further clarifications are required.
I am using OpenCV 2.4.8, Kinect SDK for Windows version-1.8.0 and Microsoft Visual Studio 2010.
Also can someone please explan to me the role of the CopyData parameter in Mat::Mat. I have already gone through this link, but still it's not completely clear. Maybe that's why I could not solve the above error in the first place since it's working is not very clear.
Thanks in advance.
first, do not use IplImages, stick with cv::Mat, please.
the equivalent code for that would be:
Mat img_borrowed = Mat( height, width, CV_8U4C, colorBuffer, colorLockedRect.Pitch );
note, that this does not do any allocation on its own, it's still the kinect's pixels, so you will have to clone() it:
rgb[rgb_read++] = img_borrowed.clone();
this is the same as setting the flag in your code above to 'true'. (deep-copy the data)
[edit] maybe it's a good idea to skip the useless 4th channel (also less mem required), so , instead of the above you could do:
cvtColor( img_borrowed, rgb[rgb_read++], CV_BGRA2BGR); // will make a 'deep copy', too.
so, - here's the bummer: if you don't save a deep-copy in your array, you'll end up with garbled (and all the same!) images, probably even with undefined behaviour due to the locking/unlocking of the kinect buffer, if you do copy it (and you must), you will need a lot of memory.
unlikely, that you can keep 3000 *1024*786*4 = 9658368000 bytes in memory, you'll have to cut it down one way or another.

Detect object stored in Mat image opencv

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.