Subversion Repositories OpenCV2-Cookbook

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 PointedEar 1
/*------------------------------------------------------------------------------------------*\
2
   This file contains material supporting chapter 4 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
using namespace std;
20
 
21
#include "cv.h"
22
#include "highgui.h"
23
#include "histogram.h"
24
 
25
int main()
26
{
27
        // Read input image
28
        cv::Mat image= cv::imread("../group.jpg",0);
29
        if (!image.data)
30
                return 0;
31
 
32
    // Display the image
33
        cv::namedWindow("Image");
34
        cv::imshow("Image",image);
35
 
36
        // The histogram object
37
        Histogram1D h;
38
 
39
    // Compute the histogram
40
        cv::MatND histo= h.getHistogram(image);
41
 
42
        // Loop over each bin
43
        for (int i=0; i<256; i++)
44
                cout << "Value " << i << " = " << histo.at<float>(i) << endl;  
45
 
46
        // Display a histogram as an image
47
        cv::namedWindow("Histogram");
48
        cv::imshow("Histogram",h.getHistogramImage(image));
49
 
50
        // creating a binary image by thresholding at the valley
51
        cv::Mat thresholded;
52
        cv::threshold(image,thresholded,60,255,cv::THRESH_BINARY);
53
 
54
        // Display the thresholded image
55
        cv::namedWindow("Binary Image");
56
        cv::imshow("Binary Image",thresholded);
57
        cv::imwrite("binary.bmp",thresholded);
58
 
59
        // Equalize the image
60
        cv::Mat eq= h.equalize(image);
61
 
62
        // Show the result
63
        cv::namedWindow("Equalized Image");
64
        cv::imshow("Equalized Image",eq);
65
 
66
        // Show the new histogram
67
        cv::namedWindow("Equalized Histogram");
68
        cv::imshow("Equalized Histogram",h.getHistogramImage(eq));
69
 
70
        // Stretch the image ignoring bins with less than 5 pixels
71
        cv::Mat str= h.stretch(image,5);
72
 
73
        // Show the result
74
        cv::namedWindow("Stretched Image");
75
        cv::imshow("Stretched Image",str);
76
 
77
        // Show the new histogram
78
        cv::namedWindow("Stretched Histogram");
79
        cv::imshow("Stretched Histogram",h.getHistogramImage(str));
80
 
81
        // Create an image inversion table
82
        uchar lookup[256];
83
 
84
        for (int i=0; i<256; i++) {
85
 
86
                lookup[i]= 255-i;
87
        }
88
 
89
        // Apply lookup and display negative image
90
        cv::namedWindow("Negative image");
91
        cv::imshow("Negative image",h.applyLookUp(image,lookup));
92
 
93
        cv::waitKey();
94
        return 0;
95
}
96