我必须创建一个在频域中过滤灰度图像的功能。 主要问题是结果看起来像盐和胡椒噪音。
主要功能是:
void FiltroFrequenze::dftMia(){
int rowsExtendedImage = image.rows + kernel.rows - 1;
int colsExtendedImage = image.cols + kernel.cols - 1;
Mat extendedImage(rowsExtendedImage, colsExtendedImage, image.type());
Mat extendedKernel(rowsExtendedImage, colsExtendedImage, image.type());
filtered=extendedImage.clone();
espansione(image,kernel,extendedImage);
Mat trasformata (rowsExtendedImage, colsExtendedImage, image.type());
translation(extendedImage,trasformata);
Mat planes[] ={Mat_<float>(trasformata),Mat::zeros(trasformata.size(),CV_32F)};
Mat complexI;
merge(planes,2,complexI);
dft(complexI,complexI);
cout<<"dft fatto"<<endl;
split(complexI,planes);
Mat magI=planes[0];
Mat moltiplicata(rowsExtendedImage, colsExtendedImage, image.type());
switch (type)
{
case 1:
kernelPassaBasso(raggio);
break;
case 2:
kernelPassaAlto(raggio);
break;
}
showImage(kernel,"kernel");
waitKey(0);
multiply(magI,kernel,moltiplicata);
//multiply(complexI,kernel,moltiplicata);
cout<<"moltiplicata"<<endl;
Mat inversa (rowsExtendedImage, colsExtendedImage, image.type());
dft(moltiplicata,inversa,DFT_INVERSE+DFT_SCALE);
split(inversa,planes);
//magnitude(planes[0],planes[1],planes[0]);
inversa=planes[0];
//inversa.convertTo(inversa,image.type());
cout<<"invertita"<<endl;
showImage(inversa,"inversa");
waitKey(0);
translation(inversa,filtered);
//filtered.convertTo(filtered,image.type());
在我使用的频率中翻译空间域中的图像:
void FiltroFrequenze::translation(Mat image, Mat traslatedImage){
for(int x=0;x<image.rows;x++)
{
for(int y=0;y<image.cols;y++)
{
if(image.channels()==1)
{
traslatedImage.at<float>(x,y)=image.at<float>(x,y)*pow(-1,x+y);
}
else
{
for(int k=0;k<image.channels();k++)
{
traslatedImage.at<Vec3b>(x,y)[k]=image.at<Vec3b>(x,y)[k]*pow(-1,x+y);
}
}
}
}
并创建高通和低通内核:
void FiltroFrequenze::kernelPassaBasso(int raggio){
Mat kernelNew(kernel.rows+image.rows-1, kernel.cols+image.cols-1, CV_32FC1);
kernel=kernelNew;
drawCircle(kernel, raggio, 1.0, 0.0);
void FiltroFrequenze :: kernelPassaAlto(int raggio){ showImage(图像 “想象,”);
Mat kernelNew(kernel.rows+image.rows-1, kernel.cols+image.cols-1, CV_32FC1);
kernel=kernelNew.clone();
drawCircle(kernel, raggio, 0.0, 1.0);
void FiltroFrequenze::drawCircle(Mat image, int raggio, float colorCircle, float colorBackground){
int center[]={floor(kernel.rows/2)+1,floor(kernel.cols/2)+1};
for(int y = 0; y < image.rows; y++)
for(int x = 0; x < image.cols; x++)
{
if(pow(x-center[1],2)+pow(y-center[0],2)<pow(raggio,2))
{
image.at<float>(y,x)=colorCircle;
}
else
{
image.at<float>(y,x)=colorBackground;
}
}
感谢您的帮助
答案 0 :(得分:0)
我的怀疑是,你所看到的是由于过滤后的图像被转换成浮动的事实。只有当浮点数在&lt; 0,1&gt;中时,OpenCV才能正确显示浮动图像。范围,因此您必须将结果恢复为8位或正确缩放。