1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <iostream> using namespace cv; int main() { cv::Mat srcImage = cv::imread("..\\images\\sea.jpg"); if (!srcImage.data) return 1; cv::Mat srcGray; cv::cvtColor(srcImage, srcGray, CV_RGB2GRAY); Mat CrossMat(5, 5, CV_8U, Scalar(0)); Mat diamondMat(5, 5, CV_8U, Scalar(1)); Mat squareMat(5, 5, CV_8U, Scalar(1)); Mat x(5, 5, CV_8U, Scalar(0)); for (int i = 0; i<5; i++) { CrossMat.at<uchar>(2, i) = 1; CrossMat.at<uchar>(i, 2) = 1; } diamondMat.at<uchar>(0, 0) = 0; diamondMat.at<uchar>(0, 1) = 0; diamondMat.at<uchar>(1, 0) = 0; diamondMat.at<uchar>(4, 4) = 0; diamondMat.at<uchar>(3, 4) = 0; diamondMat.at<uchar>(4, 3) = 0; diamondMat.at<uchar>(4, 0) = 0; diamondMat.at<uchar>(4, 1) = 0; diamondMat.at<uchar>(3, 0) = 0; diamondMat.at<uchar>(0, 4) = 0; diamondMat.at<uchar>(0, 3) = 0; diamondMat.at<uchar>(1, 4) = 0; for (int i = 0; i<5; i++){ x.at<uchar>(i, i) = 1; x.at<uchar>(4 - i, i) = 1; } Mat result; dilate(srcGray, result, CrossMat); erode(result, result, diamondMat); Mat result2; dilate(srcGray, result2, x); erode(result2, result2, squareMat); absdiff(result2, result, result); threshold(result, result, 40, 255, THRESH_BINARY); for (int i = 0; i < result.rows; i++) { const uchar* data = result.ptr<uchar>(i); for (int j = 0; j < result.cols; j++) { if (data[j]) circle(srcImage, Point(j, i), 8, Scalar(0, 255, 0)); } } cv::imshow("result", result); cv::imshow("srcImage", srcImage); cv::waitKey(0); return 0; }
|