Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3 | PointedEar | 1 | /*------------------------------------------------------------------------------------------*\ |
2 | This file contains material supporting chapter 6 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 <iostream> |
||
19 | #include <iomanip> |
||
20 | #include <opencv2/core/core.hpp> |
||
21 | #include <opencv2/imgproc/imgproc.hpp> |
||
22 | #include <opencv2/highgui/highgui.hpp> |
||
23 | #include "laplacianZC.h" |
||
24 | |||
25 | int main() |
||
26 | { |
||
27 | // Read input image |
||
28 | cv::Mat image= cv::imread("../boldt.jpg",0); |
||
29 | if (!image.data) |
||
30 | return 0; |
||
31 | |||
32 | // Display the image |
||
33 | cv::namedWindow("Original Image"); |
||
34 | cv::imshow("Original Image",image); |
||
35 | |||
36 | // Compute Sobel X derivative |
||
37 | cv::Mat sobelX; |
||
38 | cv::Sobel(image,sobelX,CV_8U,1,0,3,0.4,128); |
||
39 | |||
40 | // Display the image |
||
41 | cv::namedWindow("Sobel X Image"); |
||
42 | cv::imshow("Sobel X Image",sobelX); |
||
43 | |||
44 | // Compute Sobel Y derivative |
||
45 | cv::Mat sobelY; |
||
46 | cv::Sobel(image,sobelY,CV_8U,0,1,3,0.4,128); |
||
47 | |||
48 | // Display the image |
||
49 | cv::namedWindow("Sobel Y Image"); |
||
50 | cv::imshow("Sobel Y Image",sobelY); |
||
51 | |||
52 | // Compute norm of Sobel |
||
53 | cv::Sobel(image,sobelX,CV_16S,1,0); |
||
54 | cv::Sobel(image,sobelY,CV_16S,0,1); |
||
55 | cv::Mat sobel; |
||
56 | //compute the L1 norm |
||
57 | sobel= abs(sobelX)+abs(sobelY); |
||
58 | |||
59 | double sobmin, sobmax; |
||
60 | cv::minMaxLoc(sobel,&sobmin,&sobmax); |
||
61 | std::cout << "sobel value range: " << sobmin << " " << sobmax << std::endl; |
||
62 | |||
63 | // Print window pixel values |
||
64 | for (int i=0; i<12; i++) { |
||
65 | for (int j=0; j<12; j++) |
||
66 | std::cout << std::setw(5) << static_cast<int>(sobel.at<short>(i+135,j+362)) << " "; |
||
67 | std::cout << std::endl; |
||
68 | } |
||
69 | std::cout << std::endl; |
||
70 | std::cout << std::endl; |
||
71 | std::cout << std::endl; |
||
72 | |||
73 | // Conversion to 8-bit image |
||
74 | // sobelImage = -alpha*sobel + 255 |
||
75 | cv::Mat sobelImage; |
||
76 | sobel.convertTo(sobelImage,CV_8U,-255./sobmax,255); |
||
77 | |||
78 | // Display the image |
||
79 | cv::namedWindow("Sobel Image"); |
||
80 | cv::imshow("Sobel Image",sobelImage); |
||
81 | |||
82 | // Apply threshold to Sobel norm (low threshold value) |
||
83 | cv::Mat sobelThresholded; |
||
84 | cv::threshold(sobelImage, sobelThresholded, 225, 255, cv::THRESH_BINARY); |
||
85 | |||
86 | // Display the image |
||
87 | cv::namedWindow("Binary Sobel Image (low)"); |
||
88 | cv::imshow("Binary Sobel Image (low)",sobelThresholded); |
||
89 | |||
90 | // Apply threshold to Sobel norm (high threshold value) |
||
91 | cv::threshold(sobelImage, sobelThresholded, 190, 255, cv::THRESH_BINARY); |
||
92 | |||
93 | // Display the image |
||
94 | cv::namedWindow("Binary Sobel Image (high)"); |
||
95 | cv::imshow("Binary Sobel Image (high)",sobelThresholded); |
||
96 | |||
97 | // Compute Laplacian 3x3 |
||
98 | cv::Mat laplace; |
||
99 | cv::Laplacian(image,laplace,CV_8U,1,1,128); |
||
100 | |||
101 | // Display the image |
||
102 | cv::namedWindow("Laplacian Image"); |
||
103 | cv::imshow("Laplacian Image",laplace); |
||
104 | |||
105 | // Print window pixel values |
||
106 | for (int i=0; i<12; i++) { |
||
107 | for (int j=0; j<12; j++) |
||
108 | std::cout << std::setw(5) << static_cast<int>(laplace.at<uchar>(i+135,j+362))-128 << " "; |
||
109 | std::cout << std::endl; |
||
110 | } |
||
111 | std::cout << std::endl; |
||
112 | std::cout << std::endl; |
||
113 | std::cout << std::endl; |
||
114 | |||
115 | // Compute Laplacian 7x7 |
||
116 | cv::Laplacian(image,laplace,CV_8U,7,0.01,128); |
||
117 | |||
118 | // Display the image |
||
119 | cv::namedWindow("Laplacian Image"); |
||
120 | cv::imshow("Laplacian Image",laplace); |
||
121 | |||
122 | // Print window pixel values |
||
123 | for (int i=0; i<12; i++) { |
||
124 | for (int j=0; j<12; j++) |
||
125 | std::cout << std::setw(5) << static_cast<int>(laplace.at<uchar>(i+135,j+362))-128 << " "; |
||
126 | std::cout << std::endl; |
||
127 | } |
||
128 | |||
129 | // Extract small window |
||
130 | cv::Mat window(image,cv::Rect(362,135,12,12)); |
||
131 | cv::namedWindow("Image window"); |
||
132 | cv::imshow("Image window",window); |
||
133 | cv::imwrite("window.bmp",window); |
||
134 | |||
135 | // Compute Laplacian using LaplacianZC class |
||
136 | LaplacianZC laplacian; |
||
137 | laplacian.setAperture(7); |
||
138 | cv::Mat flap= laplacian.computeLaplacian(image); |
||
139 | double lapmin, lapmax; |
||
140 | cv::minMaxLoc(flap,&lapmin,&lapmax); |
||
141 | std::cout << "Laplacian value range=[" << lapmin << "," << lapmax << "]\n"; |
||
142 | laplace= laplacian.getLaplacianImage(); |
||
143 | cv::namedWindow("Laplacian Image (7x7)"); |
||
144 | cv::imshow("Laplacian Image (7x7)",laplace); |
||
145 | |||
146 | // Print Laplacian values |
||
147 | std::cout << std::endl; |
||
148 | for (int i=0; i<12; i++) { |
||
149 | for (int j=0; j<12; j++) |
||
150 | std::cout << std::setw(5) << static_cast<int>(flap.at<float>(i+135,j+362)/100) << " "; |
||
151 | std::cout << std::endl; |
||
152 | } |
||
153 | std::cout << std::endl; |
||
154 | |||
155 | // Compute and display the zero-crossing points |
||
156 | cv::Mat zeros; |
||
157 | zeros= laplacian.getZeroCrossings(lapmax); |
||
158 | cv::namedWindow("Zero-crossings"); |
||
159 | cv::imshow("Zero-crossings",zeros); |
||
160 | |||
161 | // Compute and display the zero-crossing points (Sobel version) |
||
162 | zeros= laplacian.getZeroCrossings(); |
||
163 | zeros= laplacian.getZeroCrossingsWithSobel(50); |
||
164 | cv::namedWindow("Zero-crossings (2)"); |
||
165 | cv::imshow("Zero-crossings (2)",zeros); |
||
166 | |||
167 | // Print window pixel values |
||
168 | for (int i=0; i<12; i++) { |
||
169 | for (int j=0; j<12; j++) |
||
170 | std::cout << std::setw(2) << static_cast<int>(zeros.at<uchar>(i+135,j+362)) << " "; |
||
171 | std::cout << std::endl; |
||
172 | } |
||
173 | |||
174 | // Display the image with window |
||
175 | cv::rectangle(image,cv::Point(362,135),cv::Point(374,147),cv::Scalar(255,255,255)); |
||
176 | cv::namedWindow("Original Image with window"); |
||
177 | cv::imshow("Original Image with window",image); |
||
178 | |||
179 | cv::waitKey(); |
||
180 | return 0; |
||
181 | } |