Esempio n. 1
0
void applyFilter(Mat_<float> src, Mat_<float> output)
{
	int wxOrig = src.cols;
  int wyOrig = src.rows;

	int m = cv::getOptimalDFTSize(2*wyOrig);
	int n = cv::getOptimalDFTSize(2*wxOrig);

  copyMakeBorder(src, src, 0, m - wyOrig, 0, n - wxOrig, cv::BORDER_CONSTANT, cv::Scalar::all(0));

	const int wx = src.cols, wy = src.rows;
  const int cx = wx/2, cy = wy/2;

	std::cout << wxOrig << " " << wyOrig << std::endl;
	std::cout << wx << " " << wy << std::endl;
	std::cout << cx << " " << cy << std::endl;

	cv::Mat_<float> imgs[] = {src.clone(), cv::Mat_<float>::zeros(wy, wx)};
  cv::Mat_<cv::Vec2f> img_dft;
  cv::merge(imgs, 2, img_dft);
  cv::dft(img_dft, img_dft);

	dftshift(img_dft);
	cout << "helo " << endl;

  cv::Mat hpf = BHPF(3000, 4, wy, wx, cx, cy);
  cv::mulSpectrums(hpf, img_dft, img_dft, cv::DFT_ROWS);

	dftshift(img_dft);

  cv::idft(img_dft, img_dft); //the result is a 2 channel image
	split(img_dft, imgs);
	normalize(imgs[0], output, 0, 1, CV_MINMAX);

	cv::Mat_<float> croppedOutput(output,cv::Rect(0,0,wxOrig,wyOrig));

	output = croppedOutput;

	namedWindow("High-pass filtered input",WINDOW_NORMAL);
	namedWindow("Input",WINDOW_NORMAL);
	cv::imshow("Input", src);
	cv::imshow("High-pass filtered input", croppedOutput);
	imwrite("/home/student/ROVI/VisMand1/build-vis_man_1-Desktop-Debug/output/out.jpg",croppedOutput);
	cout << "lol" << endl;
	cv::waitKey(0);

}
Esempio n. 2
0
int main()
{
  // A gray image
  cv::Mat_<float> img = cv::imread("Image4_1.png", CV_LOAD_IMAGE_GRAYSCALE);
  // Load image
  //     cv::Mat_<float> img = cv::imread(argv[1], cv::IMREAD_GRAYSCALE);
  
  // Get original size
  int wxOrig = img.cols;
  int wyOrig = img.rows;
  
  
  int m = cv::getOptimalDFTSize( 2*wyOrig );
  int n = cv::getOptimalDFTSize( 2*wxOrig );
  
  copyMakeBorder(img, img, 0, m - wyOrig, 0, n - wxOrig, cv::BORDER_CONSTANT, cv::Scalar::all(0));
  
  // Get padded image size
  const int wx = img.cols, wy = img.rows;
  const int cx = wx/2, cy = wy/2;
  
  std::cout << wxOrig << " " << wyOrig << std::endl;
  std::cout << wx << " " << wy << std::endl;
  std::cout << cx << " " << cy << std::endl;
  
  // Compute DFT of image
  cv::Mat_<float> imgs[] = {img.clone(), cv::Mat_<float>::zeros(wy, wx)};
  cv::Mat_<cv::Vec2f> img_dft;
  cv::merge(imgs, 2, img_dft);
  cv::dft(img_dft, img_dft);
  
  // Shift to center
  dftshift(img_dft);
  
  // Used for visualization only
  cv::Mat_<float> magnitude, phase;
  cv::split(img_dft, imgs);
  cv::cartToPolar(imgs[0], imgs[1], magnitude, phase);
  magnitude = magnitude + 1.0f;
  cv::log(magnitude, magnitude);
  cv::normalize(magnitude, magnitude, 0, 1, CV_MINMAX);
  cv::imwrite("img_dft.png", magnitude * 255);
  //             cv::imshow("img_dft", magnitude);
  
  // Create a Butterworth low-pass filter of order n and diameter d0 in the frequency domain
  cv::Mat lpf = BLPF(100, 2, wy, wx, cx, cy);
  cv::Mat bsf = BBSF(2, wy, wx, cx, cy);
  cv::Mat nf = BNF(2, wy, wx, cx, cy);
  // Multiply and shift back
  cv::mulSpectrums(nf, img_dft, img_dft, cv::DFT_ROWS);

  dftshift(img_dft);
  
  //Display high pass filter
  cv::Mat realImg[2];
  cv::split(nf,realImg);
  cv::Mat realNF = realImg[0];
  cv::normalize(realNF, realNF, 0.0, 1.0, CV_MINMAX);
  // 	namedWindow("", cv::WINDOW_NORMAL);
  cv::imwrite("NF.png", realNF);
  
  
  
  //----- Compute IDFT of HPF filtered image
  
  //you can do this
  //cv::idft(img_dft, img_dft); //the result is a 2 channel image
  //Mat output;
  // therefore you split it and get the real one
  //split(img_dft, imgs);
  //normalize(imgs[0], output, 0, 1, CV_MINMAX);
  
  //or you can do like this, then you dont need to split
  cv::Mat_<float> output;
  cv::dft(img_dft, output, cv::DFT_INVERSE| cv::DFT_REAL_OUTPUT);
  cv::Mat_<float> croppedOutput(output,cv::Rect(0,0,wxOrig,wyOrig));
  
  cv::normalize(output, output, 0, 1, CV_MINMAX);
  cv::normalize(img, img, 0.0, 1.0, CV_MINMAX);
  
  // 	namedWindow("", cv::WINDOW_NORMAL);
  //     cv::imshow("Input", img);
  cv::imwrite("out.png", croppedOutput * 255);
  // 	namedWindow("", cv::WINDOW_NORMAL);
  //     cv::imshow("High-pass filtered input", croppedOutput);
  cv::waitKey();
  return 0;
  return 0;
}