查看文章 |
还要有一个坐标平移到中心的操作
//start #include "drawarea.h" #include <qDebug> void show(cmat & src){ int hi = src.size(); int wi = src[0].size(); int i,j; for (i=0;i<hi;i++){ for (j=0;j<wi;j++){ qDebug()<<" ("<<src[i][j].real() <<" , "<<src[i][j].imag()<<")"; } qDebug()<<endl; } qDebug()<<endl; }
cmat mk(int hi,int wi){ int i,j; cmat src; for (i=0;i<hi;i++){ cline line; for (j=0;j<wi;j++){ line.push_back(cpl()); } src.push_back(line); } return src; }
void Drawarea::mkdft(){ QPixmap pix = QPixmap(fname); // pix.scaled(this->size()); QImage src = pix.toImage(); int hi= src.height(); int wi =src.width();
cmat datred= mk(wi,hi); cmat datgr = mk(wi,hi); cmat datbu = mk(wi,hi); int i,j; for (i=0;i<wi;i++) for (j=0;j<hi;j++){//wi hi !!! QRgb tmp = src.pixel(i,j); int reg = qRed(tmp); int green = qGreen(tmp); int blue = qBlue(tmp); datred[i][j]=cpl(reg); datgr[i][j]=cpl(green); datbu[i][j]=cpl(blue); } // ::show(datred); cmat fa = dft2(datred); cmat fb = dft2(datgr); cmat fc = dft2(datbu); qDebug()<<"dft:\n"; // ::show(fa); for (i=0;i<wi;i++) for (j=0;j<hi;j++){ int a=(int)(fa[i][j].real()*150); fix(a); int b=(int)(fb[i][j].real()*150); fix(b); int c=(int)(fc[i][j].real()*150); fix(c); src.setPixel(i,j,qRgb(a,b,c)); } for (i=0;i<wi;i++) for (j=0;j<hi;j++){ img.setPixel((i+wi/2)%wi,(j+hi/2)%hi,src.pixel(i,j)); } this->update(); } cline dft(cline& src){ int len = src.size(); int k,i,j; cline dest; for (k=0;k<len;k++){ cline line; double base = -2.0*PI*k/(double)len;
for (i=0;i<len;i++){ double tmp = base*i; cpl nd = exp(cpl(0,tmp)); line.push_back(nd); } cpl ans; for (i=0;i<len;i++){ ans+=(src[i]*line[i]);
} ans/=cpl(len,0); dest.push_back(ans); } return dest; } cmat dft2(cmat & src){ int hi = src.size(); int wi = src[0].size(); int i,j; cmat tmp = mk(hi,wi); for (i=0;i<hi;i++){ tmp[i]=dft(src[i]); } cmat dest=mk(hi,wi); for (j=0;j<wi;j++){ cline col; for (i=0;i<hi;i++){ col.push_back(tmp[i][j]); } cline dcol=dft(col); for (i=0;i<hi;i++){ dest[i][j]=dcol[i]; } } return dest; }
Drawarea::Drawarea(QWidget * parent):QWidget(parent) { // QLabel * lab =new QLabel("maio",this); fname=""; deta=0; }
void Drawarea::fix(int & dat){ if (dat>COL) dat=COL; if (dat<0) dat=0; } void Drawarea::setfile(QString fin){ fname=fin; QPixmap pix = QPixmap(fname); //pix.scaled(this->size()); img = pix.toImage(); this->update(); } void Drawarea::setdeta(int dat){ deta=dat; this->update(); } void Drawarea::savefile(QString fsave){ this->img.save(fsave); } void Drawarea::paintEvent(QPaintEvent *event){ QPainter g(this); if (fname.isEmpty()){ QFont chfont("楷体",14,72); g.setFont(chfont); g.drawText(this->width()/2-150 , this->height()/2,"please open a image file"); }else{ /* QPixmap pix = QPixmap(fname); pix.scaled(this->size()); img = pix.toImage(); int hi= img.height(); int wi =img.width(); int i,j; for (i=0;i<wi;i++) for (j=0;j<hi;j++){ QRgb tmp = img.pixel(i,j); int reg = qRed(tmp); int green = qGreen(tmp); int blue = qBlue(tmp); reg+=deta; green+=deta; blue+=deta; fix(reg); fix(green); fix(blue); img.setPixel(i,j,qRgb(reg,green,blue)); } */ //g.drawImage(this->rect(),img); g.drawImage(0,0,img); } }
//end |