Program to demonstrate Arithmetic operations on Digital Images

Read northpole .jpg image, obtain its negative. Display the original image and its negative.

im1=imread('northpole.jpg');
im1N=255-im1; figure();
subplot(121); imshow(im1); title('Original');
subplot(122); imshow(im1N); title('Negative');

Read Fourier.jpg image and apply log transformation and display the Fourier Spectrum.
s=T(r)=clog(1+r)

im2=imread('Fourier.jpg');
im2Log=log10(1+double(im2));
maxV=max(im2Log(:));
im2Log=im2Log/maxV; figure();
subplot(121); imshow(im2); title('Original');
subplot(122); imshow(im2Log); title('Log transformed');

Plot input intensity levels ‘r’ (0:255) verses output intensity levels ‘s’ using power law transformation curves for ?=[ 0.04; 0.10; 0.20; 0.40; 0.67; 1; 1.5; 2.5; 5.0; 10.0;25.0].Scale the output to (0:255).
s=cr^? . When r=0, s= c(r+?)?

gamma=[0.04 0.10 0.20 0.40 0.67 1 1.5 2.5 5.0 10.0 25.0];
figure();
r=1:255;
c=1; epsilon=0.001;
lenr=length(r);
for i=1:length(gamma)
s=c*power(im2double(r),gamma(i));
s=s-min(s); s=s*255/max(s);
text(r(175),s(175),num2str(gamma(i)),'HorizontalAlignment','left' );
plot(s,r);hold all;
axis([0 255 0 255]);
end

Read crt.jpg, a simple linear wedge gray scale image an input to CRT monitor which has intensity to voltage ratio that is a power function with exponents varying from gamma=1.8 to 2.5 and the display system produces an image which is darker than intended. Apply the transformation function with gamma =1/2.5. Display the gamma corrected image.
s = cr^y

r=imread('crt.jpg');
c=1;
y=2.5;
y1=[1.8 2.0 2.2 2.5]; figure();
for i=1:length(y1)
s=c*power(im2double(r),y1(i));
subplot(2,4,i); imshow(s); title(['gamma=',num2str(y1(i))]);
s2=c*power(im2double(r),1/2.5);
subplot(2,4,i+4); imshow(s2); title('gamma corrected');

end
figure();
s=c*im2double(r).^y;
subplot(221); imshow(r); title('Original Image');
subplot(222); imshow(s); title('Gamma = 2.5');
cs=c*im2double(r).^(1/2.5);
subplot(223); imshow(cs); title('Gamma = 1/2.5 ');
cs=c*im2double(cs).^(2.5);
subplot(224); imshow(cs); title('Gamma corrected Image ');

Read mr.jpg image which is a magnetic resonance image with an upper thoracic human spine with a fracture dislocation.
Apply power law transformation with exponents ?=0.6, ?=0.4, ?=0.3.
Display the outputs and comment on the results.

im6=imread('mr.jpg');
c=1.2;
gamma=[0.6 0.4 0.3];
figure();
subplot(141); imshow(im6); title('Original Image');
for i=1:length(gamma)
s=c*power(im2double(im6),gamma(i));
subplot(1,4,i+1);imshow(s); title(['gamma=',num2str(gamma(i))]);
end

Read remote.jpg image which is an aerial image which has washed out appearance. Compression of gray level is required. Apply power law transform with ?=3,4,5.Display and comment on the results.

im7=imread('remote.jpg');
c=1.0;
gamma=[3 4 5]; figure();
subplot(141); imshow(im7); title('Original Image');
for i=1:length(gamma)
s=c*power(im2double(im7),gamma(i));
subplot(1,4,i+1); imshow(s); title(['gamma=',num2str(gamma(i))]);
end

Read contrast.jpg image which is a low contrast image .
1) Apply the transformation such that r1=rmin, S1=0, r2=rmax, S2=L-1.
Plot the transfn function and display the contrast stretched image.
2) Apply thresholding to the original image such that r1= r2=mean
gray level. Display the result.Hint: use builtin function im2bw.

im8=imread('contrast.jpg');
r1=min(im2double(im8(:))); %contrast stretching
r2=max(im2double(im8(:)));
s1=0; s2=255;gamma=0.5;
figure();
rc=imadjust(im2double(im8),[r1;r2],[0;1],gamma);
level=graythresh(im8);
bw=im2bw(im2double(im8),level);
subplot(131); imshow(imresize(im8,[255 255])); title('Original Image');
subplot(132); imshow(imresize(rc,[255 255])); title('Contrast stretched');
subplot(133); imshow(imresize(bw,[255 255])); title('Threshold image');

Read kidney.tif file. Enter lower and upper threshold value from the user. Apply graylevel slicing with and without background. Display the result.

im9=imread('kidney.tif'); figure();
subplot(131); imshow(im9); title('Original Image');
rb=grayslice(im9,20); % without background
rbw=grayslice(im9,255); % with background
subplot(132); imshow(rb); title('W/O background');
subplot(133); imshow(rbw); title('With background');

Read fractal.jpg file. Display individual bits as binary image. Display and comment on the result.
(Hint: use bitget builtin function)

im10=imread('fractal.jpg');
figure();
for i=1:8
s=bitget(uint8(im10),i);
subplot(3,3,i); imshow(uint8(s.*255)); title(['Bit-plane ',num2str(i)]);
end
subplot(3,3,9); imshow(im10); title('Original Image');

Go back to Noise removal in digital images by averaging