5.4 형태학 필터 로 가장자리 와 각 점 을 측정 한다.

주요 참고:http://blog.csdn.net/thefutureisour/article/details/7574819
검 측 가장자리 에 대해 morphologyEx 함수 로 하면 됩 니 다. (cvMorphologyEx 에서 형태 조작 유형: CV MOP OPEN - 연산 CV MOP CLOSE - 폐 연산 CV MOP GRADIENT - 형태 경사도 CV MOP TOPHAT - & quot; 캡 & quot; CV MOP BLACKHAT - & quot; 검 은 모자 & quot; ) 매개 변수 CV MOP GRADIENT - 형태 경사도 는 크게 이미지 의 변 화 를 감지 하 는 직선 입 니 다. 각 점 에 특정 함수 가 없 는 것 을 검사 하 는 것 은 이미지 에 서로 다른 구조 요소 의 팽창 부식 을 응용 한 다음 에 차이 점 을 만 드 는 것 이다.
//morphoFeatures.h
#if ! defined MORPHOF
#define MORPHOF

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;

class MorphoFeatures
{
private:
	//         
	int threshold;
	//   
	Mat cross;
	Mat diamond;
	Mat square;
	Mat x;
	//      
	void applyThreshold(Mat &result)
	{
		if(threshold>0)
			cv::threshold(result,result,threshold,255,THRESH_BINARY_INV);//,THRESH_BINARY_INV);
	}


public:
	//    
	MorphoFeatures():threshold(-1),
		cross(5,5,CV_8U,Scalar(0)),
		diamond(5,5,CV_8U,Scalar(255)),
		square(5,5,CV_8U,Scalar(255)),
		x(5,5,CV_8U,Scalar(0))
	{
		//        
		for(int i = 0; i < 5; i++)
		{
			cross.at<uchar>(2,i) = 255;
			cross.at<uchar>(i,2) = 255;
		}

		//       :   ,               
		diamond.at<uchar>(0,0)= 0;
		diamond.at<uchar>(0,1)= 0;
		diamond.at<uchar>(1,0)= 0;
		diamond.at<uchar>(4,4)= 0;
		diamond.at<uchar>(3,4)= 0;
		diamond.at<uchar>(4,3)= 0;
		diamond.at<uchar>(4,0)= 0;
		diamond.at<uchar>(4,1)= 0;
		diamond.at<uchar>(3,0)= 0;
		diamond.at<uchar>(0,4)= 0;
		diamond.at<uchar>(0,3)= 0;
		diamond.at<uchar>(1,4)= 0;

		//  X 
		for(int i = 0; i < 5; i++)
		{
			//    
			x.at<uchar>(i,i) = 255;
			//    
			x.at<uchar>(4-i,i) = 255;
		}

	}

	//      
	void setThreshold(int t)
	{
		threshold = t;
	}

	//      
	int getThreshold() const
	{
		return threshold;
	}

	//      
	Mat getEdges(const Mat &image)
	{
		Mat result;
		//       
		morphologyEx(image,result,cv::MORPH_GRADIENT,Mat());
		//imshow("    ",result);
		//     
		applyThreshold(result);
		return result;
	}

	//      
	Mat getCorners(const Mat &image)
	{
		Mat result;
		dilate(image,result,cross);
		erode(result,result,diamond);
		Mat result2;
		dilate(image,result2,x);
		erode(result2,result2,square);
		absdiff(result2,result,result);
		applyThreshold(result);
		return result;
	}

	//        morpho.drawOnImage(corners,image);
	void drawOnImage(const Mat &binary,Mat &image)
	{
		Mat_<uchar>::const_iterator it = binary.begin<uchar>();
		Mat_<uchar>::const_iterator itend = binary.end<uchar>();
		for(int i = 0;it != itend;++it,++i)
		{
			if(!*it)  //      
				circle(image,Point(i%image.step,i/image.step),5,Scalar(255,0,0));  //         
		}
	}

};



#endif
다음은 주 함수 입 니 다.
#include <opencv2/highgui/highgui.hpp>
#include "morphoFeatures.h"

int main()
{

	Mat image = imread("C:\\Users\\Administrator\\Desktop\\  \\testp\\building.jpg",0);
	if(!image.data)
		return -1;
	imshow("   ",image);

	MorphoFeatures morpho;
	morpho.setThreshold(40);

	//    
	Mat edges;
	edges = morpho.getEdges(image);
	imshow("  ",edges);

	//    
	morpho.setThreshold(-1);
	Mat corners;
	corners = morpho.getCorners(image);
//	imshow("corners",corners);
	morphologyEx(corners,corners,MORPH_TOPHAT,Mat()); //;  
	//imshow("corners",corners);
	threshold(corners,corners,40,255,THRESH_BINARY_INV);
	//imshow("  ",corners);
	//imshow("corners",corners);
	//        
	morpho.drawOnImage(corners,image);
	imshow("      ",image);


	waitKey(0);
	return 0;
}

좋은 웹페이지 즐겨찾기