Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用OpenCV检测组件的主方向_Opencv_Image Processing_Contour - Fatal编程技术网

使用OpenCV检测组件的主方向

使用OpenCV检测组件的主方向,opencv,image-processing,contour,Opencv,Image Processing,Contour,我有一张像图像A的图像,我想检测图像B中显示的主方向。在我的代码中,我首先检测边缘,然后尝试找到轮廓。然后,我计划利用力矩,通过“angle=180*atan((2*m11)/(m20-m02))/(PI*2.0);”得到角度,但结果是错误的 // My function Mat edge_Processing(Mat Input_image,int Para_canny_1,int Para_canny_2) { Mat edge; //blur gray blur(Input_image,

我有一张像图像A的图像,我想检测图像B中显示的主方向。在我的代码中,我首先检测边缘,然后尝试找到轮廓。然后,我计划利用力矩,通过“angle=180*atan((2*m11)/(m20-m02))/(PI*2.0);”得到角度,但结果是错误的

// My function
Mat edge_Processing(Mat Input_image,int Para_canny_1,int Para_canny_2)
{
Mat edge;

//blur gray
blur(Input_image, Input_image, Size(3,3));

// Canny edge
Canny(Input_image,edge, Para_canny_1,Para_canny_2 , 3);

//imshow("Before contour",edge);

std::vector<std::vector<cv::Point>> contours;
cv::findContours(edge, 
    contours, // a vector of contours 
    CV_RETR_EXTERNAL, // retrieve the ezxternal contours
    CV_CHAIN_APPROX_NONE); // all pixels of each contours

// Draw black contours on a white image
cv::Mat result(edge.size(),CV_8U,cv::Scalar(0));


std::vector<std::vector<cv::Point>>::
    const_iterator itc= contours.begin();

//Get the moments
vector<Moments> mu(contours.size() );
int moment_count=0;
while (itc!=contours.end())
{
    mu[moment_count] = moments(contours[moment_count], false );

double m11=mu[moment_count].m11;
double m20=mu[moment_count].m20;
double m02=mu[moment_count].m02;

double angle=90;
    // In case of the situation of dividing by 0        
if (abs(m20 - m02)>0.001)
{
angle = 180*atan((2*m11)/(m20 - m02))/(PI*2.0);
}

cout<<angle<<endl; // Here! The angle are wrong!


++itc;
moment_count++;

}
    // Draw the contour
cv::drawContours(result,contours,
    -1, // draw all contours
    cv::Scalar(255), // in black
    1); // with a thickness of 1

//imshow("contour after filtering",result);

return result;
};

//我的函数
Mat边缘处理(Mat输入图像,int Para_canny_1,int Para_canny_2)
{
垫边;
//模糊灰色
模糊(输入图像,输入图像,大小(3,3));
//精明的边缘
Canny(输入图像、边缘、Para_Canny_1、Para_Canny_2、3);
//imshow(“轮廓前”,边缘);
矢量轮廓;
cv::findContours(边缘、,
等高线,//等高线的向量
CV_RETR_EXTERNAL,//检索EZexternal轮廓
CV_CHAIN_APPROX_NONE);//每个轮廓的所有像素
//在白色图像上绘制黑色轮廓
cv::Mat结果(edge.size(),cv_8U,cv::Scalar(0));
向量::
常量迭代器itc=等高线。开始();
//抓住机会
向量μ(等高线.size());
int矩_计数=0;
while(itc!=contours.end())
{
mu[力矩计数]=力矩(等高线[力矩计数],false);
双m11=mu[力矩计数].m11;
双m20=mu[力矩计数].m20;
double m02=mu[力矩计数].m02;
双角度=90;
//在除以0的情况下
如果(abs(m20-m02)>0.001)
{
角度=180*atan((2*m11)/(m20-m02))/(PI*2.0);
}

cout根据您想要执行的操作,您也可以使用
RotatedRect

//you got your contour
RotatedRect minRect = minAreaRect(contour);
minRect.angle //angle from 0-90
查看,看看如何从中计算出正确的角度

这可能仅在您希望使用RotatedRect并且您的轮廓接近矩形时才可行