get OpenCV Mat variables of rgb and depth information from pcl openni grabber - opencv

I'm now using pcl openni grabber to get point cloud from kinect cameras. But I also want to get OpenCV Mat variables for the rgb and depth information.
Does anyone know how to achieve this?
Thanks a lot!

I just found pcl has its own openni2 wrapper to get the color and depth image directly. We can write a callback function like:
void PclProcessor::image_cb1_ (const boost::shared_ptr<pcl::io::Image>& rgb1, const boost::shared_ptr<pcl::io::DepthImage>& depth1, float reciprocalFocalLength)
{
if (refreshK1)
{
C1 = Mat(rgb1->getHeight(), rgb1->getWidth(), CV_8UC3);
rgb1->fillRGB(C1.cols,C1.rows,C1.data,C1.step);
cvtColor(C1, C1, CV_RGB2BGR);
D1 = Mat(depth1->getHeight(), depth1->getWidth(), CV_32F);
depth1->fillDepthImage(D1.cols, D1.rows,(float *)D1.data,D1.step);
refreshK1 = false;
}
imshow("camera 1 color",C1);
imshow("camera 1 depth",D1);
cv::waitKey(0);
}
In this case, I can get the color image right. However, the depth image does not look right.

Related

How to use convexHull function of openCV in iOS objective C?

I am someone who is new to openCV and has been trying to use convexhull function in the openCV library for an app (objective-C being used), I need to know what is the input format of function arguments, it's pretty confusing. And does this function return the points in a sequence? Like, if I use addLineToPoint to draw a bezierpath of this hull, is it possible?
Some sample code for you:
std::vector<cv::Point> points;
//fill that vector with your points
std::vector<cv::Point> hull;
if (points.size()) {
cv::convexHull(points, hull);
}
cv::Size size = cv::Size(w, h);
//some size for the matrix where you will draw your convex hull
cv::Mat hullMask = Mat::zeros(size, CV_8UC1);
int hull_count = (int)hull.size();
if (hull_count) {
const cv::Point* hull_pts = &hull[0];
cv::fillPoly(hullMask, &hull_pts, &hull_count, 1, cv::Scalar(255));
}
This code will help you to create convex hull and draw it.
Here you can find complete documentation for that function. It will return points in a sequence according to the "clockwise" argument. By default it will be counter-clockwise.

How to get similarties and differences between two images using Opencv

I want to compare two images and find same and different parts of images. I tired "cv::compare and cv::absdiff" methods but confused which one can good for my case. Both show me different results. So how i can achieve my desired task ?
Here's an example how you can use cv::absdiff to find image similarities:
int main()
{
cv::Mat input1 = cv::imread("../inputData/Similar1.png");
cv::Mat input2 = cv::imread("../inputData/Similar2.png");
cv::Mat diff;
cv::absdiff(input1, input2, diff);
cv::Mat diff1Channel;
// WARNING: this will weight channels differently! - instead you might want some different metric here. e.g. (R+B+G)/3 or MAX(R,G,B)
cv::cvtColor(diff, diff1Channel, CV_BGR2GRAY);
float threshold = 30; // pixel may differ only up to "threshold" to count as being "similar"
cv::Mat mask = diff1Channel < threshold;
cv::imshow("similar in both images" , mask);
// use similar regions in new image: Use black as background
cv::Mat similarRegions(input1.size(), input1.type(), cv::Scalar::all(0));
// copy masked area
input1.copyTo(similarRegions, mask);
cv::imshow("input1", input1);
cv::imshow("input2", input2);
cv::imshow("similar regions", similarRegions);
cv::imwrite("../outputData/Similar_result.png", similarRegions);
cv::waitKey(0);
return 0;
}
Using those 2 inputs:
You'll observe that output (black background):

Real time circle detection using OpenCV

I have written the following program to detect a circle in real time. But it doesn't work.
The compiler doesn't show any error but the problem is that the program doesn't even detect a circle. How can I fix it?
here is my code
using namespace cv;
int main()
{
VideoCapture cap(0);
namedWindow("main",CV_WINDOW_AUTOSIZE);
namedWindow("blur",CV_WINDOW_AUTOSIZE);
Mat img;
Mat img2;
int c;
float radius;
while(1)
{
cap>>img;
imshow("main",img);
cvtColor(img,img2,CV_BGR2GRAY);
GaussianBlur(img2,img2,Size(9,9),2,2);
imshow("blur",img2);
vector <Vec3f> circles;
HoughCircles(img2,circles,CV_HOUGH_GRADIENT,1,img2.rows/8,200,100,0,0);
for(size_t i=0;i<circles.size();i++)
{
Point center(cvRound(circles[i][0]),cvRound(circles[i][1]));
radius = cvRound(circles[i][2]);
circle(img,center,3,Scalar(0,255,0),-1,8,0);
circle(img,center,radius,Scalar(0,0,255),3,8,0);
}
c = waitKey(33);
if(c==27)
break;
}
destroyAllWindows();
return 0;
}
I checked your program, it seems you just forgot to visualize it using imshow() after the detection. You only drew the image before the detection, in this way, you were not able to see the circles (maybe this mistakenly make you think there is no circles detected) even it did detect some circles.
Try to add
imshow("main", img);
right before c = waitKey(33);.
You will see the circles if it does detect some circles.
Edit: to answer your comment for real time circle detection:
Do it in a while loop style will make it work for video frames. However, whether it is real time or not depends on how fast HoughCircles() will work and also other stuff inside the loop despite you setup the proper time for waitKey().

OpenCV 2.4.3 and videoInput into Mat

I am trying to capture video into a Mat type from two or more MSFT LifeCam HD-3000s using the videoInput library, OpenCV 2.4.3, and VS2010 Express.
I followed the example at: Most efficient way to capture and send images from a webcam in a network and it worked great.
Now I want to replace the IplImage type with a c++ Mat type. I tried to follow the example at: opencv create mat from camera data
That gave me the following:
VI = new videoInput;
int CurrentCam = 0;
VI->setupDevice(CurrentCam,WIDTH,HEIGHT);
int width = VI->getWidth(CurrentCam);
int height = VI->getHeight(CurrentCam);
unsigned char* yourBuffer = new unsigned char[VI->getSize(CurrentCam)];
cvNamedWindow("test",1);
while(1)
{
VI->getPixels(CurrentCam, yourBuffer, false, true);
cv::Mat image(width, height, CV_8UC3, yourBuffer, Mat::AUTO_STEP);
imshow("test", image);
if(cvWaitKey(15)==27) break;
}
The output is a lined image (i.e., it looks like the first line is correct but the second line seems off, third correct, fourth off, etc). That suggests that either the step part is wrong or there is some difference between the IplImage type and the Mat type that I am not getting. I have tried looking at/altering all the parameters, but I can't find anything.
Hopefully, an answer will help those facing what appears to be a fairly common issue with loading an image form the videoInput library to the Mat type.
Thanks in advance!
Try
cv::Mat image(height, width, CV_8UC3, yourBuffer, Mat::AUTO_STEP);

OpenCV C++/Obj-C: goodFeaturesToTrack inside specific blob

Is there a quick solution to specify the ROI only within the contours of the blob I'm intereseted in?
My ideas so far:
Using the boundingRect, but it contains too much stuff I don't want to analyse.
Applying goodFeaturesToTrack to the whole image and then loop through the output coordinates to eliminate the once outside my blobs contour
Thanks in advance!
EDIT
I found what I need: cv::pointPolygonTest() seems to be the right thing, but I'm not sure how to implement it …
Here's some code:
// ...
IplImage forground_ipl = result;
IplImage *labelImg = cvCreateImage(forground.size(), IPL_DEPTH_LABEL, 1);
CvBlobs blobs;
bool found = cvb::cvLabel(&forground_ipl, labelImg, blobs);
IplImage *imgOut = cvCreateImage(cvGetSize(&forground_ipl), IPL_DEPTH_8U, 3);
if (found) {
vb::CvBlob *greaterBlob = blobs[cvb::cvGreaterBlob(blobs)];
cvb::cvRenderBlob(labelImg, greaterBlob, &forground_ipl, imgOut);
CvContourPolygon *polygon = cvConvertChainCodesToPolygon(&greaterBlob->contour);
}
"polygon" contains the contour I need.
goodFeaturesToTrack is implemented this way:
- (std::vector<cv::Point2f>)pointsFromGoodFeaturesToTrack:(cv::Mat &)_image
{
std::vector<cv::Point2f> corners;
cv::goodFeaturesToTrack(_image,corners, 100, 0.01, 10);
return corners;
}
So next I need to loop through the corners and check each point with cv::pointPolygonTest(), right?
You can create a mask over your interest region:
EDIT
How to make a mask:
Make a mask;
Mat mask(origImg.size(), CV_8UC1);
mask.setTo(Scalar::all(0));
// here I assume your contour is extracted with findContours,
// and is stored in a vector<vector<Point>>
// and that you know which contour is the blob
// if it's not the case, use fillPoly instead of drawContour();
Scalar color(255,255,255); // white. actually, it's monchannel.
drawContours(mask, contours, contourIdx, color );
// fillPoly(Mat& img, const Point** pts, const int* npts,
// int ncontours, const Scalar& color)
And now you're ready to use it. BUT, look carefully at the result - I have heard about some bugs in OpenCV regarding the mask parameter for feature extractors, and I am not sure if it's about this one.
// note the mask parameter:
void goodFeaturesToTrack(InputArray image, OutputArray corners, int maxCorners,
double qualityLevel, double minDistance,
InputArray mask=noArray(), int blockSize=3,
bool useHarrisDetector=false, double k=0.04 )
This will also improve the speed of your aplication - goodFeaturesToTrack eats a hoge amount of time, and if you apply it only on a smaller image, the overall gain is significant.

Resources