openCV를 활용하여 그림 내 픽셀의 값을 밝기별로 히스토그램으로 정렬한 후, 히스토그램 평준화를 한 코드이다.

히스토그램 평준화를 통해서 그림의 화질개선을 시킬 수 있다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#pragma once
#include "IPDef.h"
#include "ISP.h"
//Gray Histogram
int main(void)
{
    //load color image
    Mat img_color = imread("histo.jpg");
    Mat img_gray = Mat(img_color.rows, img_color.cols, CV_8UC1);
    ISP isp;
    isp.cvtColor(img_color.data, img_gray.data,
        img_color.cols, img_color.rows,
        ECvtColor::eCvtBGR2GRAY);
    //histogram
    const int histoSize = 256;
    vector<int> vHisto(histoSize, 0);
    isp.getHistogram(img_gray.data,
        img_gray.cols,
        img_gray.rows,
        vHisto.data(), vHisto.size());
 
    Mat img_draw = Mat(img_color.rows, img_color.cols + histoSize, CV_8UC3);
    img_draw = 0;
    uchar* pImgColor = img_color.data;
    uchar* pImgDraw = img_draw.data;
 
    for (int row = 0; row < img_color.rows; row++)
    {
        for (int col = 0; col < img_color.cols; col++)
        {
            int color_index = row * img_color.cols + col;
            int draw_index = row * img_draw.cols + col;
            color_index *= 3;
            draw_index *= 3;
            pImgDraw[draw_index + 0= pImgColor[color_index + 0];
            pImgDraw[draw_index + 1= pImgColor[color_index + 1];
            pImgDraw[draw_index + 2= pImgColor[color_index + 2];
        }
    }
 
    int maxval = *std::max_element(vHisto.begin(), vHisto.end());
    for (int i = 0; i < histoSize; i++)
    {
        int moveTo = img_color.cols + i;
        int lineTo = static_cast<int>((1.0f * vHisto[i] * img_draw.rows / maxval));
        line(img_draw, Point(moveTo, img_draw.rows),
            Point(moveTo, img_draw.rows - lineTo), Scalar(255255255));
    }
 
    //histogram equlization
    int sum = 0;
    vector<int> vAccHisto(histoSize, 0);
    for (int i = 0; i < histoSize; i++)
    {
        sum += vHisto[i];
        vAccHisto[i] = sum;
    }
 
    for (int i = 0; i < histoSize-1; i++)
    {
        int X = img_color.cols + i;
        int YmoveTo = static_cast<int>((1.0f * vAccHisto[i] * img_draw.rows / vAccHisto[histoSize - 1]));
        int YlineTo = static_cast<int>((1.0f * vAccHisto[i+1* img_draw.rows / vAccHisto[histoSize-1]));
        line(img_draw, Point(X, img_draw.rows-YmoveTo),
            Point(X, img_draw.rows-YlineTo), Scalar(00255), 5);
    }
    imwrite("D:\\img_histo_gray.bmp", img_gray);
    imwrite("D:\\img_histo.bmp", img_draw);
 
 
 
 
 
 
 
 
 
 
 
 
    //normalized acc histogram
    vector<float> norm_Acc_Histo(histoSize, 0);
    int length = img_gray.rows * img_gray.cols;
    for (int i = 0; i < histoSize; i++)
        norm_Acc_Histo[i] = (1.0f * vAccHisto[i]) / length;
 
    Mat img_gray_histoEqual = Mat(img_gray.rows, img_gray.cols, CV_8UC1);
    uchar* pImgGrayHistoEqual = img_gray_histoEqual.data;
    for (int row = 0; row < img_gray.rows; row++)
    {
        for (int col = 0; col < img_gray.cols; col++)
        {
            int index = row * img_gray.cols + col;
            pImgGrayHistoEqual[index] = norm_Acc_Histo[img_gray.data[index]] * 255;
        }
    }
 
 
    //histogram
    vector<int> vHistoEqul(histoSize, 0);
    isp.getHistogram(img_gray_histoEqual.data,
        img_gray_histoEqual.cols,
        img_gray_histoEqual.rows,
        vHistoEqul.data(), vHistoEqul.size());
 
    Mat img_draw_equal = Mat(img_color.rows, img_color.cols + histoSize, CV_8UC3);
 
    img_draw_equal = 0;
    uchar* pImgDraw_Equal = img_draw_equal.data;
 
    for (int row = 0; row < img_color.rows; row++)
    {
        for (int col = 0; col < img_color.cols; col++)
        {
            int color_index = row * img_color.cols + col;
            int draw_index = row * img_draw.cols + col;
            color_index *= 3;
            draw_index *= 3;
            pImgDraw_Equal[draw_index + 0= pImgColor[color_index + 0];
            pImgDraw_Equal[draw_index + 1= pImgColor[color_index + 1];
            pImgDraw_Equal[draw_index + 2= pImgColor[color_index + 2];
        }
    }
 
    maxval = *std::max_element(vHistoEqul.begin(), vHistoEqul.end());
    for (int i = 0; i < histoSize; i++)
    {
        int moveTo = img_color.cols + i;
        int lineTo = static_cast<int>((1.0f * vHistoEqul[i] * img_draw.rows / maxval));
        line(img_draw_equal, Point(moveTo, img_draw.rows),
            Point(moveTo, img_draw.rows - lineTo), Scalar(255255255));
    }
 
    sum = 0;
    vector<int> vAccHistoEqual(histoSize, 0);
    for (int i = 0; i < histoSize; i++)
    {
        sum += vHistoEqul[i];
        vAccHistoEqual[i] = sum;
    }
 
    for (int i = 0; i < histoSize - 1; i++)
    {
        int X = img_color.cols + i;
        int YmoveTo = static_cast<int>((1.0f * vAccHistoEqual[i] * img_draw_equal.rows / vAccHistoEqual[histoSize - 1]));
        int YlineTo = static_cast<int>((1.0f * vAccHistoEqual[i + 1* img_draw_equal.rows / vAccHistoEqual[histoSize - 1]));
        line(img_draw_equal, Point(X, img_draw_equal.rows-YmoveTo),
            Point(X, img_draw_equal.rows-YlineTo), Scalar(00255), 5);
    }
    imwrite("D:\\img_histo_equal_gray.bmp", img_gray_histoEqual);
    imwrite("D:\\img_histo_equal.bmp", img_draw_equal);
    cin.get();
}
cs

+ Recent posts