Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3 | PointedEar | 1 | /*------------------------------------------------------------------------------------------*\ |
2 | This file contains material supporting chapter 5 of the cookbook: |
||
3 | Computer Vision Programming using the OpenCV Library. |
||
4 | by Robert Laganiere, Packt Publishing, 2011. |
||
5 | |||
6 | This program is free software; permission is hereby granted to use, copy, modify, |
||
7 | and distribute this source code, or portions thereof, for any purpose, without fee, |
||
8 | subject to the restriction that the copyright notice may not be removed |
||
9 | or altered from any source or altered source distribution. |
||
10 | The software is released on an as-is basis and without any warranties of any kind. |
||
11 | In particular, the software is not guaranteed to be fault-tolerant or free from failure. |
||
12 | The author disclaims all warranties with regard to this software, any use, |
||
13 | and any consequent failure, is purely the responsibility of the user. |
||
14 | |||
15 | Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name |
||
16 | \*------------------------------------------------------------------------------------------*/ |
||
17 | |||
18 | #include <opencv2/core/core.hpp> |
||
19 | #include <opencv2/imgproc/imgproc.hpp> |
||
20 | #include <opencv2/highgui/highgui.hpp> |
||
21 | #include "morphoFeatures.h" |
||
22 | |||
23 | int main() |
||
24 | { |
||
25 | // Read input image |
||
26 | cv::Mat image= cv::imread("../building.jpg",0); |
||
27 | if (!image.data) |
||
28 | return 0; |
||
29 | |||
30 | // Display the image |
||
31 | cv::namedWindow("Image"); |
||
32 | cv::imshow("Image",image); |
||
33 | |||
34 | // Create the morphological features instance |
||
35 | MorphoFeatures morpho; |
||
36 | morpho.setThreshold(40); |
||
37 | |||
38 | // Get the edges |
||
39 | cv::Mat edges; |
||
40 | edges= morpho.getEdges(image); |
||
41 | |||
42 | // Display the edge image |
||
43 | cv::namedWindow("Edge Image"); |
||
44 | cv::imshow("Edge Image",edges); |
||
45 | |||
46 | // Get the corners |
||
47 | morpho.setThreshold(-1); |
||
48 | cv::Mat corners; |
||
49 | corners= morpho.getCorners(image); |
||
50 | cv::morphologyEx(corners,corners,cv::MORPH_TOPHAT,cv::Mat()); |
||
51 | cv::threshold(corners, corners, 40, 255, cv::THRESH_BINARY_INV); |
||
52 | |||
53 | // Display the corner image |
||
54 | cv::namedWindow("Corner Image"); |
||
55 | cv::imshow("Corner Image",corners); |
||
56 | |||
57 | // Display the corner on the image |
||
58 | morpho.drawOnImage(corners,image); |
||
59 | cv::namedWindow("Corners on Image"); |
||
60 | cv::imshow("Corners on Image",image); |
||
61 | |||
62 | // Read and display image of square |
||
63 | image= cv::imread("../square.bmp",0); |
||
64 | cv::namedWindow("Square Image"); |
||
65 | cv::imshow("Square Image",image); |
||
66 | |||
67 | // Creating the cross-shaped structuring element |
||
68 | cv::Mat cross(5,5,CV_8U,cv::Scalar(0)); |
||
69 | for (int i=0; i<5; i++) { |
||
70 | |||
71 | cross.at<uchar>(2,i)= 1; |
||
72 | cross.at<uchar>(i,2)= 1; |
||
73 | } |
||
74 | |||
75 | // Dilate with a cross |
||
76 | cv::Mat result; |
||
77 | cv::dilate(image,result,cross); |
||
78 | |||
79 | // Display the result |
||
80 | cv::namedWindow("Dilated square with cross"); |
||
81 | cv::imshow("Dilated square with cross",result); |
||
82 | |||
83 | // Creating the diamond-shaped structuring element |
||
84 | cv::Mat diamond(5,5,CV_8U,cv::Scalar(1)); |
||
85 | diamond.at<uchar>(0,0)= 0; |
||
86 | diamond.at<uchar>(0,1)= 0; |
||
87 | diamond.at<uchar>(1,0)= 0; |
||
88 | diamond.at<uchar>(4,4)= 0; |
||
89 | diamond.at<uchar>(3,4)= 0; |
||
90 | diamond.at<uchar>(4,3)= 0; |
||
91 | diamond.at<uchar>(4,0)= 0; |
||
92 | diamond.at<uchar>(4,1)= 0; |
||
93 | diamond.at<uchar>(3,0)= 0; |
||
94 | diamond.at<uchar>(0,4)= 0; |
||
95 | diamond.at<uchar>(0,3)= 0; |
||
96 | diamond.at<uchar>(1,4)= 0; |
||
97 | |||
98 | // Erode with a diamond |
||
99 | cv::Mat result2; |
||
100 | cv::erode(result,result2,diamond); |
||
101 | |||
102 | // Display the result |
||
103 | cv::namedWindow("Eroded square with diamond"); |
||
104 | cv::imshow("Eroded square with diamond",result2); |
||
105 | |||
106 | // Combine the images into one |
||
107 | cv::Mat final(100,300,CV_8U); |
||
108 | cv::Mat window= final(cv::Rect(0,0,100,100)); |
||
109 | image.copyTo(window); |
||
110 | window= final(cv::Rect(100,0,100,100)); |
||
111 | result.copyTo(window); |
||
112 | window= final(cv::Rect(200,0,100,100)); |
||
113 | result2.copyTo(window); |
||
114 | |||
115 | // Display the combined result |
||
116 | cv::namedWindow("Combined"); |
||
117 | cv::imshow("Combined",final); |
||
118 | |||
119 | // Save combined result |
||
120 | cv::imwrite("squares.bmp",final); |
||
121 | |||
122 | cv::waitKey(); |
||
123 | |||
124 | return 0; |
||
125 | } |