[visionlist] re: phase spectrum scrambling

nprins nprins at olemiss.edu
Fri Jun 1 02:24:53 GMT 2007


Hi Mariya (and others interested),

I’d like to add the following phase scrambling Matlab code
which will generate a different result compared to the code
listed earlier today by Steven Dakin. Depending on one’s
needs one might want to consider the following code.
Steven’s code randomizes the phase of the r, g and b
layers of an image individually. This changes the color
composition of the image significantly. For example, an rgb
(mxnx3) image of a black-and-white sine wave grating will
after scrambling contain 3 superimposed sine waves (one red,
one green, one blue) in random relative phase (more
generally: any grayscale image will after scrambling be
multi-colored and all rgb images will have a different color
composition compared to original image). In case this is not
the desired result, the code provided here adds the same
random phase structure to the existing three (rgb) phase
structures in the original image. As a result, the relative
phases of the r, g, and b layers in the scrambled image will
be identical to their relative phases in the original image
and the color composition of the scrambled image will be as
in the original image. (e.g., a grayscale rgb image will
generate a scrambled image which is also grayscale). RMS
contrast of all three layers in the image will, after
scrambling, be identical to that of the rescaled (0-1)
original image. (This is not the case after the
transformation to the jpg format).

The functions imread, imwrite, and mat2gray require the
image processing toolbox. mat2gray is easily replaced by
custom-written code (all it does is rescale the mxnx3 image
matrix to have values between 0 and 1).

Cheers to Steven for the idea to get a random phase
structure by borrowing it from a noise image. Brilliant. A
number of years ago I wasted many frustating hours writing
code that generated noise that displays Hermitian symmetry.
If anyone’s interested in this code let me know (it will
shave at least 1/1000 of a second of the time to scramble an
image).

The code:

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)

Cheers,

Nick

______________________________________
Nicolaas Prins, Ph.D.
Assistant Professor & Psi Chi Faculty Advisor
Department of Psychology
University of Mississippi
University, MS 38677
Phone: 662-915-1207
Fax: 662-915-5398
e-mail: nprins at olemiss.edu
______________________________________




More information about the visionlist mailing list