Rev 3 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3 | Rev 5 | ||
---|---|---|---|
1 | /*------------------------------------------------------------------------------------------*\
|
1 | /*------------------------------------------------------------------------------------------*\
|
2 | This file contains material supporting chapter 4 of the cookbook:
|
2 | This file contains material supporting chapter 4 of the cookbook:
|
3 | Computer Vision Programming using the OpenCV Library.
|
3 | Computer Vision Programming using the OpenCV Library.
|
4 | by Robert Laganiere, Packt Publishing, 2011.
|
4 | by Robert Laganiere, Packt Publishing, 2011.
|
5 | 5 | ||
6 | This program is free software; permission is hereby granted to use, copy, modify,
|
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,
|
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
|
8 | subject to the restriction that the copyright notice may not be removed
|
9 | or altered from any source or altered source distribution.
|
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.
|
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.
|
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,
|
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.
|
13 | and any consequent failure, is purely the responsibility of the user.
|
14 |
|
14 |
|
15 | Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
|
15 | Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
|
16 | \*------------------------------------------------------------------------------------------*/
|
16 | \*------------------------------------------------------------------------------------------*/
|
17 | 17 | ||
18 | #include <iostream>
|
18 | #include <iostream>
|
19 | using namespace std; |
19 | using namespace std; |
20 | 20 | ||
21 | #include "cv.h"
|
21 | #include <opencv/cv.h>
|
22 | #include "highgui.h"
|
22 | #include <opencv/highgui.h>
|
23 | #include "histogram.h"
|
23 | #include "histogram.h"
|
24 | 24 | ||
25 | int main() |
25 | int main() |
26 | {
|
26 | {
|
27 | // Read input image
|
27 | // Read input image
|
28 | cv::Mat image= cv::imread("../group.jpg",0); |
28 | cv::Mat image= cv::imread("../group.jpg",0); |
29 | if (!image.data) |
29 | if (!image.data) |
30 | return 0; |
30 | return 0; |
31 | 31 | ||
32 | // Display the image
|
32 | // Display the image
|
33 | cv::namedWindow("Image"); |
33 | cv::namedWindow("Image"); |
34 | cv::imshow("Image",image); |
34 | cv::imshow("Image",image); |
35 | 35 | ||
36 | // The histogram object
|
36 | // The histogram object
|
37 | Histogram1D h;
|
37 | Histogram1D h;
|
38 | 38 | ||
39 | // Compute the histogram
|
39 | // Compute the histogram
|
40 | cv::MatND histo= h.getHistogram(image); |
40 | cv::MatND histo= h.getHistogram(image); |
41 | 41 | ||
42 | // Loop over each bin
|
42 | // Loop over each bin
|
43 | for (int i=0; i<256; i++) |
43 | for (int i=0; i<256; i++) |
44 | cout << "Value " << i << " = " << histo.at<float>(i) << endl; |
44 | cout << "Value " << i << " = " << histo.at<float>(i) << endl; |
45 | 45 | ||
46 | // Display a histogram as an image
|
46 | // Display a histogram as an image
|
47 | cv::namedWindow("Histogram"); |
47 | cv::namedWindow("Histogram"); |
48 | cv::imshow("Histogram",h.getHistogramImage(image)); |
48 | cv::imshow("Histogram",h.getHistogramImage(image)); |
49 | 49 | ||
50 | // creating a binary image by thresholding at the valley
|
50 | // creating a binary image by thresholding at the valley
|
51 | cv::Mat thresholded; |
51 | cv::Mat thresholded; |
52 | cv::threshold(image,thresholded,60,255,cv::THRESH_BINARY); |
52 | cv::threshold(image,thresholded,60,255,cv::THRESH_BINARY); |
53 | 53 | ||
54 | // Display the thresholded image
|
54 | // Display the thresholded image
|
55 | cv::namedWindow("Binary Image"); |
55 | cv::namedWindow("Binary Image"); |
56 | cv::imshow("Binary Image",thresholded); |
56 | cv::imshow("Binary Image",thresholded); |
57 | cv::imwrite("binary.bmp",thresholded); |
57 | cv::imwrite("binary.bmp",thresholded); |
58 | 58 | ||
59 | // Equalize the image
|
59 | // Equalize the image
|
60 | cv::Mat eq= h.equalize(image); |
60 | cv::Mat eq= h.equalize(image); |
61 | 61 | ||
62 | // Show the result
|
62 | // Show the result
|
63 | cv::namedWindow("Equalized Image"); |
63 | cv::namedWindow("Equalized Image"); |
64 | cv::imshow("Equalized Image",eq); |
64 | cv::imshow("Equalized Image",eq); |
65 | 65 | ||
66 | // Show the new histogram
|
66 | // Show the new histogram
|
67 | cv::namedWindow("Equalized Histogram"); |
67 | cv::namedWindow("Equalized Histogram"); |
68 | cv::imshow("Equalized Histogram",h.getHistogramImage(eq)); |
68 | cv::imshow("Equalized Histogram",h.getHistogramImage(eq)); |
69 | 69 | ||
70 | // Stretch the image ignoring bins with less than 5 pixels
|
70 | // Stretch the image ignoring bins with less than 5 pixels
|
71 | cv::Mat str= h.stretch(image,5); |
71 | cv::Mat str= h.stretch(image,5); |
72 | 72 | ||
73 | // Show the result
|
73 | // Show the result
|
74 | cv::namedWindow("Stretched Image"); |
74 | cv::namedWindow("Stretched Image"); |
75 | cv::imshow("Stretched Image",str); |
75 | cv::imshow("Stretched Image",str); |
76 | 76 | ||
77 | // Show the new histogram
|
77 | // Show the new histogram
|
78 | cv::namedWindow("Stretched Histogram"); |
78 | cv::namedWindow("Stretched Histogram"); |
79 | cv::imshow("Stretched Histogram",h.getHistogramImage(str)); |
79 | cv::imshow("Stretched Histogram",h.getHistogramImage(str)); |
80 | 80 | ||
81 | // Create an image inversion table
|
81 | // Create an image inversion table
|
82 | uchar lookup[256]; |
82 | cv::Mat lookup; |
83 | 83 | ||
84 | for (int i=0; i<256; i++) { |
84 | for (int i=0; i<256; i++) { |
85 | 85 | ||
86 | lookup[i]= 255-i; |
86 | lookup.data[i]= 255-i; |
87 | }
|
87 | }
|
88 | 88 | ||
89 | // Apply lookup and display negative image
|
89 | // Apply lookup and display negative image
|
90 | cv::namedWindow("Negative image"); |
90 | cv::namedWindow("Negative image"); |
91 | cv::imshow("Negative image",h.applyLookUp(image,lookup)); |
91 | cv::imshow("Negative image",h.applyLookUp(image,lookup)); |
92 | 92 | ||
93 | cv::waitKey(); |
93 | cv::waitKey(); |
94 | return 0; |
94 | return 0; |
95 | }
|
95 | }
|
96 | 96 | ||
97 | 97 |