5.4 형태학 필터 로 가장자리 와 각 점 을 측정 한다.
검 측 가장자리 에 대해 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;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Linux Shell 프로 그래 밍 - 텍스트 처리 grep, sed사용자 가 지정 한 '모드' 에 따라 대상 텍스트 를 일치 하 게 검사 하고 일치 하 는 줄 을 인쇄 합 니 다. ##포함 되 지 않 음, 역방향 일치 \ ##키워드 앞 뒤 가 맞지 않 고 키워드 만 일치 합 니 다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.