// ================= // || Worksheet 7 // || In this example, we are going be performing low/high pass filters. // ================= #include "program.h" using namespace std; using namespace cv; // In this worksheet, we will be performing DFT's and Inverse DFT's int worksheetseven(Mat& src) { // embed our image into a complex image Mat complexImage = embedInComplexImage(src); // and now take the DFT. dft(complexImage, complexImage); // Now, right after we have performed the DFT. we are going to modify the spectrum image. // all of the low frequency data is stored need the corners of the image. //we are going to do a quick swap of the quadrants of our image, so that it is easier to edit. rearrange(complexImage); // let us set all of the low frequency data to zero! Point centerPoint(complexImage.cols/2,complexImage.rows/2 ); // A LOW PASS FILTER: // lets draw a black filled-in cirlce at the center point of the image, of radius 5 pixels. // Scalar(0,0,0) means black float low_pass_circle_radius = 100; circle(complexImage, centerPoint, low_pass_circle_radius, Scalar( 0, 0, 0 ),CV_FILLED); // A HIGH PASS FILTER: // now, we are going to make black everything that is OUTSIDE a circle. // first, loop over all pixels that are in the complexImage. float high_pass_circle_radius = 50.0; // so, we are going to loop over all the rows for(int row = 0; row < complexImage.rows ; row++) { // and loop over each column for( int col = 0; col < complexImage.cols; col++) { // if that pixel is greater than a certain distance from the center, then set it to black. Point pt(col, row); float distance_to_center = sqrt( (col - complexImage.cols/2)*(col - complexImage.cols/2) + (row - complexImage.rows/2)*(row - complexImage.rows/2) ) ; if( distance_to_center > high_pass_circle_radius ) { //complexImage.at>(pt) = complex(0.0,0.0); } } // end of the 'column' for loop } // end of the 'row' for loop // make sure we rearrange it back. rearrange(complexImage); Mat magnitudeImage = computeMagnitudeImage(complexImage); rearrange(magnitudeImage); // Show the result imshow("Input Image" , src ); imshow("Modified Spectrum Magnitude", magnitudeImage); waitKey(); //calculating the IDFT cv::Mat invtransformedImage; // perform the inverse transform of our complex image. cv::dft(complexImage, invtransformedImage, cv::DFT_INVERSE | cv::DFT_REAL_OUTPUT); /// just normalize our image to be safe. normalize(invtransformedImage, invtransformedImage, 0, 1, CV_MINMAX); imshow("Reconstructed", invtransformedImage); waitKey(); return 0; }