1
+ #include < opencv2\opencv.hpp>
2
+ #include < iostream>
3
+ #include < vector>
4
+
5
+ using namespace cv ;
6
+ using namespace std ;
7
+
8
+ void drawLine (Mat histImage, int bin_w, int i, int height, Mat b_hist, Scalar color) {
9
+ line (histImage,
10
+ Point (bin_w*(i - 1 ), height - cvRound (b_hist.at <float >(i - 1 ))),
11
+ Point (bin_w*(i), height - cvRound (b_hist.at <float >(i))),
12
+ color);
13
+ }
14
+
15
+ void drawHistogram (cv::InputArray img)
16
+ {
17
+ // Dividir los canales BRG
18
+ vector<Mat> bgr;
19
+ split (img, bgr);
20
+
21
+ // Crear un histograma con 256 bin (numero de subdivisiones) uno por pixel [0..256]
22
+ int numbins = 256 ;
23
+
24
+ // Establecer rango para los canales (B, G, R)
25
+ float range[] = { 0 , 256 };
26
+ const float * histRange = { range };
27
+
28
+ Mat b_hist, g_hist, r_hist;
29
+
30
+ // Calcular el histograma para cada canal
31
+ calcHist (&bgr[0 ], 1 , 0 , Mat (), b_hist, 1 , &numbins, &histRange);
32
+ calcHist (&bgr[1 ], 1 , 0 , Mat (), g_hist, 1 , &numbins, &histRange);
33
+ calcHist (&bgr[2 ], 1 , 0 , Mat (), r_hist, 1 , &numbins, &histRange);
34
+
35
+ // Tamano del histograma
36
+ int h_width = 512 ;
37
+ int h_height = 400 ;
38
+
39
+ // Crear una imagen para dibujar en ella
40
+ Mat histImage (h_height, h_width, CV_8UC3, Scalar (20 , 20 , 20 ));
41
+
42
+ // Normalizar los histogramas
43
+ normalize (b_hist, b_hist, 0 , h_height, NORM_MINMAX);
44
+ normalize (g_hist, g_hist, 0 , h_height, NORM_MINMAX);
45
+ normalize (r_hist, r_hist, 0 , h_height, NORM_MINMAX);
46
+
47
+ int bin_width = cvRound ((float )h_width / (float )numbins);
48
+
49
+ // Dibujar cada una de las lineas
50
+ for (int i = 1 ; i < numbins; i++)
51
+ {
52
+ drawLine (histImage, bin_width, i, h_height, b_hist, Scalar (255 , 0 , 0 ));
53
+ drawLine (histImage, bin_width, i, h_height, g_hist, Scalar (0 , 255 , 0 ));
54
+ drawLine (histImage, bin_width, i, h_height, r_hist, Scalar (0 , 0 , 255 ));
55
+ }
56
+
57
+ // Mostrar el histograma
58
+ imshow (" Histograma BGR" , histImage);
59
+ }
60
+
61
+ void equalizeColorImage (cv::InputArray src, cv::OutputArray dst)
62
+ {
63
+ // Convertir BGR image a YCrCb
64
+ Mat ycrcb;
65
+ cvtColor (src, ycrcb, COLOR_BGR2YCrCb);
66
+
67
+ // Extarer cada uno de los canales
68
+ vector<Mat> channels;
69
+ split (ycrcb, channels);
70
+
71
+ // Ecualizar histograma del canal Y (luminosidad)
72
+ equalizeHist (channels[0 ], channels[0 ]);
73
+
74
+ // Unir los canales nuevamente
75
+ merge (channels, ycrcb);
76
+
77
+ // Convertir color YCrCb a BGR
78
+ cvtColor (ycrcb, dst, COLOR_YCrCb2BGR);
79
+ }
80
+
81
+ int main (int , char ** argv)
82
+ {
83
+ cv::Mat src, dst, hist;
84
+
85
+ std::string original = " Imagen Original" ;
86
+ std::string equalized = " Imagen Equalizada" ;
87
+
88
+ // leer la imagen
89
+ src = cv::imread (" image/lena.jpg" , 1 );
90
+ if (src.empty ()) return -1 ;
91
+
92
+ equalizeColorImage (src, dst);
93
+ drawHistogram (src);
94
+
95
+ // // convertir en escala de grises y ecualizar histograma
96
+ // cv::cvtColor(src, src, cv::COLOR_BGR2GRAY);
97
+ // cv::equalizeHist(src, dst);
98
+
99
+ // mostrar la imagen original y ecualizada
100
+ cv::imshow (original, src);
101
+ cv::imshow (equalized, dst);
102
+
103
+ cv::waitKey (0 );
104
+ return 0 ;
105
+ }
0 commit comments