利用FFTW3生成图像频谱模板

//进行频谱的中心化
void fftshift(Mat source, Mat &dest)
{
	int row = source.rows;
	int col = source.cols;
	int halfRow = row / 2;
	int halfCol = col / 2;
	int temp = 0;

	for (size_t j = 0; j < row; j++)
	{
		temp = (j + halfRow) % row;
		for (size_t i = 0; i < col; i++)
		{
			*dest.ptr<float>(temp, i) = *source.ptr<float>(j, i);
		}
	}

	source = dest.clone();

	for (size_t i = 0; i < col; i++)
	{
		temp = (i + halfCol) % col;
		for (size_t j = 0; j < row; j++)
		{
			*dest.ptr<float>(j, temp) = *source.ptr<float>(j, i);
		}
	}


}

  
  
void test() {  int  i;  fftw_complex*din, *out;  fftw_plan p,backp;
 Mat image = imread("C:\\Users\\Tony\\Desktop\\Image\\lenaCopy.bmp", CV_LOAD_IMAGE_GRAYSCALE);  din = (fftw_complex *)fftw_malloc(sizeof(fftw_complex)*image.cols*image.rows);  out = (fftw_complex *)fftw_malloc(sizeof(fftw_complex)*image.cols*image.rows);
 Mat floatImage(image.size(), CV_32FC1);  image.convertTo(floatImage, CV_32FC1);  
 for (size_t j = 0; j < image.rows; j++)  {   for (size_t i = 0; i < image.cols; i++)   {    din[i + j*image.cols][0] = *floatImage.ptr<float>(j, i);    din[i + j*image.cols][1] = 0;   }  }  //forward fft  p = fftw_plan_dft_2d(image.rows, image.cols, din, out, FFTW_FORWARD, FFTW_ESTIMATE);  fftw_execute(p);
 Mat Resource(image.size(), CV_32FC1);  Mat Imsource(image.size(), CV_32FC1);  for (size_t j = 0; j < image.rows; j++)  {   for (size_t i = 0; i < image.cols; i++)   {    *Resource.ptr<float>(j, i) = out[i + j*image.cols][0];//实部    *Imsource.ptr<float>(j, i) = out[i + j*image.cols][1];//虚部   }  }    Mat Redest(image.size(), CV_32FC1);  Mat Imdest(image.size(), CV_32FC1);  fftshift(Resource, Redest);  fftshift(Imsource, Imdest);    Mat absImage(image.size(), CV_32FC1);  for (size_t j = 0; j < image.rows; j++)  {   for (size_t i = 0; i < image.cols; i++)   {    *absImage.ptr<float>(j, i) = sqrt(pow(*Redest.ptr<float>(j, i), 2) + pow(*Imdest.ptr<float>(j, i), 2));   }  }      absImage += Scalar::all(1);  Mat logImage(image.size(), CV_32FC1);  log(absImage, logImage);  //SaveData(logImage);  double min = 0.0, max = 0.0;  minMaxLoc(logImage, &min, &max);  double scale = 255/(max-min);  double shift = -min*scale;  Mat myImage;  convertScaleAbs(logImage, myImage, scale, shift);  //imshow("123", myImage);  imwrite("C:\\Users\\Tony\\Desktop\\lenaCopy.bmp", myImage); }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值