#include<QApplication>
#include<QImage>
#include<QLabel>
#include<QtMath>
#include <QImageReader>
#include<QDebug>
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QImage *oldImage,*leeImage;
oldImage = new QImage("test.tiff");
//原始图像长和宽
int width = 0,height = 0;
width = oldImage->width();
height = oldImage->height();
//滤波窗口大小
int filterWinSize = 3;
//滤波窗口一半
int halfWinSize = filterWinSize / 2;
//实际滤波窗口大小
int realWinSize = (halfWinSize * 2) + 1;
//相干斑指数平方
double exp = 1.0/realWinSize;
//double exp = 1.0;
//构造新图像
leeImage = new QImage(width,height,QImage::Format_RGB32);
//原图像开始的横纵坐标
int startRow,startCol,endRow,endCol;
startRow = halfWinSize + 1;
startCol = halfWinSize + 1;
endRow = width - halfWinSize;
endCol = height -halfWinSize;
//扫描整幅图像
for(int j = startCol; j < endCol; ++j)
{
for(int i = startRow; i < endRow; ++i )
{
//存储每个窗口rgb和
double counter_r = 0.0,counter_g = 0.0,counter_b = 0.0;
//存储像素rgb平均值
double mean_r = 0.0,mean_g = 0.0,mean_b = 0.0;
//实际每个窗口像素个数
int pixelCount = 0;
//每个小窗口始坐标
int win_start_row,win_start_col;
win_start_row = i - halfWinSize;
win_start_col = j - halfWinSize;
//遍历窗口像素,求窗口平均值
for(int win_i = 0; win_i < realWinSize; ++win_i)
{
for(int win_j = 0; win_j < realWinSize; ++win_j)
{
//统计像素个数
pixelCount++;
//取出每个像素rgb值并累加求和
QRgb rgb = oldImage->pixel((win_start_row + win_i),(win_start_col + win_j));
counter_r += qRed(rgb);
counter_g += qGreen(rgb);
counter_b += qBlue(rgb);
}
}
//qDebug()<<"pixelCount:"<<pixelCount<<"\n";
//qDebug()<<"realWinSize:"<<realWinSize<<"\n";
//qDebug()<<"exp:"<<exp<<"\n";
//求平均值
mean_r = counter_r / pixelCount;
mean_g = counter_g / pixelCount;
mean_b = counter_b / pixelCount;
//qDebug()<<"mean_r:"<<mean_r<<"mean_g:"<<mean_g<<"mean_b:"<<mean_b<<"\n";
//计数器清零
counter_r = 0.0;
counter_g = 0.0;
counter_b = 0.0;
//遍历窗口像素,求窗口方差
//像素rgb方差
double var_y_r = 0.0,var_y_g = 0.0,var_y_b = 0.0;
double var_x_r = 0.0,var_x_g = 0.0,var_x_b = 0.0;
//相干斑抑制参数
double b_r = 0.0,b_g = 0.0,b_b = 0.0;
//最终rgb值
int result_r = 0,result_g = 0,result_b = 0;
for(int win_i = 0; win_i < realWinSize; ++win_i)
{
for(int win_j = 0; win_j < realWinSize; ++win_j)
{
//取出每个像素rgb值并累加求和
QRgb rgb = oldImage->pixel((win_start_row + win_i),(win_start_col + win_j));
//
double temp_r = 0.0,temp_g = 0.0,temp_b = 0.0;
temp_r = qRed(rgb) - mean_r;
temp_g = qGreen(rgb) - mean_g;
temp_b = qBlue(rgb) - mean_b;
//qDebug()<<"temp_r:"<<temp_r<<"temp_g:"<<temp_g<<"temp_b:"<<temp_b<<"\n";
counter_r += (temp_r * temp_r);
counter_g += (temp_g * temp_g);
counter_b += (temp_b * temp_b);
}
}
//求方差
var_y_r = counter_r / pixelCount;
var_y_g = counter_g / pixelCount;
var_y_b = counter_b / pixelCount;
//qDebug()<<"counter_r:"<<counter_r<<"counter_g:"<<counter_g<<"counter_b:"<<counter_b<<"\n";
//qDebug()<<"var_y_r:"<<var_y_r<<"var_y_g:"<<var_y_g<<"var_y_b:"<<var_y_b<<"\n";
//
var_x_r = (var_y_r - ( mean_r * mean_r * exp))/(1.0+exp);
var_x_g = (var_y_g - ( mean_g * mean_g * exp))/(1.0+exp);
var_x_b = (var_y_b - ( mean_b * mean_b * exp))/(1.0+exp);
//qDebug()<<"var_x_r:"<<var_x_r<<"var_x_g:"<<var_x_g<<"var_x_b:"<<var_x_b<<"\n";
//
b_r = var_x_r/var_y_r;
b_g = var_x_g/var_y_g;
b_b = var_x_b/var_y_b;
//qDebug()<<"b_r:"<<b_r<<"b_g:"<<b_g<<"b_b:"<<b_b<<"\n";
//取出原像素值
QRgb oldrgb = oldImage->pixel(i,j);
double temp_r = 0.0,temp_g = 0.0,temp_b = 0.0;
temp_r = qRed(oldrgb) - mean_r;
temp_g = qGreen(oldrgb) - mean_g;
temp_b = qBlue(oldrgb) - mean_b;
double x,y,z;
x = mean_r + (b_r * temp_r) + 0.5;
y = mean_g + (b_g * temp_g) + 0.5;
z = mean_b + (b_b * temp_b) + 0.5;
//qDebug()<<"x:"<<x<<"y:"<<y<<"z:"<<z<<"\n";
result_r = x;
result_g = y;
result_b = z;
//qDebug()<<"result_r:"<<result_r<<"result_g:"<<result_g<<"result_b:"<<result_b<<"\n";
//为新图像每个像素赋值
leeImage->setPixel(i,j,qRgb(result_r,result_g,result_b));
}
}
QLabel *label = new QLabel;
QLabel *label1 = new QLabel;
label1->setPixmap(QPixmap::fromImage(*oldImage));
label1->setWindowTitle("old");
label1->show();
label->resize(leeImage->width(),leeImage->height());
label->setPixmap(QPixmap::fromImage(*leeImage));
label->setWindowTitle("new");
label->show();
return app.exec();
}
以上为我的Qt写的Lee滤波程序,折腾一个星期了,结果始终不对,希望大家帮忙指点指点!谢谢了!