因为pcl的点云模板匹配遇到了各种困难,暂时先用opencv的模板匹配函数做一个简单的焊缝识别,看看效果。此方法的缺陷就在于物体和相机位置必须固定,只允许微小位移,否则数据将失效。

1

什么是模板匹配?

模板匹配是一种用于查找与模板图像(补丁)匹配(类似)的图像区域的技术。

虽然补丁必须是一个矩形,可能并不是所有的矩形都是相关的。在这种情况下,可以使用掩模来隔离应该用于找到匹配的补丁部分。

它是如何工作的?

我们需要两个主要组件:

源图像(I):我们期望找到与模板图像匹配的图像

模板图像(T):将与模板图像进行比较的补丁图像

f7f57d45a160

源图像--->模板图像--->匹配结果

要识别匹配区域,我们必须通过滑动来比较模板图像与源图像

通过滑动,我们的意思是一次移动补丁一个像素(从左到右,从上到下)。在每个位置,计算度量,以便它表示在该位置处的匹配的“好”还是“坏”(或者与图像的特定区域相似)。

2

OpenCV在函数matchTemplate()中实现模板匹配。可用的方法有以下6种(我采取的上第二种):

f7f57d45a160

f7f57d45a160

3

f7f57d45a160

源图像(工件)

f7f57d45a160

模板图像(焊缝)

f7f57d45a160

匹配结果

f7f57d45a160

焊缝锁定

f7f57d45a160

端点空间坐标输出

然后通过匹配得到焊缝位置,使用getPointXYZ将像素坐标(u,v)转化为相机坐标(x,y,z)。从而得到焊缝的空间坐标,后续将xyz传给机械臂,为手眼标定做准备。

4

关键代码

其中因为matchTemplate的输入图像要求是三通道,而我得到的rgbd为四通道,需要使用cvtColor将四通道改为三通道(cv::cvtColor(rgbd, rgbd, COLOR_BGRA2BGR);)很关键,当时纠结了好久。

Mat rgbmat, depthmat, irmat, rgbd, model, result;

cv::namedWindow("registered", WND_PROP_ASPECT_RATIO);

//cv::namedWindow("result", WND_PROP_ASPECT_RATIO);

model = imread("model.png");

float x1, y1, z1, x2, y2, z2;

Point p1, p2;

while(!protonect_shutdown)

{

listener.waitForNewFrame(frames);

libfreenect2::Frame *rgb = frames[libfreenect2::Frame::Color];

libfreenect2::Frame *ir = frames[libfreenect2::Frame::Ir];

libfreenect2::Frame *depth = frames[libfreenect2::Frame::Depth];

cv::Mat(rgb->height, rgb->width, CV_8UC4, rgb->data).copyTo(rgbmat);

cv::Mat(ir->height, ir->width, CV_32FC1, ir->data).copyTo(irmat);

cv::Mat(depth->height, depth->width, CV_32FC1, depth->data).copyTo(depthmat);

registration->apply(rgb, depth, &undistorted, &registered, true, &depth2rgb);

cv::Mat(registered.height, registered.width, CV_8UC4, registered.data).copyTo(rgbd);

cv::cvtColor(rgbd, rgbd, COLOR_BGRA2BGR);

int result_cols = rgbd.cols - model.cols + 1;

int result_rows = rgbd.rows - model.rows + 1;

result.create(result_cols, result_rows, CV_32FC1);

matchTemplate(rgbd, model, result, CV_TM_SQDIFF_NORMED);//这里我们使用的匹配算法是标准平方差匹配 method=CV_TM_SQDIFF_NORMED,数值越小匹配度越好

normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat());

double minVal = -1;

double maxVal;

Point minLoc;

Point maxLoc;

Point matchLoc;

//cout << "匹配度:" << minVal << endl;

minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, Mat());

//cout << "匹配度:" << minVal << endl;

matchLoc = minLoc;

p1 = Point(matchLoc.x+5, matchLoc.y+8);

p2 = Point(matchLoc.x-4 + model.cols, matchLoc.y+13);

//rectangle(rgbd, matchLoc, Point(matchLoc.x + model.cols, matchLoc.y + model.rows), Scalar(0, 255, 0), 2, 8, 0);

registration->getPointXYZ(&undistorted, p1.y, p1.x, x1, y1, z1 );

registration->getPointXYZ(&undistorted, p2.y, p2.x, x2, y2, z2 );

circle(rgbd, p1, 1, Scalar(255,255,0), 2);

circle(rgbd, p2, 1, Scalar(255,255,0), 2);

line(rgbd, p1, p2, Scalar(0,255,0));

cv::imshow("registered", rgbd);

//cv::imshow("result", result);

//cout << matchLoc.x <

cout <

cout <

int key = cv::waitKey(1);

protonect_shutdown = protonect_shutdown || (key > 0 && ((key & 0xFF) == 27)); // shutdown on escape

listener.release(frames);

}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐