• 主页
  • 相册
  • 随笔
  • 目录
  • 存档
Total 244
Search AboutMe

  • 主页
  • 相册
  • 随笔
  • 目录
  • 存档

图像增强处理-直方图均衡化与规定化

2019-04-22

本方法用c/cpp实现,不依赖于opencv库

0.0.1. BMP图像读取:

  • _hxlbmp.h
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

#ifndef hxlbmpfiledll_H
#define hxlbmpfiledll_H
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <math.h>
#include <stdio.h>

#ifdef BUILD_HXL_DLL
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif

class EXPORT HXLBMPFILE
{
BYTE *Imagedata;

public:
int imagew, imageh;
int iYRGBnum; //1 if grey; 3 if color
RGBQUAD palette[256];

BYTE *pDataAt(int h, int Y0R0G1B2 = 0);
BOOL AllocateMem();

BOOL LoadBMPFILE(char *fname);
BOOL SaveBMPFILE(char *fname);

HXLBMPFILE();
~HXLBMPFILE();
};
#endif

  • _hxlbmp.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#define BUILD_HXL_DLL
#include "_hxlbmp.h"

HXLBMPFILE::HXLBMPFILE()
{
Imagedata = NULL;

for (int i = 0; i < 256; i++)
{
palette[i].rgbBlue =
palette[i].rgbGreen =
palette[i].rgbRed = i;
palette[i].rgbReserved = 0;
}

iYRGBnum = 0;
imagew = imageh = 0;
}

HXLBMPFILE::~HXLBMPFILE()
{
if (Imagedata)
delete[] Imagedata;
}

BYTE *HXLBMPFILE::pDataAt(int h, int Y0R0G1B2)
{
if (iYRGBnum <= Y0R0G1B2)
return NULL;
int w = imagew * h + Y0R0G1B2 * imageh * imagew;
return Imagedata + w;
}

BOOL HXLBMPFILE::AllocateMem()
{
int w = imagew * imageh * iYRGBnum;

if (Imagedata)
{
delete[] Imagedata;
Imagedata = NULL;
}

Imagedata = new BYTE[w];

if (Imagedata)
memset(Imagedata, 0, w);
return (Imagedata != NULL);
}

BOOL HXLBMPFILE::LoadBMPFILE(char *cFilename)
{
FILE *f;
if (strlen(cFilename) < 1)
return FALSE;

f = fopen(cFilename, "r+b");
if (f == NULL)
return FALSE;

BITMAPFILEHEADER fh;
BITMAPINFOHEADER ih;

fread(&fh, sizeof(BITMAPFILEHEADER), 1, f);
if (fh.bfType != 0x4d42) //"BM"
{
fclose(f);
return FALSE;
}

fread(&ih, sizeof(BITMAPINFOHEADER), 1, f);
if ((ih.biBitCount != 8) && (ih.biBitCount != 24))
{
fclose(f);
return FALSE;
}
printf("ih.biBitCount = %d",ih.biBitCount);
iYRGBnum = ih.biBitCount / 8;
imagew = ih.biWidth;
imageh = ih.biHeight;

if (!AllocateMem())
{
fclose(f);
return FALSE;
}

if (iYRGBnum == 1)
fread(palette, sizeof(RGBQUAD), 256, f);

fseek(f, fh.bfOffBits, SEEK_SET);

int w4b = (imagew * iYRGBnum + 3) / 4 * 4, i, j;
BYTE *ptr;

ptr = new BYTE[w4b];
if (iYRGBnum == 1)
{
for (i = imageh - 1; i >= 0; i--)
{
fread(ptr, w4b, 1, f);
memmove(pDataAt(i), ptr, imagew);
}
}
if (iYRGBnum == 3)
{
for (i = imageh - 1; i >= 0; i--)
{
fread(ptr, w4b, 1, f);
for (j = 0; j < imagew; j++)
{
*(pDataAt(i, 0) + j) = *(ptr + j * 3 + 2);
*(pDataAt(i, 1) + j) = *(ptr + j * 3 + 1);
*(pDataAt(i, 2) + j) = *(ptr + j * 3 + 0);
}
}
}
delete[] ptr;
fclose(f);
return TRUE;
}

BOOL HXLBMPFILE::SaveBMPFILE(char *cFilename)
{
if (!Imagedata)
return FALSE;
FILE *f;
if (strlen(cFilename) < 1)
return FALSE;
f = fopen(cFilename, "w+b");
if (f == NULL)
return FALSE;

BITMAPFILEHEADER fh;
BITMAPINFOHEADER ih;
memset(&ih, 0, sizeof(BITMAPINFOHEADER));
/*--headers--*/
fh.bfType = 0x4d42;
fh.bfReserved1 = fh.bfReserved2 = 0;
fh.bfOffBits = sizeof(LPBITMAPFILEHEADER) +
sizeof(BITMAPINFOHEADER) +
((iYRGBnum == 1) ? 256 * sizeof(RGBQUAD) : 0);

ih.biSize = 40;
ih.biPlanes = 1;
ih.biWidth = imagew;
ih.biHeight = imageh;
ih.biBitCount = 8 * iYRGBnum;

int w4b = (imagew * iYRGBnum + 3) / 4 * 4;
ih.biSizeImage = ih.biHeight * w4b;
fh.bfSize = fh.bfOffBits + ih.biSizeImage;

fwrite(&fh, sizeof(BITMAPFILEHEADER), 1, f);
fwrite(&ih, sizeof(BITMAPINFOHEADER), 1, f);
/*palette*/
if (iYRGBnum == 1)
fwrite(palette, sizeof(RGBQUAD), 256, f);

BYTE *ptr;
int i, j;
ptr = new BYTE[w4b];
memset(ptr, 0, w4b);
if (iYRGBnum == 1)
{
for (i = ih.biHeight - 1; i >= 0; i--)
{
memmove(ptr, pDataAt(i), ih.biWidth);
fwrite(ptr, w4b, 1, f);
}
}
if (iYRGBnum == 3)
{
for (i = ih.biHeight - 1; i >= 0; i--)
{
for (j = 0; j < ih.biWidth; j++)
{
*(ptr + j * 3 + 0) = *(pDataAt(i, 2) + j);
*(ptr + j * 3 + 1) = *(pDataAt(i, 1) + j);
*(ptr + j * 3 + 2) = *(pDataAt(i, 0) + j);
}
fwrite(ptr, w4b, 1, f);
}
}
delete[] ptr;
fclose(f);
return TRUE;
}
int main()
{
HXLBMPFILE bmp;
char Load_Addr[] = { "1.bmp" };
char Save_Addr[] = { "result.bmp" };
if (!bmp.LoadBMPFILE(Load_Addr))
{
return 1;
}
HXLBMPFILE bf;
bf.imagew = bmp.imagew;
bf.imageh = bmp.imageh;
bf.iYRGBnum = bmp.iYRGBnum;

if (!bf.AllocateMem())
{
return 1;
}
for (int i = 0; i < bmp.imageh; i++)
for (int j = 0; j < bmp.imagew; j++)
{
bf.pDataAt(i)[j] = bmp.pDataAt(i)[j];
}
if (bf.SaveBMPFILE(Save_Addr))
{
return 1;
}
}

0.0.2. 直方图均衡化

  1. 获取图像直方图:
1
2
3
4
5
6
7
8
9
10
11
int* GetHistagram(HXLBMPFILE* bmp)
{
int* ptrHistagram = new int[256];
memset(ptrHistagram, 0, sizeof(ptrHistagram));
for (int i = 0; i < (*bmp).imageh; i++)
for (int j = 0; j < (*bmp).imagew; j++)
{
ptrHistagram[((*bmp).pDataAt(i)[j])] ++;
}
return ptrHistagram;
}
  1. 获取累计直方图:
1
2
3
4
5
6
7
8
9
10
11
double* GetGAddHistagram(int* ptrHistagram, int w)
{
double* ptrGAgram = new double[256];
memset(ptrGAgram, 0, sizeof(ptrGAgram));
ptrGAgram[0] = ptrHistagram[0] / double(w);
for (int i = 1; i < 256; i++)
{
ptrGAgram[i] = (ptrHistagram[i] / double(w) + ptrGAgram[i - 1]);
}
return ptrGAgram;
}
  1. 获取均衡化:
1
2
3
4
5
6
7
8
int* EquHistagram(double* ptrGAgram)
{
int* ptrEqgram = new int[256];
memset(ptrEqgram, 0, sizeof(ptrEqgram));
for (int i = 0; i < 256; i++)
ptrEqgram[i] = 255 * int(ptrGAgram[i]);
return ptrEqgram;
}

​ 直方图均衡化实质上就是一个LUT,将原有灰度值变换到新的灰度值,由此增强图像对比度

0.0.3. 直方图规定化

  • 获取规定化图像的直方图(如上1)
    • 如给定直方图曲线:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int* getHistagram(int w)
{
double* temp = new double[256];
int* ptrHistagram = new int[256];
memset(ptrHistagram, 0, sizeof(ptrHistagram));
memset(temp,0,sizeof(temp));
double count = 0;
for (int i = 0; i < 256; i++)
{
temp[i] = 1 / 4.0 - (i / 255.0 - 1 / 2.0) * (i / 255.0 - 1 / 2.0);
count += temp[i];
}
printf("%f\n",count);
for (int i = 0; i < 256; i++)
{
ptrHistagram[i] =int(w*temp[i] / count);
}
return ptrHistagram;
}
  • 获取规定化图像的累计直方图(如上2)

  • 用原图的累计概率匹配规定化的累计概率:

    即是,找出与原概率最接近的规定化的概率,便将其灰度值相匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int* RegHistagram(double* ptrGAgram, double* ptrGAgram2)
{
int* ptrRegram = new int[256];
memset(ptrRegram, 0, sizeof(ptrRegram));
for (int i = 0; i < 256; i++)
{
if (ptrGAgram[i] <= ptrGAgram2[0])
ptrRegram[i] = 0;
else
for (int j = 1; j < 256; j++)
{
if (ptrGAgram[i] > ptrGAgram2[j] && ptrGAgram[i] <= ptrGAgram2[j + 1])
{
if ((ptrGAgram[i] - ptrGAgram2[j]) > (ptrGAgram2[j + 1] - ptrGAgram[i]))
ptrRegram[i] = j;
else
ptrRegram[i] = j + 1;
}
}
}
return ptrRegram;
}
  • Lab
  • Computer Graphics
  • Lab
[翻譯]Depth perception
© 2024 何决云 载入天数...