检测对象中的行Opencv [英] Detect Lines Opencv in object

查看:79
本文介绍了检测对象中的行Opencv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的图片.我想检测将该对象分为两部分的线.最好的方法是什么?我已经尝试过霍夫变换,但有时对象不够大,无法检测到.有任何想法吗?

I have the image below. I want to detect the line that divides this object in two pieces. Which is the best way? I've tried with the Hough transform but sometimes the object is not big enough for it to detect. Any ideias?

谢谢!

推荐答案

通常,霍夫变换用于行检测.

Normally, Hough Transform is used for line detection.

但是,如果对您不起作用,试穿线也是一个不错的选择.

But if it doesn't work for you, fitting line is also a good alternative.

检查OpenCV fitline 函数以获取更多详细信息和参数.

Check OpenCV fitline function for more details and parameters.

由于您已经尝试过hough线,因此我将在此处使用OpenCV-Python演示拟合线:

Since you have already tried hough lines, I will demonstrate fitting line here, using OpenCV-Python :

# Load image, convert to grayscale, threshold and find contours
img = cv2.imread('lail.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
contours,hier = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]

# then apply fitline() function
[vx,vy,x,y] = cv2.fitLine(cnt,cv2.cv.CV_DIST_L2,0,0.01,0.01)

# Now find two extreme points on the line to draw line
lefty = int((-x*vy/vx) + y)
righty = int(((gray.shape[1]-x)*vy/vx)+y)

#Finally draw the line
cv2.line(img,(gray.shape[1]-1,righty),(0,lefty),255,2)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

以下是我得到的结果:

如果要找到将对象分为两部分的线,请首先找到拟合线,然后找到与其垂直的线.

If you want to find the line to divide the object into two pieces, first find the fitting line, then find the line normal to it.

为此,请在cv2.fitLine()函数下添加以下代码:

For that, add below piece of code under cv2.fitLine() function :

nx,ny = 1,-vx/vy
mag = np.sqrt((1+ny**2))
vx,vy = nx/mag,ny/mag

下面是我得到的结果:

希望有帮助!

更新:

下面是您所要求的第一种情况的Python的C ++代码.该代码对我来说很好用.输出与上面给出的相同:

Below is the C++ code for Python code of first case as you requested. The code works fine for me. Output is same as given above :

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv/cv.h>

using namespace std;
using namespace cv;

int main()
{
    cv::Mat img, gray,thresh;
    vector<vector<Point>> contours;
    Vec4f lines;

    img = cv::imread("line.png");
    cv::cvtColor(img,gray,cv::COLOR_BGR2GRAY);
    cv::threshold(gray,thresh,127,255,cv::THRESH_BINARY);
    cv::findContours(thresh,contours,cv::RETR_LIST,cv::CHAIN_APPROX_SIMPLE);
    cv::fitLine(Mat(contours[0]),lines,2,0,0.01,0.01);

    //lefty = int((-x*vy/vx) + y)
    //righty = int(((gray.shape[1]-x)*vy/vx)+y)
    int lefty = (-lines[2]*lines[1]/lines[0])+lines[3];
    int righty = ((gray.cols-lines[2])*lines[1]/lines[0])+lines[3];

    cv::line(img,Point(gray.cols-1,righty),Point(0,lefty),Scalar(255,0,0),2);

    cv::imshow("img",img);
    cv::waitKey(0);
    cv::destroyAllWindows();
}

这篇关于检测对象中的行Opencv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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