How to obtain the Hadamard Transform of an image in matlab - matlab

I have a 128x128 grascale image that i wish to find the Hadamard transform of with Normal Hadamard, sequency, and dyadic ordering.
imdata = imread('origim.png'); %Load image
new = rgb2gray(imdata); %Convert to 2D Grayscale
N = 128;
H = hadamard(N); % Hadamard matrix
y = fwht(new,N,'sequency') %Perform Fast-walsh-hadamard-transform with order 128
imshow(y); %Display image transform
I may be doing it wrong, however y should be the transformed image if i understand the matlab walch transform correctly. When i try runing it i get an error with y = fwht(new,N,'sequency')

Before processing convert the image into double. Then put semicolon (;) at the end of y = fwht(new,N,'sequency'). Then you will get transformed image.
Just try the below code.
imdata = imread('peppers.png'); %Load image
new = rgb2gray(imdata); %Convert to 2D Grayscale
neww = im2double(new);
N = 128;
H = hadamard(N); % Hadamard matrix
y = fwht(neww,N,'sequency'); %Perform Fast-walsh-hadamard-transform with order 128
imshow(y); %Display image transform

Related

How can I linearly index a masked image

I have an image which i applied a mask to
Input_image = double(imread('re1.jpg')); % original image
mask = double(imread(/block_text.bmp')); % text mask
mask = mask ./ max(mask(:));
X = Input_image.*mask; % partially observed image. (matrix with missing elements)
This code gives this image
EDIT: what i mean is that i have the code below and i want to modify to use images in the code above instead of random data.
i.e matrix M becomes my image X. but what i dont know is how to get omega in my case.
m=400, n=500;
M = rand(m,r) * rand(r,n); % randomly generated matrix
I = randi([0 1],m,n);
Omega = find(I);
A = M(Omega);

Manual Fourier transform to blur images with Gaussian PSF giving inverted result

I am trying to blur an image with Gaussian PSF. Instead of inbuilt convolution in MATLAB, I need to use a manual Fourier transformation to perform the convolution.I use the DFT matrix given as:
W = exp(1j*2*pi*[0:n-1]'*[0:n-1]/n)
I get the blurred image as the output but inverted.the code is as follows:
%%%blur a given input using Gaussian PSF and Fourier transform based
%%%convolution
close all
clear
clc
g0 = imread('E:\images\cameraman256.png');%reading the image
figure;imagesc(abs(g0));colormap(gray);axis off;axis equal;title('Image');
g0 = double(g0(:,:,1));
g0 = g0./max(max(g0));
%%%Gaussian PSF generation
%Blur Kernel
ksize = 31;
kernel = zeros(ksize);
%Gaussian Blur
s = 3;
% m = ksize/2;
[X, Y] = meshgrid(1:ksize);
kernel = (1/(2*pi*s^2))*exp(-((X).^2 + (Y).^2)/(2*s^2));
%Display Kernel
figure, imagesc(kernel)
axis square
title('Blur Kernel')
colormap gray
%Embed kernel in image that is size of original image
[h, w] = size(g0);
kernelimage = zeros(h,w);
kernelimage(1:ksize, 1:ksize) = kernel;
n=size(g0,1);
W = exp(1j*2*pi*[0:n-1]'*[0:n-1]/n);
ft = W*g0*W.';
ft_psf = W*kernelimage*W.';
convol = W*(ft.*ft_psf)*W.';
figure;imagesc(abs(convol));colormap(gray);axis off;axis equal;title('Blurred Image');
The DFT matrix implementation has been done according to the source found on the following link:
https://scicomp.stackexchange.com/questions/6949/how-can-a-2-d-fft-be-constructed-to-an-equivalent-matrix
Input Image
Blurred Image

Converting code to take RGB image instead of grayscale

I have this code converting a fisheye image into rectangular form but the code is only able to perform this operation on a grayscale image. Can anybody help converting the code to perform the operation on a RGB image. The code is as follows:
edit: I have updated the code to contain a functionality which performs interpolation in each color channel. But this seem to disform the output image. See pictures below
function imP = FISHCOLOR (imR)
rMin=0.1;
rMax=1;
[Mr, Nr, Dr] = size(imR); % size of rectangular image
xRc = (Mr+1)/2; % co-ordinates of the center of the image
yRc = (Nr+1)/2;
sx = (Mr-1)/2; % scale factors
sy = (Nr-1)/2;
M=size(imR,1);N=size(imR,2);
dr = (rMax - rMin)/(M-1);
dth = 2*pi/N;
r=rMin:dr:rMin+(M-1)*dr;
th=(0:dth:(N-1)*dth)';
[r,th]=meshgrid(r,th);
x=r.*cos(th);
y=r.*sin(th);
xR = x*sx + xRc;
yR = y*sy + yRc;
imP =zeros(M, N); % initialize the final matrix
for k=1:3 % colors
T = imR(:,:,k);
Ichannel = interp2(T,xR,yR);
imP(:,:,k)= Ichannel; % add k channel
end
SOLVED
Input image <- Image link
Grayscale output, what i would like in color <- Image link
Try changing these three lines:
[Mr Nr] = size(imR); % size of rectangular image
...
imP = zeros(M, N);
...
imP = interp2(imR, xR, yR); %interpolate (imR, xR, yR);
...to these:
[Mr Nr Pr] = size(imR); % size of rectangular image
...
imP = zeros(M, N, Pr);
...
for dim = 1:Pr
imP(:,:,dim) = interp2(imR(:,:,dim), xR, yR); %interpolate (imR, xR, yR);
end

MATLAB Histogram equalization on GIF images

I'm having a bit of trouble understanding how to change a colormap of a grayscale GIF image after performing histogram equalization on the image. The process is perfectly simple with image compression types that don't have an associated colormap, such as JPEG, and I've gotten it to work with grayscale JPEG images.
clear
clc
[I,map] = imread('moon.gif');
h = zeros(256,1); %array value holds number of pixels with same value
hmap = zeros(256,1);
P = zeros(256,1); %probability that pixel intensity will appear in image
Pmap = zeros(256,1);
s = zeros(256,1); %calculated CDF using P
smap = zeros(256,1);
M = size(I,1);
N = size(I,2);
I = double(I);
Inew = double(zeros(M,N));
mapnew = zeros(256,3);
for x = 1:M;
for y = 1:N;
for l = 1:256;
%count pixel intensities and probability
end
end
end
for j = 2:256
for i = 2:j
%calculate CDF of P
end
end
s(1) = P(1);
smap(1) = Pmap(1);
for x = 1:M;
for y = 1:N;
for l = 1:256;
%calculates adjusted CDF and sets it to new image
end
end
end
mapnew = mapnew/256;
Inew = uint8(Inew);
I = uint8(I);
subplot(1,2,1), imshow(Inew,map); %comparing the difference between original map
subplot(1,2,2), imshow(Inew,mapnew); %to'enhanced' colormap, but both turn out poorly
All is fine in terms of the equalization of the actual image, but I'm not sure what to change about the color map. I tried performing the same operations on the colormap that I did with the image, but no dice.
Sorry that I can't post images cause of my low rep, but I'll try and provide all the info I can on request.
Any help would be greatly appreciated.
function J=histeqo(I)
J=I;
[m,n]=size(I);
[h,d]=imhist(I);
ch=cumsum(h); // The cumulative frequency
imagesize=(m*n); // The image size lightsize=size(d,1);// The Lighting range
tr=ch*(lightsize/imagesize); // Adjustment function
for x=1:m
for y=1:n
J(x,y)=tr(I(x,y)+1);
end
end
subplot(1,2,1);imshow(J);
subplot(1,2,2);imhist(J);
end

How to convert an indexed image to rgb image in MATLAB?

I did a gaussian filter and the image become index. I have to use imagesc to show the determine the color difference. How can I convert it to rgb so that I can do further process.
Edited Added some images, top is the 'original image', 'imshow(C)', 'imagesc(C)' respectively. Then I just want the 'C' variable to be like imagesc image. Is it possible??
Edited Here is my coding, see from gaussian onward
% Read Image
rgb = imread('barcode.jpg');
% Resize Image
rgb = imresize(rgb,0.33);
%figure(),imshow(rgb);
% Convert from RGB to Gray
Igray = rgb2gray(rgb);
BW2 = edge(Igray,'canny');
%figure(),imshow(BW2);
% Perform the Hough transform
[H, theta, rho] = hough(BW2);
% Find the peak pt in the Hough transform
peak = houghpeaks(H);
% Find the angle of the bars
barAngle = theta(peak(2));
J = imrotate(rgb,barAngle,'bilinear','crop');
%figure(),imshow(J);
Jgray = double(rgb2gray(J));
% Calculate the Gradients
[dIx, dIy] = gradient(Jgray);
%if min(dIx(:))<= -100 && max(dIx(:))>=100 || min(dIy(:))<=-100 && max(dIy(:))>=100
if barAngle <= 65 && barAngle >=-65 && min(dIx(:))<= -100
B = abs(dIx) - abs(dIy);
else
B = abs(dIy) - abs(dIx);
end
% Low-Pass Filtering
H = fspecial('gaussian', 20, 10);
C = imfilter(B, H);
C = imclearborder(C);
figure(),imshow(C);
figure(),imagesc(C);colorbar;
RGB = ind2rgb(X,map)
RGB is just eye-candy at this point, you can't magically add information that isn't there.
EDIT
In your code, C is a gray-scale image, because B is gray-scale which in terms is caused by the fact that it is composed from gradients dIx and dIy that originate from an image, you yourself make grayscale explicitly with the line Jgray = double(rgb2gray(J));