QT实现图像处理-傅立叶变换、傅立叶反变换、平滑、锐化与模板匹配
From: http://www.cnblogs.com/qytan36/archive/2010/04/04/1704226.html
實(shí)驗(yàn)環(huán)境:
1,Linux操作系統(tǒng)
2,QT3編程開發(fā)環(huán)境
3,C++編程語言
傅立葉變換和傅立葉反變換
1.1. 主要源代碼
readImage() 從圖像中讀取數(shù)據(jù)
writeImage() 往圖像中寫入數(shù)據(jù)
fft() 快速傅立葉變換
ifft() 快速傅立葉反變換
adjustImageSize() 調(diào)整圖像大小
fourier() 傅立葉變換
ifourier() 傅立葉反變換
1.1.1 從圖像中讀取數(shù)據(jù)
void ImageProcess::readImage(complex<double> data[], const QImage &srcImage)
{
byte *pImageBytes = srcImage.bits(); //數(shù)據(jù)首地址
int depth = srcImage.depth(); //每個像素的bit數(shù)
int lineBytes = srcImage.bytesPerLine(); //每行的字節(jié)數(shù)
int w = srcImage.width(); //寬
int h = srcImage.height(); //高
byte *pByte;
//遍歷讀取每個像素,并轉(zhuǎn)換為灰度值
int i, j;
for(i = 0; i < h; i++)
{
for(j = 0; j < w; j++)
{
if(8 == depth) //采用了256色調(diào)色板,8位顏色索引
{
pByte = pImageBytes + i * lineBytes + j;
data[i * w + j] = complex<double>( *pByte, 0);
}
else if(32 == depth)//32位表示,數(shù)據(jù)格式為0xFFBBGGRR或0xAABBGGRR
{
pByte = pImageBytes + i * lineBytes + j * 4;
//根據(jù)RGB模式轉(zhuǎn)化成YIQ色彩模式的方式,取Y作為灰度值
byte pixelValue = (byte)(0.299 * (float)pByte[0] + 0.587 * (float)pByte[1]
+ 0.114 * (float)pByte[2]);
data[i * w + j] = complex<double>( pixelValue, 0);
}
else
{
cout << "invalid format. depth = " << depth << "\n";
return;
}
}
}
}
1.1.2 將數(shù)據(jù)寫入圖像
//coef為比例系數(shù),主要用來調(diào)整灰度值以便于觀察
void ImageProcess::writeImage(QImage &destImage, const complex<double> data[], double coef)
{
int lineBytes = destImage.bytesPerLine();
int depth = destImage.depth();
int w = destImage.width();
int h = destImage.height();
byte *pImageBytes = destImage.bits();
byte *pByte;
for(int i = 0; i < h; i++)
{
for(int j = 0; j < w; j++)
{
double spectral = abs(data[i * w + j]) * coef; //灰度值調(diào)整
spectral = spectral > 255 ? 255 : spectral;
//根據(jù)圖像格式寫數(shù)據(jù)
if(8 == depth)
{
pByte = pImageBytes + i * lineBytes + j;
*pByte = spectral;
}
else if(32 == depth)
{
pByte = pImageBytes + i * lineBytes + j * 4;
pByte[0] = pByte[1] = pByte[2] = spectral;
}
else
{
return;
}
}
}
}
1.1.3 遞歸形式的快速傅立葉變換
//數(shù)組a為輸入,數(shù)組y為輸出,2的power次方為數(shù)組的長度
void ImageProcess::fft(const complex<double> a[], complex<double> y[], int power)
{
if(0 == power)
{
y[0] = a[0];
return;
}
int n = 1 << power;
double angle = 2 * PI / n;
complex<double> wn(cos(angle), sin(angle));
complex<double> w(1, 0);
complex<double> *a0 = new complex<double>[n / 2];
complex<double> *a1 = new complex<double>[n / 2];
complex<double> *y0 = new complex<double>[n / 2];
complex<double> *y1 = new complex<double>[n / 2];
for(int i = 0; i < n / 2; i ++)
{
a0[i] = a[2 * i];
a1[i] = a[2 * i + 1];
}
//分開成兩個子fft過程
fft(a0, y0, power - 1);
fft(a1, y1, power - 1);
complex<double> u;
for(int k = 0; k < n / 2; k++) //蝶形算法
{
u = w * y1[k];
y[k] = y0[k] + u;
y[k + n / 2] = y0[k] - u;
w = w * wn;
}
delete[] a0;
delete[] a1;
delete[] y0;
delete[] y1;
}
1.1.4 快速傅立葉反變換
//y為輸入,a為輸出,2的power次方為數(shù)組的長度
void ImageProcess::ifft(const complex<double> y[], complex<double> a[], int power)
{
int count = 1 << power;
complex<double> *x = new complex<double>[count];
memcpy(x, y, sizeof(complex<double>) * count);
int i;
for(i = 0; i < count; i++)
{
x[i] = complex<double>(x[i].real(), -x[i].imag()); //共軛復(fù)數(shù)
}
fft(x, a, power); //調(diào)用快速傅立葉變換算法
for(i = 0; i < count; i++)
{
a[i] = complex<double>(a[i].real() / count, -a[i].imag() / count); //共軛復(fù)數(shù)
}
delete[] x;
}
1.1.5 調(diào)整圖像的大小
//寬和高都截取為2的指數(shù)倍
void ImageProcess::adjustImageSize(QImage &image)
{
int w = 1;
int h = 1;
int width = image.width();
int height = image.height();
wp = 0, hp = 0;
while(w * 2 <= width) { w *= 2; wp++; }
while(h * 2 <= height) {h *= 2; hp++;}
QImage adjustedImage(w, h, image.depth(), image.numColors(), image.bitOrder());
byte *destBytes = adjustedImage.bits();
byte *srcBytes = image.bits();
int lineBytes = image.bytesPerLine();
int bytesPerPixel = image.depth() / 8; //每個象素的字節(jié)數(shù)
for(int i = 0; i < h; i++) //拷貝數(shù)據(jù)
{
memcpy(destBytes + i * w * bytesPerPixel, srcBytes + i * lineBytes,
sizeof(byte) * w * bytesPerPixel);
}
image = adjustedImage; //更新圖像
}
1.1.6 傅立葉變換的主過程
void ImageProcess::fourier()
{
int w = currentImage.width();
int h = currentImage.height();
if(needAdjust) //調(diào)整圖像的大小為2的冪次以便于快速傅立葉變換
{
adjustImageSize(currentImage); //調(diào)整大小
needAdjust = false;
if(currentImageData)
{
delete[] currentImageData;
}
currentImageData = new complex<double>[w * h];
readImage(currentImageData, currentImage); //讀取數(shù)據(jù)
}
else if(NULL == currentImageData)
{
currentImageData = new complex<double>[w * h];
readImage(currentImageData, currentImage); //讀取數(shù)據(jù)
}
w = currentImage.width(); //更新寬和高
h = currentImage.height();
complex<double> *TD = currentImageData; //當(dāng)前讀取的數(shù)據(jù)為時域
complex<double> *FD = new complex<double>[w * h]; //申請空間保存變換結(jié)果
int i, j;
for(i = 0; i < h; i++) //在x方向上對按行進(jìn)行快速傅立葉變換
{
fft(&TD[w * i], &FD[w * i], wp);
}
memcpy(TD, FD, sizeof(complex<double>) * w * h);
complex<double> *columnt = new complex<double>[h];
complex<double> *columnf = new complex<double>[h];
for(i = 0; i < w; i++) //調(diào)整行列數(shù)據(jù),在y方向上按列進(jìn)行快速傅立葉變換
{
for(j = 0; j < h; j++)
{
columnt[j] = TD[j * w + i];
}
fft(columnt, columnf, hp);
for(j = 0; j < h; j++)
{
FD[j * w + i] = columnf[j];
}
}
delete[] columnt;
delete[] columnf;
writeImage(currentImage, FD, 0.02); //寫入數(shù)據(jù)
delete[] currentImageData;
currentImageData = FD;
pDispLabel->setPixmap(QPixmap(currentImage));
}
1.1.7 傅立葉反變換
傅立葉反變換的思想與傅立葉變化相似,只是時域和頻域互換,然后調(diào)用快速傅立葉反變換ifft而不是快速傅立葉變換fft。
1.2. 運(yùn)行截圖
1.2.1 正方形
輸入一個256*256的圖形,背景為白色,中間有一黑色的正方形,如圖1-1所示。經(jīng)過傅立葉變換后的結(jié)果如圖1-2所示(注:沒有采用平移到中心的方法)。
圖1-1
圖1-2
1.2.2 旋轉(zhuǎn)45度
將圖1-1旋轉(zhuǎn)45度后的輸入如圖1-3所示。其傅立葉變換結(jié)果如圖1-4所示。
圖1-3
圖1-4
1.2.3 輸入長方形圖像
輸入圖像如圖1-5所示。傅立葉變換結(jié)果如圖1-6所示。
圖1-5
圖1-6
1.2.4 傅立葉反變換
對傅立葉變換結(jié)果圖1-2進(jìn)行傅立葉反變換,其結(jié)果與原圖1-1相同,如圖1-7所示:
圖1-7
圖像增強(qiáng)
圖像增強(qiáng)是一種很重要的圖像處理技術(shù),為了方便人們觀察以及機(jī)器處理而去處理給定的一幅圖像。有很多圖像增強(qiáng)的方法,以下這部分實(shí)現(xiàn)了其中的平滑和銳化這兩種方法。
2.1. 主要源碼
2.1.1 平滑
平滑采用的模板是, 實(shí)現(xiàn)如下:
void ImageProcess::smooth()
{
int w = currentImage.width();
int h = currentImage.height();
if(NULL == currentImageData) //判斷是否需要重新讀取數(shù)據(jù)
{
currentImageData = new complex<double>[w * h];
readImage(currentImageData, currentImage);
}
//拷貝一份數(shù)據(jù)便于計(jì)算
complex<double> *buffer = new complex<double>[w * h];
memcpy(buffer, currentImageData, sizeof(complex<double>) * w * h);
//根據(jù)模板進(jìn)行計(jì)算
//為了簡化編碼忽略了圖像邊界(i =0 or h, j =0 or w),對于整體效果沒有影響
int i, j;
for(i = 1; i < h - 1; i++)
{
for(j = 1; j < w - 1; j++)
{
complex<double> k;
k = buffer[(i - 1) * w + j - 1];
k += buffer[(i - 1) * w + j];
k += buffer[(i - 1) * w + j + 1];
k += buffer[i * w + j - 1];
k += buffer[i * w + j];
k += buffer[i * w + j + 1];
k += buffer[(i + 1) * w + j - 1];
k += buffer[(i + 1) * w + j];
k += buffer[(i + 1) * w + j + 1];
k = complex<double>(k.real() / 9, 0);
currentImageData[i * w + j] = k;
}
}
writeImage(currentImage, currentImageData);
pDispLabel->setPixmap(QPixmap(currentImage));
}
2.1.2 銳化
采用拉普拉斯銳化,其模板為,其實(shí)現(xiàn)如下:
void ImageProcess::sharp()
{
int w = currentImage.width();
int h = currentImage.height();
if(NULL == currentImageData) //判斷是否需要讀取數(shù)據(jù)
{
currentImageData = new complex<double>[w * h];
readImage(currentImageData, currentImage);
}
//拷貝一份數(shù)據(jù)便于計(jì)算
complex<double> *buffer = new complex<double>[w * h];
memcpy(buffer, currentImageData, sizeof(complex<double>) * w * h);
//根據(jù)模板進(jìn)行計(jì)算
//為了簡化編碼忽略了圖像邊界(i =0 or h, j =0 or w),對于整體效果沒有影響
int i, j;
complex<double> k;
for(i = 1; i < h - 1; i++)
{
for(j = 1; j < w - 1; j++)
{
k = buffer[i * w + j];
k = complex<double>(k.real() * 5, 0);
k -= buffer[(i - 1) * w + j];
k -= buffer[i * w + j - 1];
k -= buffer[i * w + j + 1];
k -= buffer[(i + 1) * w + j];
currentImageData[i * w + j] = k;
}
}
writeImage(currentImage, currentImageData);
pDispLabel->setPixmap(QPixmap(currentImage));
}
2.2. 運(yùn)行截圖
輸入圖像2-1,其平滑結(jié)果為圖2-2,銳化結(jié)果為 圖2-3。
圖2-1原來的圖像
圖2-2 平滑后的圖像
圖2-3 銳化后的圖像
圖像分析
這部分主要實(shí)現(xiàn)了圖像的模板匹配。模板匹配是一種非常原始的模式識別方法。有很多模板匹配的算法。這里采用的算法是計(jì)算二者之間的相似度,在目標(biāo)圖像中選取一個坐標(biāo),將以該坐標(biāo)為左上角選定一塊區(qū)域,計(jì)算該區(qū)域與模板的相似度,相似度最大的點(diǎn)即為匹配之處。通過二者之間的差異度來判斷其相似程度,差異度的計(jì)算:m = 。即將累加其像素之間的差值,為了提高計(jì)算速度,可以設(shè)置閥值,當(dāng)m大于閥值時,認(rèn)定該塊區(qū)域不匹配,繼續(xù)尋找下一區(qū)域。
3.1. 主要源碼
void ImageProcess::match()
{
//讓用戶選取模板
QString fileName = QFileDialog::getOpenFileName("/home/tanqiyu", "Images (*.png *.xpm
.jpg)", this, "open file dialog", "Choose a model image");
if(QString::null == fileName)
{
return;
}
//讀取模板數(shù)據(jù)
QImage modelImage(fileName);
int mw = modelImage.width();
int mh = modelImage.height();
complex<double> *modelImageData = new complex<double>[mw * mh];
readImage(modelImageData, modelImage);
unsigned long t = mw * mh * 8; //根據(jù)匹配模板的大小設(shè)置一定的閥值
unsigned long m = t; //初始差異度
int ri = -1; //z左上角坐標(biāo)(ri, rj)
int rj = -1;
int w = currentImage.width();
int h = currentImage.height();
if(NULL == currentImageData) //判斷是否需要讀取目標(biāo)圖像數(shù)據(jù)
{
currentImageData = new complex<double>[w * h];
readImage(currentImageData, currentImage);
}
//遍歷目標(biāo)圖像,選取左上角坐標(biāo),考慮到模板圖像的大小注意不要越界
int i, j;
for(i = 0; i < h - mh + 1; i++ )
{
for(j = 0; j < w - mw + 1; j++)
{
//下面開始對點(diǎn)(i, j)為左上角的mw * mh區(qū)域進(jìn)行匹配
bool overFlag = false;
unsigned long k = 0; //差異值的累加和
int u, v;
for(u = 0; u < mh && !overFlag; u++)
{
for(v = 0; v < mw && !overFlag; v++)
{
k += abs(currentImageData[(i + u) * w + j + v].real()
- modelImageData[u * mw + v].real()); //計(jì)算差值并累加
if(k >= t) //判斷是否大于閥值
{
overFlag = true;
}
}
}
if(k < m) //判斷是否找到更加匹配的區(qū)域
{
ri = i;
rj = j;
m = k;
}
}
}
//找到匹配區(qū)域,則將目標(biāo)圖像匹配區(qū)域之外的點(diǎn)置成白色以便于觀察結(jié)果
if(ri != -1)
{
for(i = 0; i < h; i++)
{
for(j = 0; j < w; j++)
{
if(i < ri || j < rj || i > ri + mh - 1 || j > rj + mw - 1)
{
currentImageData[i * w + j] = complex<double>(255, 0);
}
}
}
}
writeImage(currentImage, currentImageData);
pDispLabel->setPixmap(QPixmap(currentImage));
}
3.2. 運(yùn)行截圖
目標(biāo)圖像為圖3-1,圖3-2位匹配模板,匹配結(jié)果為圖3-3。
圖3-1 目標(biāo)圖像
圖3-2 匹配模板
匹配結(jié)果
總結(jié)
以上是生活随笔為你收集整理的QT实现图像处理-傅立叶变换、傅立叶反变换、平滑、锐化与模板匹配的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决红蜘蛛教师端教师图标显示灰色导致无法
- 下一篇: C++之deque