我的任务如下:“使用具有合适内核的 cvFilter2D 函数来扫描图片,然后只保留 +- 45 度和 +- 60 度的线”。
有人可以给我一些线索,特别是如何计算内核?
我的任务如下:“使用具有合适内核的 cvFilter2D 函数来扫描图片,然后只保留 +- 45 度和 +- 60 度的线”。
有人可以给我一些线索,特别是如何计算内核?
你需要一点微积分。我假设您想创建一个线路内核。所以你需要知道如何创建一条线。
http://courses.engr.illinois.edu/ece390/archive/archive-f2000/mp/mp4/anti.html包含大量技术。
最后,对内核中的所有像素求和,并将它们归一化,使它们加起来为 1。
很抱歉,我答复晚了 !我已经完成了这个任务!首先,再次感谢您,佩凡诺夫!感谢您的参考链接,我找到了解决问题的方法!这是我的代码:
// 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;
}
结果如下: