扫描图片并检测线条

时间:2013-02-25 03:30:54

标签: c++ opencv feature-detection

我的作业如下:“使用cvFilter2D函数和合适的内核来扫描图片,然后只保留+ - 45度和+ - 60度的线条。”

有人能给我一些线索,特别是如何计算内核吗?

2 个答案:

答案 0 :(得分:1)

你需要一点预分解。 我假设您要创建一个行内核。所以你需要知道如何创建一条线。

http://courses.engr.illinois.edu/ece390/archive/archive-f2000/mp/mp4/anti.html拥有大量技术。

最后,对内核中的所有像素求和,并将它们标准化,使它们相加为1。

答案 1 :(得分:0)

抱歉我迟到了!我完成了这项任务!首先,再次感谢你,Perfanoff!感谢您的参考链接,我找到了解决问题的方法! 这是我的代码:

// Image Transforms.cpp : Defines the entry point for the console application.

/*The purpose of this program is to detect lines which are +-45 degree and +- 60 degree from a binary picture.*/
 #include "stdafx.h"
 #include "cv.h"
 #include "highgui.h" 

int _tmain(int argc, _TCHAR* argv[])
{
//IplImage* src = cvLoadImage("C:\\Users\\USER\\Desktop\\black white 1.jpg");
IplImage* src = cvLoadImage("C:\\Users\\USER\\Desktop\\line detection 4.png");

cvNamedWindow("src", CV_WINDOW_NORMAL);
cvShowImage("src", src);    

IplImage* DstSum = cvCreateImage(cvGetSize(src),src->depth, 3);
IplImage* Dst45 = cvCreateImage(cvGetSize(src),src->depth, 3);
IplImage* Dst135 = cvCreateImage(cvGetSize(src),src->depth, 3);
IplImage* Dst60 = cvCreateImage(cvGetSize(src),src->depth, 3);
IplImage* Dst120 = cvCreateImage(cvGetSize(src),src->depth, 3);

/*double Ker0 [] = { -0.1,-0.1,-0.1,-0.1,-0.1,
                0, 0, 0, 0, 0, 0,
                0.2,0.2,0.2,0.2,0.2,
                0, 0, 0, 0, 0, 0,
                -0.1,-0.1,-0.1,-0.1,-0.1
                };
double Ker90 [] = {-0.1,0,0.2,0,-0.1,
                -0.1,0,0.2,0,-0.1,
                -0.1,0,0.2,0,-0.1,
                -0.1,0,0.2,0,-0.1,
                -0.1,0,0.2,0,-0.1
                }; */

double Ker45[]={
                 0,-0.1,-0.1, 0, 0.2,
                -0.1,-0.1, 0, 0.2, 0,
                -0.1, 0, 0.2, 0,-0.1,
                 0, 0.2, 0,-0.1,-0.1,
                0.2, 0,-0.1,-0.1, 0
                };// 45 degree 

CvMat Kernel45=cvMat(5, 5, CV_64FC1,Ker45);

double Ker135[]={
        0.2, 0,-0.1,-0.1, 0,
        0, 0.2, 0,-0.1,-0.1,
        -0.1, 0, 0.2, 0,-0.1,
        -0.1,-0.1, 0, 0.2, 0,
        0,-0.1,-0.1, 0, 0.2
        };// 135 degree 

CvMat Kernel135=cvMat(5, 5, CV_64FC1,Ker135);

double Ker120[] = {0,0,0,0,0,0,0,
                  1/7,0.25/7,0,0,0,0,0,
                  0,0.75/7,0.75/7,0.25/7,0,0,0,
                  0,0,0,0.75/7,0.6/7,0.25/7,0,
                  0,0,0,0,0.4/7,0.75/7,1/7,
                  0,0,0,0,0,0,0,
                  0,0,0,0,0,0,0
                };//120 degree
CvMat Kernel120=cvMat(7, 7, CV_64FC1,Ker120);

double Ker60[] = {0,0,0,0,1/7,0,0,
                  0,0,0,0.25/7,0.75/7,0,0,
                  0,0,0,0.6/7,0.4/7,0,0,
                  0,0,0.25/7,0.75/7,0,0,0,
                  0,0,0.75/7,0.25/7,0,0,0,
                  0,0.25/7,0.75/7,0,0,0,0,
                  0,1/7,0,0,0,0,0
                };//60 degree

CvMat Kernel60=cvMat(7, 7, CV_64FC1,Ker60);

cvFilter2D(src,Dst60,&Kernel60,cvPoint(-1,-1));
cvThreshold(Dst60,Dst60,100,255,CV_THRESH_BINARY);

cvFilter2D(src,Dst120,&Kernel120,cvPoint(-1,-1));
cvThreshold(Dst120,Dst120,100,255,CV_THRESH_BINARY);

cvFilter2D(src,Dst45,&Kernel45,cvPoint(-1,-1));
cvThreshold(Dst45,Dst45,200,255,CV_THRESH_BINARY);

cvFilter2D(src,Dst135,&Kernel135,cvPoint(-1,-1));
cvThreshold(Dst135,Dst135,200,255,CV_THRESH_BINARY);

cvAdd(Dst45,Dst60,DstSum,NULL);
cvAdd(Dst135,DstSum,DstSum,NULL);
cvAdd(Dst120,DstSum,DstSum,NULL);

cvNamedWindow("dst", CV_WINDOW_NORMAL);
cvShowImage("dst", DstSum);

cvReleaseImage(&DstSum);

cvWaitKey(0);

cvReleaseImage(&src);

cvDestroyWindow("src");
cvDestroyWindow("dst"); 

return 0;

}

结果如下: enter image description here