C++ OpenCV测线

C++ OpenCV测线,c++,opencv,image-processing,C++,Opencv,Image Processing,我正在尝试查找此图像中居中框的边缘: 我尝试过使用一个HoughLines,使用dRho=img_width/1000、dTheta=pi/180和threshold=250 它在这张图片上效果很好,缩放到大小的1/3,但在全尺寸图片上,它只是在每个方向上到处都有线条 我可以做些什么来调整它以使其更准确?尝试使用腐蚀过滤器的预处理过程。它会给你和缩小尺寸一样的效果——线条会变细,不会同时消失 正如chaiy所说,“模糊”过滤器也是一个好主意 这种方式(使用模糊)将类似于(基于内核的Hough变

我正在尝试查找此图像中居中框的边缘:

我尝试过使用一个HoughLines,使用dRho=img_width/1000、dTheta=pi/180和threshold=250 它在这张图片上效果很好,缩放到大小的1/3,但在全尺寸图片上,它只是在每个方向上到处都有线条


我可以做些什么来调整它以使其更准确?

尝试使用腐蚀过滤器的预处理过程。它会给你和缩小尺寸一样的效果——线条会变细,不会同时消失

正如chaiy所说,“模糊”过滤器也是一个好主意


这种方式(使用模糊)将类似于(基于内核的Hough变换)

调整图像大小时,通常首先使用过滤器(例如高斯)对图像进行模糊,以去除高频。调整大小后的图像效果更好,这可能是因为您的原始图像有一定的噪声


首先尝试模糊图像,例如使用
cv::GaussianBlur(src,target,Size(0,0),1.5)
,然后它应该相当于调整大小。(它忘记了理论,如果它不起作用,请尝试3和6)

实现以下结果的代码是对此答案中给出的代码的轻微修改:

原始程序可以在OpenCV中找到,它被称为squares.cpp。下面的代码被修改为只在第一个颜色平面中搜索方块,但由于它仍然检测到许多方块,在程序结束时,我放弃了除第一个方块以外的所有方块,然后调用
draw_squares()
,以显示检测到的方块。您可以很容易地更改此选项,以绘制所有对象并查看检测到的所有对象

您可以从现在开始自己做各种事情,包括设置(ROI)感兴趣区域以提取正方形内部的区域(忽略周围的所有其他内容)

您可以看到,检测到的矩形与图像中的线条没有完全对齐。您应该在图像中执行一些预处理(腐蚀?)操作,以减少线条的厚度并提高检测效果。但从现在起,一切都取决于你:

#include <cv.h>
#include <highgui.h>

using namespace cv;

double angle( cv::Point pt1, cv::Point pt2, cv::Point pt0 ) {
    double dx1 = pt1.x - pt0.x;
    double dy1 = pt1.y - pt0.y;
    double dx2 = pt2.x - pt0.x;
    double dy2 = pt2.y - pt0.y;
    return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}

void find_squares(Mat& image, vector<vector<Point> >& squares)
{
    // TODO: pre-processing

    // blur will enhance edge detection
    Mat blurred(image);
    medianBlur(image, blurred, 9);

    Mat gray0(blurred.size(), CV_8U), gray;
    vector<vector<Point> > contours;

    // find squares in the first color plane.
    for (int c = 0; c < 1; c++)
    {
        int ch[] = {c, 0};
        mixChannels(&blurred, 1, &gray0, 1, ch, 1);

        // try several threshold levels
        const int threshold_level = 2;
        for (int l = 0; l < threshold_level; l++)
        {
            // Use Canny instead of zero threshold level!
            // Canny helps to catch squares with gradient shading
            if (l == 0)
            {
                Canny(gray0, gray, 10, 20, 3); // 

                // Dilate helps to remove potential holes between edge segments
                dilate(gray, gray, Mat(), Point(-1,-1));
            }
            else
            {
                    gray = gray0 >= (l+1) * 255 / threshold_level;
            }

            // Find contours and store them in a list
            findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

            // Test contours
            vector<Point> approx;
            for (size_t i = 0; i < contours.size(); i++)
            {
                    // approximate contour with accuracy proportional
                    // to the contour perimeter
                    approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);

                    // Note: absolute value of an area is used because
                    // area may be positive or negative - in accordance with the
                    // contour orientation
                    if (approx.size() == 4 &&
                            fabs(contourArea(Mat(approx))) > 1000 &&
                            isContourConvex(Mat(approx)))
                    {
                            double maxCosine = 0;

                            for (int j = 2; j < 5; j++)
                            {
                                    double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                                    maxCosine = MAX(maxCosine, cosine);
                            }

                            if (maxCosine < 0.3)
                                    squares.push_back(approx);
                    }
            }
        }
    }
}

void draw_squares(Mat& img, vector<vector<Point> > squares)
{
    for (int i = 0; i < squares.size(); i++)
    {
        for (int j = 0; j < squares[i].size(); j++)
        {
            cv::line(img, squares[i][j], squares[i][(j+1) % 4], cv::Scalar(0, 255, 0), 1, CV_AA);
        }
    }
}


int main(int argc, char* argv[])
{
    Mat img = imread(argv[1]);

    vector<vector<Point> > squares;
    find_squares(img, squares);

    std::cout << "* " << squares.size() << " squares were found." << std::endl;

    // Ignore all the detected squares and draw just the first found
    vector<vector<Point> > tmp;
    if (squares.size() > 0)
    {
        tmp.push_back(squares[0]);
        draw_squares(img, tmp);
    }
    //imshow("squares", img);
    //cvWaitKey(0);

    imwrite("out.png", img);

    return 0;
}
#包括
#包括
使用名称空间cv;
双角度(cv::点pt1、cv::点pt2、cv::点pt0){
双dx1=pt1.x-pt0.x;
双dy1=pt1.y-pt0.y;
双dx2=pt2.x-pt0.x;
双dy2=pt2.y-pt0.y;
返回(dx1*dx2+dy1*dy2)/sqrt(dx1*dx1+dy1*dy1)*(dx2*dx2+dy2*dy2)+1e-10);
}
无效查找方格(材质和图像、向量和方格)
{
//TODO:预处理
//模糊将增强边缘检测
Mat模糊(图像);
中间模糊(图像,模糊,9);
Mat灰色0(模糊的大小(),CV_8U),灰色;
矢量等值线;
//在第一个颜色平面中查找正方形。
对于(int c=0;c<1;c++)
{
int ch[]={c,0};
混音通道(&模糊,1,&灰色,0,1,通道,1);
//尝试几个阈值级别
const int threshold_level=2;
对于(int l=0;l=(l+1)*255/阈值\u级;
}
//找到等高线并将其存储在列表中
findContours(灰色、等高线、等高线列表、等高线链近似简单);
//测试轮廓
向量近似;
对于(size_t i=0;i1000&&
isContourConvex(材料(近似)))
{
双最大余弦=0;
对于(int j=2;j<5;j++)
{
双余弦=fabs(角度(约[j%4],约[j-2],约[j-1]);
最大余弦=最大值(最大余弦,余弦);
}
如果(最大余弦<0.3)
正方形。推回(大约);
}
}
}
}
}
空绘方格(矩阵和图像、矢量方格)
{
对于(int i=0;istd::你能告诉我如何获取我们确定的正方形的长度吗?问问你自己正方形的长度是多少,然后注意到一个正方形被定义为一组4个点(坐标)。上面的代码给出了点,但是显示其长度的计算由你自己来写。