博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
opencv2使用形态学滤波对图像进行边缘及角点检測
阅读量:5980 次
发布时间:2019-06-20

本文共 3405 字,大约阅读时间需要 11 分钟。

#if !defined MORPHOF#define MORPHOF#include 
#include
class MorphoFeatures { private: // threshold to produce binary image int threshold; // structuring elements used in corner detection cv::Mat cross; cv::Mat diamond; cv::Mat square; cv::Mat x; void applyThreshold(cv::Mat& result) { // Apply threshold on result if (threshold>0) cv::threshold(result, result, threshold, 255, cv::THRESH_BINARY_INV); } public: MorphoFeatures() : threshold(-1), cross(5,5,CV_8U,cv::Scalar(0)), diamond(5,5,CV_8U,cv::Scalar(1)), square(5,5,CV_8U,cv::Scalar(1)), x(5,5,CV_8U,cv::Scalar(0)){ // Creating the cross-shaped structuring element for (int i=0; i<5; i++) { cross.at
(2,i)= 1; cross.at
(i,2)= 1; } // Creating the diamond-shaped structuring element diamond.at
(0,0)= 0; diamond.at
(0,1)= 0; diamond.at
(1,0)= 0; diamond.at
(4,4)= 0; diamond.at
(3,4)= 0; diamond.at
(4,3)= 0; diamond.at
(4,0)= 0; diamond.at
(4,1)= 0; diamond.at
(3,0)= 0; diamond.at
(0,4)= 0; diamond.at
(0,3)= 0; diamond.at
(1,4)= 0; // Creating the x-shaped structuring element for (int i=0; i<5; i++) { x.at
(i,i)= 1; x.at
(4-i,i)= 1; } } void setThreshold(int t) { threshold= t; } int getThreshold() const { return threshold; } cv::Mat getEdges(const cv::Mat &image) { // Get the gradient image cv::Mat result; cv::morphologyEx(image,result,cv::MORPH_GRADIENT,cv::Mat()); // Apply threshold to obtain a binary image applyThreshold(result); return result; } cv::Mat getCorners(const cv::Mat &image) { cv::Mat result; // Dilate with a cross cv::dilate(image,result,cross); // Erode with a diamond cv::erode(result,result,diamond); cv::Mat result2; // Dilate with a X cv::dilate(image,result2,x); // Erode with a square cv::erode(result2,result2,square); // Corners are obtained by differencing // the two closed images cv::absdiff(result2,result,result); // Apply threshold to obtain a binary image applyThreshold(result); return result; } void drawOnImage(const cv::Mat& binary, cv::Mat& image) { cv::Mat_
::const_iterator it= binary.begin
(); cv::Mat_
::const_iterator itend= binary.end
(); // for each pixel for (int i=0; it!= itend; ++it,++i) { if (!*it) cv::circle(image,cv::Point(i%image.step,i/image.step),5,cv::Scalar(255,0,0)); } }};#endif#include
#include
#include
#include "morphoFeatures.h"int main(){ // Read input image cv::Mat image= cv::imread("d:/test/opencv/building.jpg",0); if (!image.data) return 0; // Display the image cv::namedWindow("Image"); cv::imshow("Image",image); // Create the morphological features instance MorphoFeatures morpho; morpho.setThreshold(40); // Get the edges cv::Mat edges; edges= morpho.getEdges(image); // Display the edge image cv::namedWindow("Edge Image"); cv::imshow("Edge Image",edges); // Get the corners morpho.setThreshold(-1); cv::Mat corners; corners= morpho.getCorners(image); cv::morphologyEx(corners,corners,cv::MORPH_TOPHAT,cv::Mat()); cv::threshold(corners, corners, 40, 255, cv::THRESH_BINARY_INV); // Display the corner image cv::namedWindow("Corner Image"); cv::imshow("Corner Image",corners); // Display the corner on the image morpho.drawOnImage(corners,image); cv::namedWindow("Corners on Image"); cv::imshow("Corners on Image",image); cv::waitKey(); return 0;}

你可能感兴趣的文章
比较好的网站及工具
查看>>
Keychain
查看>>
Webview图片自适应
查看>>
使用 getopt() 进行命令行处理
查看>>
js去掉html标记,中正则表达式,去掉字符,截取字符
查看>>
使用Akka Http,ActiveMQ搭建一个邮件发送服务器
查看>>
kvm starting domain: cannot send monitor command
查看>>
Tomcat主配置文件Server.xml详解
查看>>
中考在即,您为孩子选择什么?--读<<招生全攻略>>有感
查看>>
深入剖析 HTML5
查看>>
基础知识笔记之正则表达式
查看>>
Mysql mysql.server启动脚本详解 .
查看>>
网格(GridView)+图片(ImageView)+文字(TextView)
查看>>
jquery遇上Ajax
查看>>
iptables
查看>>
我的友情链接
查看>>
RHEL-6.1/5.4安装Heartbeat-3-0-7有可能碰见的各种错误及解决方法
查看>>
win32控制台应用程序中使用CString类型的方法
查看>>
关于authlib集成windows ad失败的分析并解决[草稿]
查看>>
centos5.4 x86_64禁用的服务
查看>>