目标:选取盖板图像上下固定位置的RotateRect区域,转正后分别与模板区域进行匹配,比较匹配效果来判断盖板是否倒放。
得到上方RotateRect的四个点后,生成RotateRect并旋正:
RotateRect rotateRectUp = minAreaRect(tempCornerPointsUp);
float angle = 0.0;
Size si = rotateRectUp.size;
if (rotateRectUp.size.width <= rotateRectUp.size.height)
{
angle = rotateRectUp.angle + 90;
swap(si.width, si.height);
}
else
{
angle = rotateRectUp.angle;
}
Mat rotmat = getRotationMatrix2D(rotateRectUp.center, angle, 1);
Mat dealImg;
warpAffine(srcImg, dealImg, rotmat, srcImg.size(), CV_INTER_CUBIC);
Mat upRectImg;
getRectSubPix(dealImg, si, rotateRectUp.center, upRectImg);
下方ROI生如上。
分别与模板进行ORB特征匹配:得到vector matches(代码略)
因为三个ROI的位置较为精确,特征点剔除没有采用用RANSAC的方法,直接比较每组匹配的keypoint的位置,偏差较小视为相同位置的正确匹配,若场景区域的匹配数量为零,说明物体倒置:
vector goodMatches;
for (size_t i = 0; i < matches.size(); i++)
{
Point2f pointL = keypointsUp[matcher[i].queryIdxs].pt;
Point2f pointR = keypointsModle[matcher[i].trainIdxs].pt;
if(abs(pointL.x - pointR.x)<5 && pointL.y - pointR.y)<5)
{
goodMatches.push_back(matches[i]);
}
}
最终得到正确的匹配集合。