在轮廓OPENCV中绘制最长的线 [英] Draw Longest Line in Contours OPENCV

查看:147
本文介绍了在轮廓OPENCV中绘制最长的线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenCV和Python.我正在尝试在轮廓内绘制最长的线.

I am using OpenCV and Python. I am trying to draw the longest line inside a contours.

我有一个名为cnt的轮廓.图像为二进制,轮廓的内部为白色,外部为黑色.我想在白色轮廓内画最长的线.我找到了如何使用cv2.lines绘制线条,但没有找到绘制最长的线条的方法.你有什么想法?

I have a contour named cnt. The image is binary, the inside of the contours is white and the outside is black. I would like to draw the longest line inside the white contours. I found how to draw lines using cv2.lines but I didn't find how to draw the longest one. Do you have any ideas?

img_copy = cv2.dilate(copy.deepcopy(img), np.ones((2,2),np.uint8),iterations = 2)
contours, hierarchy = cv2.findContours(copy.deepcopy(img_copy),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
areas = [cv2.contourArea(c) for c in contours]
max_index = np.argmax(areas)
cnt = contours[max_index]

推荐答案

以下方法用于从图像中绘制行数并获得最大值的程度,请尝试此操作.工作正常

The below method using to draw the number of number of lines from the image and get the degree of max value try this. its working fine

 Mat Compute_skewAngle (Mat& src,Mat& src_gray,int drawLine) {

int thresh = 100;
RNG rng(12345);


// 1. Load Gray Scae Image
// 2. Get Size of Image
cv::Size size = src_gray.size();
// 3. blur the Grayscale image
cv::blur(src_gray, src_gray, cv::Size(3,3) );



cv::Mat threshold_output;
std::vector<std::vector<cv::Point> > contours;
std::vector<Vec4i> hierarchy;


// 4. Detect edges using Threshold / Canny edge Detector
//cv::threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );
Mat dst, cdst;
cv::Canny(src_gray, dst, thresh, 200, 3);

// 5. Gray Image to BGR
cvtColor(dst, cdst, CV_GRAY2BGR);


 #if 0
vector<Vec2f> lines;
HoughLines(dst, lines, 1, CV_PI/180, 100, 0, 0 );

for( size_t i = 0; i < lines.size(); i++ )
{
    float rho = lines[i][0], theta = lines[i][1];
    Point pt1, pt2;
    double a = cos(theta), b = sin(theta);
    double x0 = a*rho, y0 = b*rho;
    pt1.x = cvRound(x0 + 1000*(-b));
    pt1.y = cvRound(y0 + 1000*(a));
    pt2.x = cvRound(x0 - 1000*(-b));
    pt2.y = cvRound(y0 - 1000*(a));
    line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA);
}
#else
vector<Vec4i> lines;

double angle = 0.;
int countNegative = 0;
int countPositive =0;

HoughLinesP(dst, lines, 1, CV_PI/180, 100, 10, 100);

NSMutableDictionary *angleCountDict = [[NSMutableDictionary alloc] init];

for( size_t i = 0; i < lines.size(); i++ )
{
    if(drawLine == 1) {  // draw line while pass flag value 1
        Vec4i l = lines[i];
        line( cdst, cv::Point(l[0], l[1]), cv::Point(l[2], l[3]), Scalar(0,0,255), 3, CV_AA);
    }


    double delta_y = lines[i][3] - lines[i][1];
    double delta_x = lines[i][2] - lines[i][0];
    double currentAngle =atan2(delta_y,delta_x);
    int angleAsDeg = abs(currentAngle * 180 / CV_PI);


    NSString *_retValue = [angleCountDict objectForKey:[NSString stringWithFormat:@"%d", angleAsDeg]];
    int angleCount = [_retValue intValue];
    [angleCountDict setObject:[NSNumber numberWithInt:angleCount + 1] forKey:[NSString stringWithFormat:@"%d", angleAsDeg]];

    double slope  =  delta_y / delta_x ;  // find the slope to detect the angle " - " or " + "
    if(slope < 0)
        countNegative ++;
    else
        countPositive ++;
}

#endif

// sort the dictionary to get the largest value of degree count
NSArray *blockSortedKeys = [angleCountDict keysSortedByValueUsingComparator: ^(id obj1, id obj2) {
    return [obj2 compare:obj1];
}];


NSString *degreeVal;
if([blockSortedKeys count] > 0)
    degreeVal = [blockSortedKeys objectAtIndex:0];


angle = [degreeVal doubleValue];

if(countNegative > countPositive) {
    angle = - angle;
}
Mat outPut;
outPut = rotateMatImage(src,angle,cdst);

return outPut;

}

这篇关于在轮廓OPENCV中绘制最长的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆