一起学opencv(七) 边缘和角点检测

头文件
#if !defined MORPHOF
#define MORPHOF

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

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);//将灰度值大于threhold的赋值为255,否则为0
	}

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 第3行和第3列全为1
		for (int i = 0; i<5; i++) {

			cross.at<uchar>(2, i) = 1;
			cross.at<uchar>(i, 2) = 1;
		}

		// Creating the diamond-shaped structuring element
		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;

		// Creating the x-shaped structuring element
		for (int i = 0; i<5; i++) {

			x.at<uchar>(i, i) = 1;
			x.at<uchar>(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);//非Corner像素值变为0,

		// Apply threshold to obtain a binary image
		applyThreshold(result);

		return result;
	}

	void drawOnImage(const cv::Mat& binary, cv::Mat& image) {

		cv::Mat_<uchar>::const_iterator it = binary.begin<uchar>();
		cv::Mat_<uchar>::const_iterator itend = binary.end<uchar>();

		// for each pixel	每个corner画一个半径为5的圆
		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 <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "morphoFeatures.h"

int main()
{
	// Read input image
	cv::Mat image = cv::imread("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);
	cv::imwrite("Edage Image.jpg", edges);

//	 Get the corners
	morpho.setThreshold(-1);//applyThreshold函数中,像素不处理
	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;
}


图片结果



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值