matlab中的scramble函数,傅立叶相位如何在Matlab中对灰度JPEG图像进行加扰?(How can I Fourier phase scramble a grayscale JPEG i...

傅立叶相位如何在Matlab中对灰度JPEG图像进行加扰?(How can I Fourier phase scramble a grayscale JPEG image in Matlab?)

我需要傅立叶相位在Matlab中加载一整套JPEG图像。

使用以下脚本我可以对彩色图像进行相位加扰,但是当我在灰度上尝试它时它不起作用。 我已经在Matlab中安装了图像处理。

关于如何修改代码使其相位加扰灰度的任何建议都会非常感激,因为我已经尝试将ImSize从3减少到2,并将(:,:,图层)减少到(:,图层) - 但仍然没有快乐!

提前致谢,

玛丽亚

Im = mat2gray(double(imread('c:\nick\matlab\randomphase\Bear.jpg')));

%read and rescale (0-1) image

ImSize = size(Im);

RandomPhase = angle(fft2(rand(ImSize(1), ImSize(2))));

%generate random phase structure

for layer = 1:ImSize(3)

ImFourier(:,:,layer) = fft2(Im(:,:,layer));

%Fast-Fourier transform

Amp(:,:,layer) = abs(ImFourier(:,:,layer));

%amplitude spectrum

Phase(:,:,layer) = angle(ImFourier(:,:,layer));

%phase spectrum

Phase(:,:,layer) = Phase(:,:,layer) + RandomPhase;

%add random phase to original phase

ImScrambled(:,:,layer) = ifft2(Amp(:,:,layer).*exp(sqrt(-1)*(Phase(:,:,layer))));

%combine Amp and Phase then perform inverse Fourier

end

ImScrambled = real(ImScrambled); %get rid of imaginery part in image (due to rounding error)

imwrite(ImScrambled,'BearScrambled.jpg','jpg');

imshow(ImScrambled)

I need to Fourier phase scramble a whole load of JPEG images in Matlab.

Using the following script I am able to phase scramble colour images, but when I try it on grayscale it does not work. I have installed the image processing in Matlab already.

Any advice on how to modify code so that it phase scrambles grayscale would be much appreciated, as I have tried reducing ImSize from 3 to 2, and reducing (:,:,layer) to (:,layer) - but still no joy!

Thanks in advance,

Maria

Im = mat2gray(double(imread('c:\nick\matlab\randomphase\Bear.jpg')));

%read and rescale (0-1) image

ImSize = size(Im);

RandomPhase = angle(fft2(rand(ImSize(1), ImSize(2))));

%generate random phase structure

for layer = 1:ImSize(3)

ImFourier(:,:,layer) = fft2(Im(:,:,layer));

%Fast-Fourier transform

Amp(:,:,layer) = abs(ImFourier(:,:,layer));

%amplitude spectrum

Phase(:,:,layer) = angle(ImFourier(:,:,layer));

%phase spectrum

Phase(:,:,layer) = Phase(:,:,layer) + RandomPhase;

%add random phase to original phase

ImScrambled(:,:,layer) = ifft2(Amp(:,:,layer).*exp(sqrt(-1)*(Phase(:,:,layer))));

%combine Amp and Phase then perform inverse Fourier

end

ImScrambled = real(ImScrambled); %get rid of imaginery part in image (due to rounding error)

imwrite(ImScrambled,'BearScrambled.jpg','jpg');

imshow(ImScrambled)

原文:https://stackoverflow.com/questions/32668867

更新时间:2019-12-05 10:57

最满意答案

在您的代码中,您使用的是ImSize = size(Im); 和ImSize(3) ,问题是灰度图像ImSize只有两个元素,因为它是一个2d矩阵。

而是使用返回1的size(Im,3) ,因为matlab总是假定其他单例维度。 出于同样的原因,您的其他代码已经可以分离为灰度图像,因为layer=1 Im(:,:,layer)返回完整图像,这就是您在这种情况下所需的图像。

In your code you are using ImSize = size(Im); and ImSize(3), the problem is for grayscale images ImSize has only two elements because it's a 2d matrix.

Instead use size(Im,3) which returns 1, as matlab always assumes additional singleton dimensions. For the same reason, your further code is already compartible to grayscale images, as Im(:,:,layer) with layer=1 returns the full image, which is what you want in this case.

2015-09-19

相关问答

假设I是你的输入图像, F是它的傅立叶变换(即F = fft2(I) ) 你可以使用这个代码: F = fftshift(F); % Center FFT

F = abs(F); % Get the magnitude

F = log(F+1); % Use log, for perceptual scaling, and +1 since log(0) is undefined

F = mat2gray(F); % Use mat2gray to scale the image between

...

您需要一个与F和0相位相同的矩阵,另一个与F具有相同的相位和均匀的幅度。 正如你所指出的, abs给你的幅度。 为了得到均匀大小的相同相位矩阵,需要用angle来获得相位,然后将相位分离回实部和虚部。 > F_Mag = abs(F); %# has same magnitude as F, 0 phase

> F_Phase = cos(angle(F)) + j*(sin(angle(F)); %# has magnitude 1, same phase as F

> I_Mag = ifft

...

在您的代码中,您使用的是ImSize = size(Im); 和ImSize(3) ,问题是灰度图像ImSize只有两个元素,因为它是一个2d矩阵。 而是使用返回1的size(Im,3) ,因为matlab总是假定其他单例维度。 出于同样的原因,您的其他代码已经可以分离为灰度图像,因为layer=1 Im(:,:,layer)返回完整图像,这就是您在这种情况下所需的图像。 In your code you are using ImSize = size(Im); and ImSize(3), th

...

如果你绘制你的new_image ,你会发现它不是一个正弦曲线: 下面是一个蛮力方法来创建一个正弦曲线模式而不使用复数: # create a sinusoid

F=4 ## select the frequency -- use an even integer to minimize spectral "leakage"

new_image = np.ones([X,Y])

for y in xrange(Y):

for x in xrange(X):

new_image[y]

...

一种简单的方法来扰乱矩阵M: r = rand(size(M));

[~,ri] = sort(r(:));

M(ri) = M;

A simple way to scramble matrix M: r = rand(size(M));

[~,ri] = sort(r(:));

M(ri) = M;

f1 = real(F);

f2 = imag(F);

magnitude = abs(F);

Fnew=real(ifft2(f0)); % set all phases to zero

figure,imagesc(Fnew)

如果将所有幅度设置为零,则像素均为零。 您可以自己设置两个参数a和b ,并使用imagesc观察real(ifft2(a*f1 + i*b*f2)) f1 = real(F);

f2 = imag(F);

magnitude = abs(F);

Fnew=real(i

...

是的,您可以阅读它,但是,您还必须解码输入以获取原始像素。 您必须读取文件中的标题,一旦开始读取原始数据,就必须应用逆DCT(离散余弦变换)来获取原始像素。 这就是为什么,最好使用外部库来读取图像。 Yes, you can read it but then, you will have to also decode the input to get raw pixels. You will have to read the header in the file, and once you sta

...

由于Matlab中的图像本质上可以被视为(像素)矩阵(与具有3个深度层的RGB相比,灰度的单个深度层),您可以将它们视为这样,在这种情况下,构建分割图像会减少到对矩阵的一些简单操作: 计算图像的灰度矩阵, 并构造一个新的矩阵(即你的分割图像),其中左半部分使用所有3层的灰度图像,而右半部分使用3个RGB层。 例如 imgRGB = imread('peppers.png');

imgGray = rgb2gray(imgRGB);

[r, c, ~] = size(imgRGB);

c = ro

...

您对实部和虚部的计算是不正确的。 我这样做: z = Mag.*cos(Phase) + 1i*Mag.*sin(Phase); % full matrix of complex amplitudes

%Here we get the real part

real_part = real(z);

imag_part = imag(z); % do you need this at all?!

image = ifftshift(real_part); % not sure why you a

...

我更喜欢Lena ,所以我们走了 - %// Indices of specific blocks to be randomized

sp_idx = [2,2;2,3;2,4;2,5;2,6;3,2;3,3;3,4;3,5;3,6;4,2;4,3;4,4;4,5;4,6;

5,3;5,4;5,5;6,3;6,4;6,5;7,3;7,4;7,5];

%// Invite lena to MATLAB workspace and *cut off her right arm*

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值