0% found this document useful (0 votes)
71 views18 pages

Ahmad Hafizh G64120079: A. Radius 60 Order 2 Program Lowpass

The document describes C++ code for implementing low pass and high pass Butterworth filters on images using discrete Fourier transforms. It defines functions for shifting the DFT quadrants, calculating the magnitude spectrum, and creating Butterworth low pass and high pass filters of a given radius and order in the frequency domain. The main function loads an image, applies the DFT, multiplies with the filter, performs the inverse DFT and displays the original, magnitude, and filtered images. The code is run for radius 60 order 2 and radius 30 order 6.

Uploaded by

Ahmad Hafizh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views18 pages

Ahmad Hafizh G64120079: A. Radius 60 Order 2 Program Lowpass

The document describes C++ code for implementing low pass and high pass Butterworth filters on images using discrete Fourier transforms. It defines functions for shifting the DFT quadrants, calculating the magnitude spectrum, and creating Butterworth low pass and high pass filters of a given radius and order in the frequency domain. The main function loads an image, applies the DFT, multiplies with the filter, performs the inverse DFT and displays the original, magnitude, and filtered images. The code is run for radius 60 order 2 and radius 30 order 6.

Uploaded by

Ahmad Hafizh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Ahmad Hafizh

G64120079
a. Radius = 60 order = 2
Program Lowpass
#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace std;
using namespace cv;
void shiftDFT(Mat& fImage){
Mat tmp, q0,q1,q2,q3;
///first crop the image, if it has an odd number of rows coloum
fImage = fImage (Rect(0,0, fImage.cols & -2, fImage.rows & -2));
int cx=fImage.cols/2;
int cy=fImage.rows/2;
///rearrange the quadrant of Fourier image
///so that the origin is at the image center
q0=fImage(Rect(0,0,cx,cy));
q1=fImage(Rect(cx,0,cx,cy));
q2=fImage(Rect(0,cy,cx,cy));
q3=fImage(Rect(cx,cy,cx,cy));
q0.copyTo(tmp);
q3.copyTo(q1);
tmp.copyTo(q3);
q1.copyTo(tmp);
q2.copyTo(q1);
tmp.copyTo(q2);
}
Mat create_spectrum_magnitude_display(Mat &complexImg, bool rearrange){
Mat planes[2];
///compute magnitude spectrum (N.B for display )
///compute log(1+sqrt(Re(DFT(img))**2 + Im(DFT(img))**2))
split(complexImg,planes);
magnitude(planes[0],planes[1],planes[0]);
Mat mag=(planes[0]).clone();
mag+=Scalar::all(1);
log(mag,mag);
if(rearrange){
///rearrange the quadreants
shiftDFT(mag);
}

normalize(mag,mag,0,1,CV_MINMAX);
return mag;
}
void create_butterworth_lowpass_filter(Mat &dft_Filter, int D, int n){
Mat tmp = Mat(dft_Filter.rows , dft_Filter.cols,CV_32F);
Point center=Point(dft_Filter.rows/2,dft_Filter.cols/2);
double radius;
for(int i=0;i<dft_Filter.rows;i++){
for(int j=0;j<dft_Filter.cols;j++){
radius=(double) sqrt(pow((i-center.x),2.0)+pow((double)(j-center.y),2.0));
tmp.at<float>(i,j)=(float) (1/(1+pow((double)(radius/D),(double)(2*n))));
}
}
Mat toMerge[]={tmp,tmp};
merge(toMerge,2,dft_Filter);
}
void create_butterworth_highpass_filter(Mat &dft_Filter, int D, int n){
Mat tmp = Mat(dft_Filter.rows , dft_Filter.cols,CV_32F);
Point center=Point(dft_Filter.rows/2,dft_Filter.cols/2);
double radius;
for(int i=0;i<dft_Filter.rows;i++){
for(int j=0;j<dft_Filter.cols;j++){
radius=(double) sqrt(pow((i-center.x),2.0)+pow((double)(j-center.y),2.0));
tmp.at<float>(i,j)=(float) (1/(1+pow((double)(D/radius),(double)(2*n))));
}
}
Mat toMerge[]={tmp,tmp};
merge(toMerge,2,dft_Filter);
}
int main(int argc, char** argv)
{
Mat img;
Mat imGray;
Mat imgOutput;///image object(s)
Mat padded;///Fourier image object and arrays
Mat complexImg, filter, filterOutput;
Mat planes[2], mag;
int N,M;///fourier image size
int radius=60;///low pass filter parameter
int order=2;///lown pass filter parameter
const string originalName="Input Image (grayscale)";
const string spectrumMagName="Magnitude Image (log transformed)";
const string lowPassName="Butterworth Low pass filtered (grayscale)";
const string filterName="Filter Image ";///window name
img=cvLoadImage("images.jpg",CV_LOAD_IMAGE_GRAYSCALE);

imGray=cvLoadImage("images.jpg",CV_LOAD_IMAGE_GRAYSCALE);
namedWindow(originalName,1);
namedWindow(spectrumMagName, 1);
namedWindow(lowPassName,1);
namedWindow(filterName, 1);
///setup the DFT image size
M=getOptimalDFTSize(img.rows);
N=getOptimalDFTSize(img.cols);
///set up the DFT images
copyMakeBorder(imGray,padded,0,M-imGray.rows,0,NimGray.cols,BORDER_CONSTANT,Scalar::all(0));
planes[0]=Mat_<float>(padded);
planes[1]=Mat::zeros(padded.size(),CV_32F);
merge(planes,2,complexImg);
///do the DFT
dft(complexImg,complexImg);
///construct the filter(same size as complex image)
filter = complexImg.clone();
create_butterworth_lowpass_filter(filter,radius,order);
///apply filter
shiftDFT(complexImg);
mulSpectrums(complexImg,filter,complexImg,0);
shiftDFT(complexImg);
///create magnitude spectrum for display
mag=create_spectrum_magnitude_display(complexImg,true);
///do inverse DFT on filter image
idft(complexImg,complexImg);
///split into planes and extract plane 0 as output
split(complexImg,planes);
normalize(planes[0],imgOutput,0,1,CV_MINMAX);
///do the same with the filter image
split(filter,planes);
normalize(planes[0],filterOutput,0,1,CV_MINMAX);
///display image in window
imshow(originalName,imGray);
imshow(spectrumMagName,mag);
imshow(lowPassName,imgOutput);
imshow(filterName,filterOutput);
cvWaitKey(0);

return 0;
}
Hasil citra :

Program highpass
#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace std;
using namespace cv;
void shiftDFT(Mat& fImage){
Mat tmp, q0,q1,q2,q3;
///first crop the image, if it has an odd number of rows coloum
fImage = fImage (Rect(0,0, fImage.cols & -2, fImage.rows & -2));
int cx=fImage.cols/2;
int cy=fImage.rows/2;
///rearrange the quadrant of Fourier image
///so that the origin is at the image center
q0=fImage(Rect(0,0,cx,cy));
q1=fImage(Rect(cx,0,cx,cy));
q2=fImage(Rect(0,cy,cx,cy));

q3=fImage(Rect(cx,cy,cx,cy));
q0.copyTo(tmp);
q3.copyTo(q1);
tmp.copyTo(q3);
q1.copyTo(tmp);
q2.copyTo(q1);
tmp.copyTo(q2);
}
Mat create_spectrum_magnitude_display(Mat &complexImg, bool rearrange){
Mat planes[2];
///compute magnitude spectrum (N.B for display )
///compute log(1+sqrt(Re(DFT(img))**2 + Im(DFT(img))**2))
split(complexImg,planes);
magnitude(planes[0],planes[1],planes[0]);
Mat mag=(planes[0]).clone();
mag+=Scalar::all(1);
log(mag,mag);
if(rearrange){
///rearrange the quadreants
shiftDFT(mag);
}
normalize(mag,mag,0,1,CV_MINMAX);
return mag;
}
void create_butterworth_lowpass_filter(Mat &dft_Filter, int D, int n){
Mat tmp = Mat(dft_Filter.rows , dft_Filter.cols,CV_32F);
Point center=Point(dft_Filter.rows/2,dft_Filter.cols/2);
double radius;
for(int i=0;i<dft_Filter.rows;i++){
for(int j=0;j<dft_Filter.cols;j++){
radius=(double) sqrt(pow((i-center.x),2.0)+pow((double)(j-center.y),2.0));
tmp.at<float>(i,j)=(float) (1/(1+pow((double)(radius/D),(double)(2*n))));
}
}
Mat toMerge[]={tmp,tmp};
merge(toMerge,2,dft_Filter);
}
void create_butterworth_highpass_filter(Mat &dft_Filter, int D, int n){
Mat tmp = Mat(dft_Filter.rows , dft_Filter.cols,CV_32F);
Point center=Point(dft_Filter.rows/2,dft_Filter.cols/2);
double radius;
for(int i=0;i<dft_Filter.rows;i++){
for(int j=0;j<dft_Filter.cols;j++){

radius=(double) sqrt(pow((i-center.x),2.0)+pow((double)(j-center.y),2.0));
tmp.at<float>(i,j)=(float) (1/(1+pow((double)(D/radius),(double)(2*n))));
}
}
Mat toMerge[]={tmp,tmp};
merge(toMerge,2,dft_Filter);
}
int main(int argc, char** argv)
{
Mat img;
Mat imGray;
Mat imgOutput;///image object(s)
Mat padded;///Fourier image object and arrays
Mat complexImg, filter, filterOutput;
Mat planes[2], mag;
int N,M;///fourier image size
int radius=60;///low pass filter parameter
int order=2;///lown pass filter parameter
const string originalName="Input Image (grayscale)";
const string spectrumMagName="Magnitude Image (log transformed)";
const string highPassName="Butterworth High pass filtered (grayscale)";
const string filterName="Filter Image ";///window name
img=cvLoadImage("images.jpg",CV_LOAD_IMAGE_GRAYSCALE);
imGray=cvLoadImage("images.jpg",CV_LOAD_IMAGE_GRAYSCALE);
namedWindow(originalName,1);
namedWindow(spectrumMagName, 1);
namedWindow(highPassName,1);
namedWindow(filterName, 1);
///setup the DFT image size
M=getOptimalDFTSize(img.rows);
N=getOptimalDFTSize(img.cols);
///set up the DFT images
copyMakeBorder(imGray,padded,0,M-imGray.rows,0,NimGray.cols,BORDER_CONSTANT,Scalar::all(0));
planes[0]=Mat_<float>(padded);
planes[1]=Mat::zeros(padded.size(),CV_32F);
merge(planes,2,complexImg);
///do the DFT
dft(complexImg,complexImg);
///construct the filter(same size as complex image)
filter = complexImg.clone();
create_butterworth_highpass_filter(filter,radius,order);

///apply filter
shiftDFT(complexImg);
mulSpectrums(complexImg,filter,complexImg,0);
shiftDFT(complexImg);
///create magnitude spectrum for display
mag=create_spectrum_magnitude_display(complexImg,true);
///do inverse DFT on filter image
idft(complexImg,complexImg);
///split into planes and extract plane 0 as output
split(complexImg,planes);
normalize(planes[0],imgOutput,0,1,CV_MINMAX);
///do the same with the filter image
split(filter,planes);
normalize(planes[0],filterOutput,0,1,CV_MINMAX);
///display image in window
imshow(originalName,imGray);
imshow(spectrumMagName,mag);
imshow(highPassName,imgOutput);
imshow(filterName,filterOutput);
cvWaitKey(0);
return 0;
}
Hasil citra :

b. Radius = 30 order = 6
Program Lowpass:
#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace std;
using namespace cv;

void shiftDFT(Mat& fImage){


Mat tmp, q0,q1,q2,q3;

///first crop the image, if it has an odd number of rows coloum


fImage = fImage (Rect(0,0, fImage.cols & -2, fImage.rows & -2));

int cx=fImage.cols/2;
int cy=fImage.rows/2;
///rearrange the quadrant of Fourier image
///so that the origin is at the image center
q0=fImage(Rect(0,0,cx,cy));
q1=fImage(Rect(cx,0,cx,cy));
q2=fImage(Rect(0,cy,cx,cy));
q3=fImage(Rect(cx,cy,cx,cy));

q0.copyTo(tmp);
q3.copyTo(q1);
tmp.copyTo(q3);

q1.copyTo(tmp);
q2.copyTo(q1);
tmp.copyTo(q2);
}
Mat create_spectrum_magnitude_display(Mat &complexImg, bool rearrange){
Mat planes[2];
///compute magnitude spectrum (N.B for display )
///compute log(1+sqrt(Re(DFT(img))**2 + Im(DFT(img))**2))
split(complexImg,planes);
magnitude(planes[0],planes[1],planes[0]);
Mat mag=(planes[0]).clone();
mag+=Scalar::all(1);
log(mag,mag);

if(rearrange){
///rearrange the quadreants
shiftDFT(mag);

}
normalize(mag,mag,0,1,CV_MINMAX);
return mag;
}
void create_butterworth_lowpass_filter(Mat &dft_Filter, int D, int n){
Mat tmp = Mat(dft_Filter.rows , dft_Filter.cols,CV_32F);
Point center=Point(dft_Filter.rows/2,dft_Filter.cols/2);

double radius;
for(int i=0;i<dft_Filter.rows;i++){
for(int j=0;j<dft_Filter.cols;j++){
radius=(double) sqrt(pow((i-center.x),2.0)+pow((double)(j-center.y),2.0));
tmp.at<float>(i,j)=(float) (1/(1+pow((double)(radius/D),(double)(2*n))));
}
}
Mat toMerge[]={tmp,tmp};
merge(toMerge,2,dft_Filter);
}

void create_butterworth_highpass_filter(Mat &dft_Filter, int D, int n){


Mat tmp = Mat(dft_Filter.rows , dft_Filter.cols,CV_32F);
Point center=Point(dft_Filter.rows/2,dft_Filter.cols/2);

double radius;
for(int i=0;i<dft_Filter.rows;i++){
for(int j=0;j<dft_Filter.cols;j++){
radius=(double) sqrt(pow((i-center.x),2.0)+pow((double)(j-center.y),2.0));
tmp.at<float>(i,j)=(float) (1/(1+pow((double)(D/radius),(double)(2*n))));
}

}
Mat toMerge[]={tmp,tmp};
merge(toMerge,2,dft_Filter);
}
int main(int argc, char** argv)
{
Mat img;
Mat imGray;
Mat imgOutput;///image object(s)
Mat padded;///Fourier image object and arrays
Mat complexImg, filter, filterOutput;
Mat planes[2], mag;

int N,M;///fourier image size


int radius=30;///low pass filter parameter
int order=6;///lown pass filter parameter

const string originalName="Input Image (grayscale)";


const string spectrumMagName="Magnitude Image (log transformed)";
const string lowPassName="Butterworth Low pass filtered (grayscale)";
const string filterName="Filter Image ";///window name
img=cvLoadImage("images.jpg",CV_LOAD_IMAGE_GRAYSCALE);
imGray=cvLoadImage("images.jpg",CV_LOAD_IMAGE_GRAYSCALE);

namedWindow(originalName,1);
namedWindow(spectrumMagName, 1);
namedWindow(lowPassName,1);
namedWindow(filterName, 1);

///setup the DFT image size


M=getOptimalDFTSize(img.rows);
N=getOptimalDFTSize(img.cols);

///set up the DFT images


copyMakeBorder(imGray,padded,0,M-imGray.rows,0,NimGray.cols,BORDER_CONSTANT,Scalar::all(0));

planes[0]=Mat_<float>(padded);
planes[1]=Mat::zeros(padded.size(),CV_32F);

merge(planes,2,complexImg);
///do the DFT
dft(complexImg,complexImg);
///construct the filter(same size as complex image)
filter = complexImg.clone();
create_butterworth_lowpass_filter(filter,radius,order);

///apply filter
shiftDFT(complexImg);
mulSpectrums(complexImg,filter,complexImg,0);
shiftDFT(complexImg);

///create magnitude spectrum for display


mag=create_spectrum_magnitude_display(complexImg,true);

///do inverse DFT on filter image


idft(complexImg,complexImg);
///split into planes and extract plane 0 as output
split(complexImg,planes);

normalize(planes[0],imgOutput,0,1,CV_MINMAX);
///do the same with the filter image
split(filter,planes);
normalize(planes[0],filterOutput,0,1,CV_MINMAX);

///display image in window


imshow(originalName,imGray);
imshow(spectrumMagName,mag);
imshow(lowPassName,imgOutput);
imshow(filterName,filterOutput);

cvWaitKey(0);
return 0;
}
Hasil citra :

Program Highpass
#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace std;
using namespace cv;
void shiftDFT(Mat& fImage){
Mat tmp, q0,q1,q2,q3;
///first crop the image, if it has an odd number of rows coloum
fImage = fImage (Rect(0,0, fImage.cols & -2, fImage.rows & -2));
int cx=fImage.cols/2;
int cy=fImage.rows/2;
///rearrange the quadrant of Fourier image
///so that the origin is at the image center
q0=fImage(Rect(0,0,cx,cy));
q1=fImage(Rect(cx,0,cx,cy));
q2=fImage(Rect(0,cy,cx,cy));
q3=fImage(Rect(cx,cy,cx,cy));

q0.copyTo(tmp);
q3.copyTo(q1);
tmp.copyTo(q3);
q1.copyTo(tmp);
q2.copyTo(q1);
tmp.copyTo(q2);
}
Mat create_spectrum_magnitude_display(Mat &complexImg, bool rearrange){
Mat planes[2];
///compute magnitude spectrum (N.B for display )
///compute log(1+sqrt(Re(DFT(img))**2 + Im(DFT(img))**2))
split(complexImg,planes);
magnitude(planes[0],planes[1],planes[0]);
Mat mag=(planes[0]).clone();
mag+=Scalar::all(1);
log(mag,mag);
if(rearrange){
///rearrange the quadreants
shiftDFT(mag);
}
normalize(mag,mag,0,1,CV_MINMAX);
return mag;
}
void create_butterworth_lowpass_filter(Mat &dft_Filter, int D, int n){
Mat tmp = Mat(dft_Filter.rows , dft_Filter.cols,CV_32F);
Point center=Point(dft_Filter.rows/2,dft_Filter.cols/2);
double radius;
for(int i=0;i<dft_Filter.rows;i++){
for(int j=0;j<dft_Filter.cols;j++){
radius=(double) sqrt(pow((i-center.x),2.0)+pow((double)(j-center.y),2.0));
tmp.at<float>(i,j)=(float) (1/(1+pow((double)(radius/D),(double)(2*n))));
}
}
Mat toMerge[]={tmp,tmp};
merge(toMerge,2,dft_Filter);
}
void create_butterworth_highpass_filter(Mat &dft_Filter, int D, int n){
Mat tmp = Mat(dft_Filter.rows , dft_Filter.cols,CV_32F);
Point center=Point(dft_Filter.rows/2,dft_Filter.cols/2);
double radius;
for(int i=0;i<dft_Filter.rows;i++){
for(int j=0;j<dft_Filter.cols;j++){
radius=(double) sqrt(pow((i-center.x),2.0)+pow((double)(j-center.y),2.0));
tmp.at<float>(i,j)=(float) (1/(1+pow((double)(D/radius),(double)(2*n))));

}
}
Mat toMerge[]={tmp,tmp};
merge(toMerge,2,dft_Filter);
}
int main(int argc, char** argv)
{
Mat img;
Mat imGray;
Mat imgOutput;///image object(s)
Mat padded;///Fourier image object and arrays
Mat complexImg, filter, filterOutput;
Mat planes[2], mag;
int N,M;///fourier image size
int radius=30;///low pass filter parameter
int order=6;///lown pass filter parameter
const string originalName="Input Image (grayscale)";
const string spectrumMagName="Magnitude Image (log transformed)";
const string highPassName="Butterworth High pass filtered (grayscale)";
const string filterName="Filter Image ";///window name
img=cvLoadImage("images.jpg",CV_LOAD_IMAGE_GRAYSCALE);
imGray=cvLoadImage("images.jpg",CV_LOAD_IMAGE_GRAYSCALE);
namedWindow(originalName,1);
namedWindow(spectrumMagName, 1);
namedWindow(highPassName,1);
namedWindow(filterName, 1);
///setup the DFT image size
M=getOptimalDFTSize(img.rows);
N=getOptimalDFTSize(img.cols);
///set up the DFT images
copyMakeBorder(imGray,padded,0,M-imGray.rows,0,NimGray.cols,BORDER_CONSTANT,Scalar::all(0));
planes[0]=Mat_<float>(padded);
planes[1]=Mat::zeros(padded.size(),CV_32F);
merge(planes,2,complexImg);
///do the DFT
dft(complexImg,complexImg);
///construct the filter(same size as complex image)
filter = complexImg.clone();
create_butterworth_highpass_filter(filter,radius,order);
///apply filter

shiftDFT(complexImg);
mulSpectrums(complexImg,filter,complexImg,0);
shiftDFT(complexImg);
///create magnitude spectrum for display
mag=create_spectrum_magnitude_display(complexImg,true);
///do inverse DFT on filter image
idft(complexImg,complexImg);
///split into planes and extract plane 0 as output
split(complexImg,planes);
normalize(planes[0],imgOutput,0,1,CV_MINMAX);
///do the same with the filter image
split(filter,planes);
normalize(planes[0],filterOutput,0,1,CV_MINMAX);
///display image in window
imshow(originalName,imGray);
imshow(spectrumMagName,mag);
imshow(highPassName,imgOutput);
imshow(filterName,filterOutput);
cvWaitKey(0);
return 0;
}
Hasil citra :

Analisis:
Penggunaan butterworth filter lowpass memberikan hasil blur pada citra, sedangkan
butterworth highpass filter menghasilkan citra tepian (outline). Dengan radius
sebesar 60 dan order 2 menghasilkan citra yang masih tajam walaupun terdapat
blur. Namun dengan radius 30 dan order 6, citra terlihat sangat buram. Apabila
diberi highpass filter pada citra dengan radius 60 dan order tidak memberikan
tepian yang jelas, namun jika digunakan radius 30 dan order 6 memberikan tepian
yang lebih jelas.

You might also like