该程序实现了基本的图像处理功能(只实现了bmp格式图处理)
1.图片的打开和定位像素位置。
void CImageproc::OpenFile()
{
// TODO: 在此处添加实现代码.
CFileDialog fileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
L"Bmp File(*.bmp)|*.bmp|JPG File(*.jpg)|*.jpg|All Files(*.*)|*.*||",
NULL,0,TRUE);if (fileDlg.DoModal() == IDOK)
{
CString stpathname = fileDlg.GetPathName();
LoadBmp(stpathname);
}
else
return;
}
void CImageproc::ShowBMP(CDC* pDC)
{
// TODO: 在此处添加实现代码.
if (m_hDib != NULL)
{
if (pDC == NULL)
return;
::SetStretchBltMode(pDC->m_hDC, COLORONCOLOR);
::StretchDIBits(pDC->m_hDC,
0, 0,
pBIH->biWidth,
pBIH->biHeight,
0, 0,
pBIH->biWidth,
pBIH->biHeight,
pBits,
(BITMAPINFO*)pBIH,
DIB_RGB_COLORS,
SRCCOPY);}
}
void CImageproc::GetColor(CClientDC *dc,int p_x,int p_y)
{if (p_x >= nWidth || p_y >= nHeight) {
CString str;
str.Format(_T("超出图像区域,非法访问!"));
dc->TextOut(p_x, p_y, str);
return;
}COLORREF rgb = dc->GetPixel(p_x, p_y);
int r = GetRValue(rgb);
int g = GetGValue(rgb);
int b = GetBValue(rgb);
int R, G, B;
double R1, G1, B1;
DWORD* pMask;
CString str, str1, str2, col, pix;
unsigned int pitch, index, index2;
//
if (pBIH == NULL);
else
switch (pBIH->biBitCount)
{
//biBitCount=1,代表处理单色位图
case 1:
//每一行存储必须为字的整数倍,即为32的整数倍,故需要对于不满32位的存储空间进行拓展
if (nWidth % 32 == 0)
pitch = nWidth;
else
pitch = (nWidth / 32 + 1) * 32;
//除以8是意味着
//对于内存的访问是以字节为单位的,在单色位图中,一个像素对应1位,故存储时只能将8个相邻的像素合并在一起
//这8个像素有相同的颜色
//存储时是以倒序方式存储
index = pBits[((nHeight - 1 - p_y) * pitch + p_x) / 8];
index = (index>>(7-p_x%8))&1;
//为0代表为黑。为1代表为白
if (index == 0)
col.Format(_T("这是一幅1位灰度图:GetColor函数:R=0,G=0,B=0"));
else
col.Format(_T("这是一幅1位灰度图:Getcolor函数:R=255,G=255,B=255"));
break;
case 4:
if (nWidth % 8 == 0)
pitch = nWidth;
else
pitch = nWidth + 8 - nWidth % 8;
index = pBits[((nHeight - 1 - p_y) * pitch + p_x) / 2];
if (p_x % 2 == 0) {
index /= 16;
}
else {
index %= 16;
}
R= pQUAD[index].rgbRed;
G = pQUAD[index].rgbGreen;
B = pQUAD[index].rgbBlue;
//R = G = B = index;
col.Format(_T("这是一幅16色图 GetColor函数:R=%d,G=%d,B=%d"), R, G, B);
break;
//处理8位图(灰度)
case 8:
if (nWidth % 4 == 0)
pitch = nWidth;
else
pitch = nWidth + 4 - nWidth % 4;
index = pBits[(nHeight - 1 - p_y) * pitch + p_x];
R = pQUAD[index].rgbRed;
G = pQUAD[index].rgbGreen;
B = pQUAD[index].rgbBlue;
col.Format(_T("这是一幅256色图 GetColor函数:R=%d,G=%d,B=%d"), R, G, B);
break;case 16:
pMask = (DWORD*)&pDib[sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)];
pitch = nWidth + nWidth % 2;
index = ((pBits[((nHeight - 1 - p_y) * pitch + p_x) * 2 + 1] << 8) & 0xFF00 | pBits[((nHeight - 1 - p_y) * pitch + p_x) * 2]);
if (pBIH->biCompression == BI_RGB)//555掩码
{
R = int((double)(index >> 10) * 255 / 31 + 0.5);
G = int((double)((index >> 5) % 32) * 255 / 31 + 0.5);
B = int((double)(index & 31) * 255 / 31 + 0.5);
col.Format(_T("这是一个16位彩色图:555掩码,Getcolor函数:R=%d,G=%d,B=%d"), R, G, B);
}
else //565掩码
{
R = int((double)(index >> 11) * 255 / 31 + 0.5);
G = int((double)((index >> 5) % 64) * 255 / 63 + 0.5);
B = int((double)(index & 31) * 255 / 31 + 0.5);
col.Format(_T("这是一个16位彩色图:565掩码,Getcolor函数:R=%d,G=%d,B=%d"), R, G, B);}
break;
case 24:
if (nWidth % 4 == 0)
pitch = 3*nWidth;
else
pitch = 3*nWidth-(3*nWidth%4)+4;
B = pBits[pitch*(nHeight-1-p_y)+3*p_x];
G = pBits[pitch * (nHeight - 1 - p_y) + 3 * p_x + 1];
R = pBits[pitch * (nHeight - 1 - p_y) + 3 * p_x + 2];
col.Format(_T("这是一个24位彩色图:GetColor函数:R=%d,G=%d,B=%d"), R, G, B);
break;
case 32:
pitch = nWidth;
B = pBits[((nHeight - 1 - p_y) * pitch + p_x) * 4];
G = pBits[((nHeight - 1 - p_y) * pitch + p_x) * 4 + 1];
R = pBits[((nHeight - 1 - p_y) * pitch + p_x) * 4 + 2];
col.Format(_T("Getcolor函数:R=%d,G=%d,B=%d"), R, G, B);
break;
default:
CString str;
str.Format(_T("无法处理此格式图像"));
MessageBox(str, _T("提示"), MB_ICONINFORMATION);
}str1.Format(_T("x=%d"), p_x);
str1 += str;
str.Format(_T(",y=%d"), p_y);
str2 += str;
str = str1 + str2;
pix.Format(_T("Getpixel函数:r=%d,g=%d,b=%d"), r, g, b);
dc->TextOut(p_x, p_y, str);
dc->TextOut(p_x, p_y + 40, pix);
dc->TextOut(p_x, p_y + 20, col);
}
2.图像的空间域滤波和频率域滤波。
//以中值滤波为例
void CImageproc::Medfilter(int radius)
{
CPicture_Filter* pic = new CPicture_Filter;
int lineByteOut = (nWidth * nNumColors / 8 + 3) / 4 * 4;
BYTE* lpDataOut = new BYTE[lineByteOut * nHeight];
pNewBits = lpDataOut;
BYTE* lpData = pBits;
int sumr[256] = { 0 }, sumg[256] = { 0 }, sumb[256] = { 0 }, sum[256] = { 0 };
if (radius < 1)
{
return;
}
int dim = 2 * radius + 1;
int p;
if (radius < 1)
{
return;
}
if (nNumColors == 24)
{
pic->m_hDib = m_hDib;
pic->pDib = pDib;
pic->pBFH = pBFH;
pic->pBIH = pBIH;
pic->pQUAD = pQUAD;
pic->pBits = lpData;for (int j = radius; j < nHeight - radius; j++)
{
for (int i = radius; i < nWidth - radius; i++)
{
p = 0;
for (int n = -radius; n <= radius; n++)
{
for (int m = -radius; m <= radius; m++)
{
int x = CLIP3(i + m, 0, nWidth - 1);
int y = CLIP3(j + n, 0, nHeight - 1);
sumb[p] = *(lpData + y * lineByteOut + x * 3 + 2);
sumg[p] = *(lpData + y * lineByteOut + x * 3 + 1);
sumr[p] = *(lpData + y * lineByteOut + x * 3);
p++;
}
}
//排序
BubbleSort(dim, sumr);
BubbleSort(dim, sumg);
BubbleSort(dim, sumb);
//映射
*(lpDataOut + j * lineByteOut + i * 3 + 2) = sumb[(dim * dim + 1) / 2];
*(lpDataOut + j * lineByteOut + i * 3 + 1) = sumg[(dim * dim + 1) / 2];
*(lpDataOut + j * lineByteOut + i * 3) = sumr[(dim * dim + 1) / 2];
}
}
pic->pBits2 = lpDataOut;
pic->OnPaint();
pic->DoModal();
for (int j = 0; j < nHeight; j++)
{
for (int i = 0; i < nWidth; i++)
{
*(lpData + j * lineByteOut + i * 3) = *(lpDataOut + j * lineByteOut + i * 3);
*(lpData + j * lineByteOut + i * 3 + 1) = *(lpDataOut + j * lineByteOut + i * 3 + 1);
*(lpData + j * lineByteOut + i * 3 + 2) = *(lpDataOut + j * lineByteOut + i * 3 + 2);
}
}
}
else
{
pic->m_hDib = m_hDib;
pic->pDib = pDib;
pic->pBFH = pBFH;
pic->pBIH = pBIH;
pic->pQUAD = pQUAD;
pic->pBits = lpData;for (int j = 0; j < nHeight; j++)
{
for (int i = 0; i < nWidth; i++)
{
p = 0;
for (int n = -radius; n <= radius; n++)
{
for (int m = -radius; m <= radius; m++)
{
int x = CLIP3(i + m, 0, nWidth - 1);
int y = CLIP3(j + n, 0, nHeight - 1);
sum[p] = *(lpData + y * lineByteOut + x);
p++;
}
}
//排序
BubbleSort(dim, sum);
//映射
*(lpDataOut + j * lineByteOut + i) = sum[(dim * dim + 1) / 2];
}
}
pic->pBits2 = lpDataOut;
pic->OnPaint();
pic->DoModal();
for (int j = 0; j < nHeight; j++)
{
for (int i = 0; i < nWidth; i++)
{
*(lpData + j * lineByteOut + i) = *(lpDataOut + j * lineByteOut + i);
}
}
}
delete lpDataOut;
}//巴特沃斯滤波器
void CImageproc::BtwasHF(bool inv, int dim, int D0) {
if (lpDataOut != NULL)
{
lpDataOut = NULL;
}BtwsShow* pic = new BtwsShow;
pic->m_hDib = m_hDib;
pic->pBIH = pBIH;
pic->pBits = pBits;
BYTE* lpData = pBits;
int lineByteOut = (nWidth * nNumColors / 8 + 3) / 4 * 4;
nWidth = lineByteOut / (nNumColors / 8);
lpDataOut = new BYTE[nHeight * lineByteOut];int size = nWidth * nHeight;
vector<double> temparray(size, 0);
vector<double> temparrayr(size, 0);
vector<double> temparrayg(size, 0);
vector<double> temparrayb(size, 0);long w = 1;
long h = 1;
while (w < nWidth)
{
w *= 2;
}
while (h < nHeight)
{
h *= 2;
}
int s = w * h;
vector<COMPLEX>com(s, { 0,0 });
vector<COMPLEX>comr(s, { 0,0 });
vector<COMPLEX>comg(s, { 0,0 });
vector<COMPLEX>comb(s, { 0,0 });
double* H = new double[s];
if (nNumColors == 8)
{
/*移中*/
for (int y = 0; y < nHeight; y++)
for (int x = 0; x < nWidth; x++)
{
//空间域乘以 pow(-1, x+y) ,频谱移至中心
com[y * w + x] = defcomplex(*(pBits + y * lineByteOut + x) * pow(-1.0, x + y), 0);
}COMPLEX* fredom = FFT2(com, nWidth, nHeight);
COMPLEX* Y = (COMPLEX*)malloc(sizeof(COMPLEX) * s);
//定义巴特沃斯高通滤波器
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
COMPLEX m = defcomplex((j - w / 2), (i - h / 2));
double D = absComplex(m);
H[i * w + j] = 1 / (1 + pow(D0 / D, 2 * dim));
}
}
//频域相乘
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
//Y[i * w + j] = mulComplex(H[i * w + j], fredom[i * w + j]);
Y[i * w + j].real = H[i * w + j] * fredom[i * w + j].real;
Y[i * w + j].imaginary = H[i * w + j] * fredom[i * w + j].imaginary;
}
}
//IFFT
COMPLEX* tidom = IFFT2(nWidth, nHeight, Y);
double temp = 0;
//赋值
for (int i = 0; i < nHeight; i++)
{
for (int j = 0; j < nWidth; j++)
{if (inv == true)
{
if ((i + j) % 2 == 0)
{
temp = tidom[i * w + j].real;
}
else
{
temp = -tidom[i * w + j].real;
}
}
else
{
temp = tidom[i * w + j].real;
}
temparray[i * lineByteOut + j] = CLIP3(temp, 0, 255);
}
}
//映射
for (int i = 0; i < nHeight; i++)
{
for (int j = 0; j < nWidth; j++)
{
*(lpDataOut + j + i * lineByteOut) = CLIP3(temparray[j + i * lineByteOut], 0, 255);
}
}
pic->pBits1 = new BYTE[nHeight * nWidth];
for (int j = 0; j < nHeight; j++)
{
for (int i = 0; i < nWidth; i++)
{
*(pic->pBits1 + j * lineByteOut + i) = *(lpDataOut + j * lineByteOut + i);
}
}
pic->OnPaint();
pic->DoModal();
for (int j = 0; j < nHeight; j++)
{
for (int i = 0; i < nWidth; i++)
{
*(pBits + j * lineByteOut + i) = *(lpDataOut + j * lineByteOut + i);
}
}
delete(fredom);
delete(tidom);
delete(Y);
}
else if (nNumColors == 24)
{
//移中
for (int y = 0; y < nHeight; y++)
for (int x = 0; x < nWidth; x++)
{
//空间域乘以 pow(-1, x+y) ,频谱移至中心
comr[y * w + x] = defcomplex(*(pBits + y * lineByteOut + x * 3 + 2) * pow(-1.0, x + y), 0);
comg[y * w + x] = defcomplex(*(pBits + y * lineByteOut + x * 3 + 1) * pow(-1.0, x + y), 0);
comb[y * w + x] = defcomplex(*(pBits + y * lineByteOut + x * 3) * pow(-1.0, x + y), 0);
}
COMPLEX* fredomr = FFT2(comr, nWidth, nHeight);
COMPLEX* fredomg = FFT2(comg, nWidth, nHeight);
COMPLEX* fredomb = FFT2(comb, nWidth, nHeight);
//定义巴特沃斯高通滤波器
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
COMPLEX m = defcomplex((j - w / 2), (i - h / 2));
double D = absComplex(m);
H[i * w + j] = 1 / (1 + pow(D0 / D, 2 * dim));
}
}
COMPLEX* Yr = (COMPLEX*)malloc(sizeof(COMPLEX) * s);
COMPLEX* Yg = (COMPLEX*)malloc(sizeof(COMPLEX) * s);
COMPLEX* Yb = (COMPLEX*)malloc(sizeof(COMPLEX) * s);
//频域相乘
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
Yr[i * w + j].real = H[i * w + j] * fredomr[i * w + j].real;
Yr[i * w + j].imaginary = H[i * w + j] * fredomr[i * w + j].imaginary;
Yg[i * w + j].real = H[i * w + j] * fredomg[i * w + j].real;
Yg[i * w + j].imaginary = H[i * w + j] * fredomg[i * w + j].imaginary;
Yb[i * w + j].real = H[i * w + j] * fredomb[i * w + j].real;
Yb[i * w + j].imaginary = H[i * w + j] * fredomb[i * w + j].imaginary;
}
}
//IFFT
COMPLEX* tidomr = IFFT2(nWidth, nHeight, Yr);
COMPLEX* tidomg = IFFT2(nWidth, nHeight, Yg);
COMPLEX* tidomb = IFFT2(nWidth, nHeight, Yb);
double tempr = 0, tempg = 0, tempb = 0;
//赋值
for (int i = 0; i < nHeight; i++)
{
for (int j = 0; j < nWidth; j++)
{
int k = i * lineByteOut + j;
if (inv == true)
{
if ((k / lineByteOut + k % lineByteOut) % 2 == 0)
{
tempr = tidomr[i * w + j].real;
tempg = tidomg[i * w + j].real;
tempb = tidomb[i * w + j].real;
}
else
{
tempr = -tidomr[i * w + j].real;
tempg = -tidomg[i * w + j].real;
tempb = -tidomb[i * w + j].real;
}
}
else
{
tempr = tidomr[i * w + j].real;
tempg = tidomg[i * w + j].real;
tempb = tidomb[i * w + j].real;
}
temparrayr[i * nWidth + j] = CLIP3(tempr, 0, 255);
temparrayg[i * nWidth + j] = CLIP3(tempg, 0, 255);
temparrayb[i * nWidth + j] = CLIP3(tempb, 0, 255);
}
}
//映射
for (int i = 0; i < nHeight; i++)
{
for (int j = 0; j < nWidth; j++)
{
*(lpDataOut + j * 3 + 2 + i * lineByteOut) = CLIP3(temparrayr[j + i * nWidth], 0, 255);
*(lpDataOut + j * 3 + 1 + i * lineByteOut) = CLIP3(temparrayg[j + i * nWidth], 0, 255);
*(lpDataOut + j * 3 + i * lineByteOut) = CLIP3(temparrayb[j + i * nWidth], 0, 255);
}
}
pic->pBits1 = new BYTE[nHeight * nWidth * 3];
for (int j = 0; j < nHeight; j++)
{
for (int i = 0; i < nWidth; i++)
{
*(pic->pBits1 + j * lineByteOut + i * 3) = *(lpDataOut + j * lineByteOut + i * 3);
*(pic->pBits1 + j * lineByteOut + i * 3 + 1) = *(lpDataOut + j * lineByteOut + i * 3 + 1);
*(pic->pBits1 + j * lineByteOut + i * 3 + 2) = *(lpDataOut + j * lineByteOut + i * 3 + 2);
}
}
pic->OnPaint();
pic->DoModal();
for (int j = 0; j < nHeight; j++)
{
for (int i = 0; i < nWidth; i++)
{
*(pBits + j * lineByteOut + i * 3) = *(lpDataOut + j * lineByteOut + i * 3);
*(pBits + j * lineByteOut + i * 3 + 1) = *(lpDataOut + j * lineByteOut + i * 3 + 1);
*(pBits + j * lineByteOut + i * 3 + 2) = *(lpDataOut + j * lineByteOut + i * 3 + 2);
}
}
delete(fredomr);
delete(fredomg);
delete(fredomb);
delete(tidomr);
delete(tidomg);
delete(tidomb);
delete(Yr);
delete(Yg);
delete(Yb);
}
else if (nNumColors == 16) {
if (pBIH->biCompression == BI_RGB) {
vector <BYTE>lpDataOut2(2 * s, 0);
vector<COMPLEX> comr(s, { 0,0 });
vector<COMPLEX> comg(s, { 0,0 });
vector<COMPLEX> comb(s, { 0,0 });
//移中
for (int y = 0; y < nHeight; y++)
for (int x = 0; x < nWidth; x++)
{
//空间域乘以 pow(-1, x+y) ,频谱移至中心
int index = ((pBits[(y * nWidth + x) * 2 + 1] << 8) & 0xFF00) | pBits[(y * nWidth + x) * 2];
comr[y * w + x] = defcomplex(int((double)(index >> 10) * 255 / 31 + 0.5) * pow(-1.0, x + y), 0);
comg[y * w + x] = defcomplex(int((double)((index >> 5) % 32) * 255 / 31 + 0.5) * pow(-1.0, x + y), 0);
comb[y * w + x] = defcomplex(int((double)(index & 31) * 255 / 31 + 0.5) * pow(-1.0, x + y), 0);
}COMPLEX* fredomr = FFT2(comr, nWidth, nHeight);
COMPLEX* fredomg = FFT2(comg, nWidth, nHeight);
COMPLEX* fredomb = FFT2(comb, nWidth, nHeight);
COMPLEX* Yr = (COMPLEX*)malloc(sizeof(COMPLEX) * s);
COMPLEX* Yg = (COMPLEX*)malloc(sizeof(COMPLEX) * s);
COMPLEX* Yb = (COMPLEX*)malloc(sizeof(COMPLEX) * s);
//定义巴特沃斯高通滤波器
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
COMPLEX m = defcomplex((j - w / 2), (i - h / 2));
double D = absComplex(m);
H[i * w + j] = 1 / (1 + pow(D0 / D, 2 * dim));
}
}
//频域相乘
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
Yr[i * w + j].real = H[i * w + j] * fredomr[i * w + j].real;
Yr[i * w + j].imaginary = H[i * w + j] * fredomr[i * w + j].imaginary;
Yg[i * w + j].real = H[i * w + j] * fredomg[i * w + j].real;
Yg[i * w + j].imaginary = H[i * w + j] * fredomg[i * w + j].imaginary;
Yb[i * w + j].real = H[i * w + j] * fredomb[i * w + j].real;
Yb[i * w + j].imaginary = H[i * w + j] * fredomb[i * w + j].imaginary;
}
}
//IFFT
COMPLEX* tidomr = IFFT2(nWidth, nHeight, Yr);
COMPLEX* tidomg = IFFT2(nWidth, nHeight, Yg);
COMPLEX* tidomb = IFFT2(nWidth, nHeight, Yb);
double tempr = 0, tempg = 0, tempb = 0;
//赋值
for (int i = 0; i < nHeight; i++)
{
for (int j = 0; j < nWidth; j++)
{
int k = i * lineByteOut + j;
if (inv == true)
{
if ((k / lineByteOut + k % lineByteOut) % 2 == 0)
{
tempr = tidomr[i * w + j].real;
tempg = tidomg[i * w + j].real;
tempb = tidomb[i * w + j].real;
}
else
{
tempr = -tidomr[i * w + j].real;
tempg = -tidomg[i * w + j].real;
tempb = -tidomb[i * w + j].real;
}
}
else
{
tempr = tidomr[i * w + j].real;
tempg = tidomg[i * w + j].real;
tempb = tidomb[i * w + j].real;
}
temparrayr[i * nWidth + j] = CLIP3(tempr, 0, 255);
temparrayg[i * nWidth + j] = CLIP3(tempg, 0, 255);
temparrayb[i * nWidth + j] = CLIP3(tempb, 0, 255);
}}
pic->pBits1 = new BYTE[2 * nHeight * nWidth];
for (int j = 0; j < nHeight; j++)
{
for (int i = 0; i < nWidth; i++)
{
int indexr = temparrayr[i + j * nWidth] * 31 / 255.0;
int indexg = temparrayg[i + j * nWidth] * 31 / 255.0;
int indexb = temparrayb[i + j * nWidth] * 31 / 255.0;
int index = (indexr << 10) + (indexg << 5) + indexb;
*(pic->pBits1 + j * nWidth * 2 + i * 2) = (BYTE)(index % 256);
*(pic->pBits1 + j * nWidth * 2 + i * 2 + 1) = (BYTE)(index >> 8);
}
}
pic->OnPaint();
pic->DoModal();
for (int j = 0; j < nHeight; j++)
{
for (int i = 0; i < nWidth; i++)
{
int indexr = temparrayr[i + j * nWidth] * 31 / 255.0;
int indexg = temparrayg[i + j * nWidth] * 31 / 255.0;
int indexb = temparrayb[i + j * nWidth] * 31 / 255.0;
int index = (indexr << 10) + (indexg << 5) + indexb;
*(pBits + j * nWidth * 2 + i * 2) = (BYTE)(index % 256);
*(pBits + j * nWidth * 2 + i * 2 + 1) = (BYTE)(index >> 8);
}
}}
else {
vector <BYTE>lpDataOut2(2 * s, 0);
vector<COMPLEX> comr(s, { 0,0 });
vector<COMPLEX> comg(s, { 0,0 });
vector<COMPLEX> comb(s, { 0,0 });
//移中
for (int y = 0; y < nHeight; y++)
for (int x = 0; x < nWidth; x++)
{
//空间域乘以 pow(-1, x+y) ,频谱移至中心
int index = ((pBits[(y * nWidth + x) * 2 + 1] << 8) & 0xFF00) | pBits[(y * nWidth + x) * 2];
comr[y * w + x] = defcomplex(int((double)(index >> 11) * 255 / 31 + 0.5) * pow(-1.0, x + y), 0);
comg[y * w + x] = defcomplex(int((double)((index >> 5) % 64) * 255 / 63 + 0.5) * pow(-1.0, x + y), 0);
comb[y * w + x] = defcomplex(int((double)(index & 31) * 255 / 31 + 0.5) * pow(-1.0, x + y), 0);
}COMPLEX* fredomr = FFT2(comr, nWidth, nHeight);
COMPLEX* fredomg = FFT2(comg, nWidth, nHeight);
COMPLEX* fredomb = FFT2(comb, nWidth, nHeight);
COMPLEX* Yr = (COMPLEX*)malloc(sizeof(COMPLEX) * s);
COMPLEX* Yg = (COMPLEX*)malloc(sizeof(COMPLEX) * s);
COMPLEX* Yb = (COMPLEX*)malloc(sizeof(COMPLEX) * s);
//定义巴特沃斯高通滤波器
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
COMPLEX m = defcomplex((j - w / 2), (i - h / 2));
double D = absComplex(m);
H[i * w + j] = 1 / (1 + pow(D0 / D, 2 * dim));
}
}
//频域相乘
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
Yr[i * w + j].real = H[i * w + j] * fredomr[i * w + j].real;
Yr[i * w + j].imaginary = H[i * w + j] * fredomr[i * w + j].imaginary;
Yg[i * w + j].real = H[i * w + j] * fredomg[i * w + j].real;
Yg[i * w + j].imaginary = H[i * w + j] * fredomg[i * w + j].imaginary;
Yb[i * w + j].real = H[i * w + j] * fredomb[i * w + j].real;
Yb[i * w + j].imaginary = H[i * w + j] * fredomb[i * w + j].imaginary;
}
}
//IFFT
COMPLEX* tidomr = IFFT2(nWidth, nHeight, Yr);
COMPLEX* tidomg = IFFT2(nWidth, nHeight, Yg);
COMPLEX* tidomb = IFFT2(nWidth, nHeight, Yb);
double tempr = 0, tempg = 0, tempb = 0;
//赋值
for (int i = 0; i < nHeight; i++)
{
for (int j = 0; j < nWidth; j++)
{
int k = i * lineByteOut + j;
if (inv == true)
{
if ((k / lineByteOut + k % lineByteOut) % 2 == 0)
{
tempr = tidomr[i * w + j].real;
tempg = tidomg[i * w + j].real;
tempb = tidomb[i * w + j].real;
}
else
{
tempr = -tidomr[i * w + j].real;
tempg = -tidomg[i * w + j].real;
tempb = -tidomb[i * w + j].real;
}
}
else
{
tempr = tidomr[i * w + j].real;
tempg = tidomg[i * w + j].real;
tempb = tidomb[i * w + j].real;
}
temparrayr[i * nWidth + j] = CLIP3(tempr, 0, 255);
temparrayg[i * nWidth + j] = CLIP3(tempg, 0, 255);
temparrayb[i * nWidth + j] = CLIP3(tempb, 0, 255);
}}
pic->pBits1 = new BYTE[2 * nHeight * nWidth];
for (int j = 0; j < nHeight; j++)
{
for (int i = 0; i < nWidth; i++)
{
int indexr = temparrayr[i + j * nWidth] * 31 / 255.0;
int indexg = temparrayg[i + j * nWidth] * 63 / 255.0;
int indexb = temparrayb[i + j * nWidth] * 31 / 255.0;
int index = (indexr << 11) + (indexg << 5) + indexb;
*(pic->pBits1 + j * nWidth * 2 + i * 2) = (BYTE)(index % 256);
*(pic->pBits1 + j * nWidth * 2 + i * 2 + 1) = (BYTE)(index >> 8);
}
}
pic->OnPaint();
pic->DoModal();
for (int j = 0; j < nHeight; j++)
{
for (int i = 0; i < nWidth; i++)
{
int indexr = temparrayr[i + j * nWidth] * 31 / 255.0;
int indexg = temparrayg[i + j * nWidth] * 63 / 255.0;
int indexb = temparrayb[i + j * nWidth] * 31 / 255.0;
int index = (indexr << 11) + (indexg << 5) + indexb;
*(pBits + j * nWidth * 2 + i * 2) = (BYTE)(index % 256);
*(pBits + j * nWidth * 2 + i * 2 + 1) = (BYTE)(index >> 8);
}
}}
}
else if (nNumColors == 32) {
//移中
for (int y = 0; y < nHeight; y++)
for (int x = 0; x < nWidth; x++)
{
//空间域乘以 pow(-1, x+y) ,频谱移至中心
comr[y * w + x] = defcomplex(*(pBits + y * lineByteOut + x * 4 + 2) * pow(-1.0, x + y), 0);
comg[y * w + x] = defcomplex(*(pBits + y * lineByteOut + x * 4 + 1) * pow(-1.0, x + y), 0);
comb[y * w + x] = defcomplex(*(pBits + y * lineByteOut + x * 4) * pow(-1.0, x + y), 0);
}
COMPLEX* fredomr = FFT2(comr, nWidth, nHeight);
COMPLEX* fredomg = FFT2(comg, nWidth, nHeight);
COMPLEX* fredomb = FFT2(comb, nWidth, nHeight);
//定义巴特沃斯高通滤波器
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
COMPLEX m = defcomplex((j - w / 2), (i - h / 2));
double D = absComplex(m);
H[i * w + j] = 1 / (1 + pow(D0 / D, 2 * dim));
}
}
COMPLEX* Yr = (COMPLEX*)malloc(sizeof(COMPLEX) * s);
COMPLEX* Yg = (COMPLEX*)malloc(sizeof(COMPLEX) * s);
COMPLEX* Yb = (COMPLEX*)malloc(sizeof(COMPLEX) * s);
//频域相乘
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
Yr[i * w + j].real = H[i * w + j] * fredomr[i * w + j].real;
Yr[i * w + j].imaginary = H[i * w + j] * fredomr[i * w + j].imaginary;
Yg[i * w + j].real = H[i * w + j] * fredomg[i * w + j].real;
Yg[i * w + j].imaginary = H[i * w + j] * fredomg[i * w + j].imaginary;
Yb[i * w + j].real = H[i * w + j] * fredomb[i * w + j].real;
Yb[i * w + j].imaginary = H[i * w + j] * fredomb[i * w + j].imaginary;
}
}
//IFFT
COMPLEX* tidomr = IFFT2(nWidth, nHeight, Yr);
COMPLEX* tidomg = IFFT2(nWidth, nHeight, Yg);
COMPLEX* tidomb = IFFT2(nWidth, nHeight, Yb);
double tempr = 0, tempg = 0, tempb = 0;
//赋值
for (int i = 0; i < nHeight; i++)
{
for (int j = 0; j < nWidth; j++)
{
int k = i * lineByteOut + j;
if (inv == true)
{
if ((k / lineByteOut + k % lineByteOut) % 2 == 0)
{
tempr = tidomr[i * w + j].real;
tempg = tidomg[i * w + j].real;
tempb = tidomb[i * w + j].real;
}
else
{
tempr = -tidomr[i * w + j].real;
tempg = -tidomg[i * w + j].real;
tempb = -tidomb[i * w + j].real;
}
}
else
{
tempr = tidomr[i * w + j].real;
tempg = tidomg[i * w + j].real;
tempb = tidomb[i * w + j].real;
}
temparrayr[i * nWidth + j] = CLIP3(tempr, 0, 255);
temparrayg[i * nWidth + j] = CLIP3(tempg, 0, 255);
temparrayb[i * nWidth + j] = CLIP3(tempb, 0, 255);
}
}
//映射
for (int i = 0; i < nHeight; i++)
{
for (int j = 0; j < nWidth; j++)
{
*(lpDataOut + j * 4 + 2 + i * lineByteOut) = CLIP3(temparrayr[j + i * nWidth], 0, 255);
*(lpDataOut + j * 4 + 1 + i * lineByteOut) = CLIP3(temparrayg[j + i * nWidth], 0, 255);
*(lpDataOut + j * 4 + i * lineByteOut) = CLIP3(temparrayb[j + i * nWidth], 0, 255);
}
}
pic->pBits1 = new BYTE[nHeight * nWidth * 4];
for (int j = 0; j < nHeight; j++)
{
for (int i = 0; i < nWidth; i++)
{
*(pic->pBits1 + j * lineByteOut + i * 4) = *(lpDataOut + j * lineByteOut + i * 4);
*(pic->pBits1 + j * lineByteOut + i * 4 + 1) = *(lpDataOut + j * lineByteOut + i * 4 + 1);
*(pic->pBits1 + j * lineByteOut + i * 4 + 2) = *(lpDataOut + j * lineByteOut + i * 4 + 2);
}
}
pic->OnPaint();
pic->DoModal();
for (int j = 0; j < nHeight; j++)
{
for (int i = 0; i < nWidth; i++)
{
*(pBits + j * lineByteOut + i * 4) = *(lpDataOut + j * lineByteOut + i * 4);
*(pBits + j * lineByteOut + i * 4 + 1) = *(lpDataOut + j * lineByteOut + i * 4 + 1);
*(pBits + j * lineByteOut + i * 4 + 2) = *(lpDataOut + j * lineByteOut + i * 4 + 2);
}
}
delete(fredomr);
delete(fredomg);
delete(fredomb);
delete(tidomr);
delete(tidomg);
delete(tidomb);
delete(Yr);
delete(Yg);
delete(Yb);
}
delete(H);
return;
}
3.图像的直方图变换。
//全局直方图
void CImageproc::match(int x,double *cdfr1,double*cdfg1,double *cdfb1, double* cdfr2, double* cdfg2, double* cdfb2)
{
//中间变量
double tmphisr[256];
double tmphisg[256];
double tmphisb[256];
//概率
double tmpcdfr[256];
double tmpcdfg[256];
double tmpcdfb[256];
for (int i = 0; i < 256; i++)
{
tmphisr[i] = 0;
tmphisg[i] = 0;
tmphisb[i] = 0;
tmpcdfr[i] = 0;
tmpcdfg[i] = 0;
tmpcdfb[i] = 0;
}
int size = nWidth * nHeight;int pitch;
if (nWidth % 4 == 0)
pitch = nWidth;
else
pitch = nWidth + 4 - nWidth % 4;for (int i = 0; i < nWidth; i++)
{
for (int j = 0; j < nHeight; j++)
{tmphisr[pBits[((nHeight - 1 - j) * nWidth + i) * 3 ]]++;
tmphisg[pBits[((nHeight - 1 - j) * nWidth + i) * 3 +1]]++;
tmphisb[pBits[((nHeight - 1 - j) * nWidth + i) * 3+2 ]]++;
}
}
for (int j = 0; j < 256; j++)
{
if (j == 0)
{
tmpcdfr[j] = tmphisr[j];
tmpcdfg[j] = tmphisg[j];
tmpcdfb[j] = tmphisb[j];
}
else
{
tmpcdfr[j] = tmpcdfr[j - 1] + tmphisr[j];
tmpcdfg[j] = tmpcdfg[j - 1] + tmphisg[j];
tmpcdfb[j] = tmpcdfb[j - 1] + tmphisb[j];
}
}
for (int j = 0; j < 256; j++)
{
tmpcdfr[j] = (tmpcdfr[j]) / (double)size;
tmpcdfg[j] = (tmpcdfg[j]) / (double)size;
tmpcdfb[j] = (tmpcdfb[j]) / (double)size;
}if (x == 1)//目标图像
{
for (int i = 0; i < 256; i++)
{
cdfr2[i] = tmpcdfr[i];
cdfg2[i] = tmpcdfg[i];
cdfb2[i] = tmpcdfb[i];
}
}
else//原图像
{
for (int i = 0; i < 256; i++)
{
cdfr1[i] = tmpcdfr[i];
cdfg1[i] = tmpcdfg[i];
cdfb1[i] = tmpcdfb[i];
}}
//UpdateData();}
其余功能代码见上传的压缩包。
下面是开发的exe文件的io界面