使用图像矩的物体方向的原理范围

时间:2015-07-01 20:53:20

标签: c++ opencv computer-vision

我试图在opencv / c ++中使用片刻提取图像中的形状角度。我能够提取角度,但问题是这个角度的主要范围是180度。这使得物体的取向相对于180度旋转是模糊的。我用来提取角度的代码是,

findContours(frame, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

vector<vector<Point2i> > hull(contours.size());

int maxArea = 0;
int maxI = -1;

int M20 = 0;
int M02 = 0;
int M11 = 0;

for (int i = 0; i < contours.size(); i++)
{
    convexHull(contours[i], hull[i], false);
    approxPolyDP(hull[i], contourVertices, arcLength(hull[i], true)*0.1, true);
    shapeMoments = moments(hull[i], false);

    if(shapeMoments.m00 <= areaThreshold || shapeMoments.m00 >= MAX_AREA)
        continue;

    if(contourVertices.size() <= 3 || contourVertices.size() >= 7)
        continue;

    if(shapeMoments.m00 >= maxArea)
    {
        maxArea = shapeMoments.m00;
        maxI = i;
    }
}

if(maxI == -1)
    return false;

fabricContour = hull[maxI];

approxPolyDP(hull[maxI], contourVertices, arcLength(hull[maxI], true)*0.02,true);
shapeMoments = moments(hull[maxI], false);
centerOfMass = Point2f(shapeMoments.m10/shapeMoments.m00, shapeMoments.m01/shapeMoments.m00);

drawContours(contourFrame, hull, maxI, Scalar(24, 35, 140), CV_FILLED, CV_AA);
drawContours(originalFrame, hull, maxI, Scalar(255, 0, 0), 8, CV_AA);
circle(contourFrame, centerOfMass, 4, Scalar(0, 0, 0), 10, 8, 0);

posX = centerOfMass.x;
posY = centerOfMass.y;
M11 = shapeMoments.mu11/shapeMoments.m00;
M20 = shapeMoments.mu20/shapeMoments.m00;
M02 = shapeMoments.mu02/shapeMoments.m00;
num = double(2)*M11;
den = M20 - M02;
angle = (int(-1*(180/(2*M_PI))*atan2(num, den)) + 45 + 180)%180;
//angle = int(-1*(180/(2*M_PI))*atan2(num, den));

area = shapeMoments.m00;

有什么办法可以消除这个提取角度的歧义吗?我尝试使用三阶矩,但它们似乎不太可靠。

0 个答案:

没有答案