0% found this document useful (0 votes)
27 views15 pages

Dip Lab Codes

The document outlines a series of experiments related to image processing techniques using MATLAB. These experiments cover various topics such as image filtering, Fourier analysis, histogram equalization, noise reduction, image scaling, and segmentation. Each experiment includes code snippets for reading images, applying transformations, and displaying results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views15 pages

Dip Lab Codes

The document outlines a series of experiments related to image processing techniques using MATLAB. These experiments cover various topics such as image filtering, Fourier analysis, histogram equalization, noise reduction, image scaling, and segmentation. Each experiment includes code snippets for reading images, applying transformations, and displaying results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 15

EXP 2

clc; u=imread(‘C\image.jpg’); oip=rgb2gray(u);

figure;

imshow(oip);

title(‘Gray scale Image’);

uiop=fft2(oip);

figure;

imshow(uiop);

title(‘Fourier analysis image’);

uyiop=ifft2(uiop);

figure;

imshow(uint8(uyiop));

title(‘Inverse Fourier analysis image');

EXP 3

clc;

u=imread(‘c\image.jpg’);

figure;

imshow(u);

title(‘original Image’);

Hm= fspecial('motion',20,45);

MotionBlur = imfilter(u,Hm,'replicate');

figure;

imshow(MotionBlur);
title(‘Motion Blurred Image’);

Hb=fspecial('disk',10);

Blurred = imfilter(u,Hb,'replicate');

figure;

imshow(blurred);

title(‘Filtered Image’);

EXP 4

clc;

u=imread(‘c\image.jpg’);

figure;

imshow(u);

title(‘original Image’);

o=rgb2gray(u);

figure

imshow(o);

title(‘Gray scale Image’);

figure;

imhist(o);

title(‘Histogram before equalization’);

i=histeq(o);

figure;

imshow(i);

title(' Histogram after equalization’);

figure;
imhist(i);

EXP 5

clc;

I = imread('eight.tif');

figure,;

imshow(I);

title(‘Original Image’);

J = imnoise(I,'salt & pepper',0.02);

K = medfilt2(J);

figure;

imshowpair(J,K,'montage');

title(‘Median Filtered Image');

EXP 6

svd(A)

[U,S,V] = svd(A) n= 3;

for i = 1:n for j = 1:n

A(i,j) = sym(1/(i-j+1/2));

end

end

EXP 7

function data = rle(x)


if iscell(x)

i = cumsum([ 1 x{2} ]);

j = zeros(1, i(end)-1);

j(i(1:end-1)) = 1;

data = x{1}(cumsum(j));

else if

size(x,1) > size(x,2), x = x';

end

i = [ find(x(1:end-1) ~= x(2:end)) length(x) ];

data{2} = diff([ 0 i ]);

data{1} = x(i);

end

EXP 8

clc;

I = imread('circuit.tif');

imshow(I)

BW1 = edge(I,'Canny'); BW2 = edge(I,'Prewitt');

imshowpair(BW1,BW2,'montage')

EXP 9

Image Scaling-1

I = imread('rice.png');

J = imresize(I, 0.5);

figure;
imshow(I); title('Original Image'); figure;

imshow(J) ;

title('Resized Image');

Image Scaling-2

I = imread('rice.png');

J = imresize(I, [256 256]);

figure;

imshow(I);

title('Original Image');

figure;

imshow(J);

title('Resized Image');

Image Scaling-3

I = imread('rice.png');

J = imresize(I, 0.5, 'nearest');

figure;

imshow(I);

title('Original Image')

figure;

imshow(J);

title('Resized Image Using Nearest-Neighbor');

I = imread('cameraman.tif');

figure;

imshow(I);

title('Original Image');
J = imtranslate(I,[15, 25]);

figure;

imshow(J);

title('Translated Image');

K = imtranslate(I,[15, 25],'OutputView','full');

figure;

imshow(K);

title('Translated Image, Unclipped')

RGB to HSV

Ima = imread('board.tif');

Imshow(Ima)

Hsv-Im=rgb2hsv(Ima); Imshow(Hsv-Im);

HSV to RGB

RGB=hsv2rgb(HSV-Im)

EXP 10

clc;

i=imread('image22.jpg');

sX=size(i);

j=rgb2gray(i)

[LL,LH,HL,HH]=dwt2(j,'db1');

figure(1);

subplot(2,2,1);

imshow(LL);
title('LL band of image');

subplot(2,2,2);

imshow(LH);

title('LH band of image');

subplot(2,2,3);

imshow(HL);

title('HL band of image');

subplot(2,2,4);

imshow(HH);

title('HH band of image');

X = idwt2(LL,LH,HL,HH,'db1',sX);

figure(2)

imshow(X);

EXP 11

clc; clear all;

I = imread('cameraman.tif');

I1=I;

[row coln]= size(I);

I= double(I);

I = I - (128*ones(256));

quality = input('What quality of compression you require - ');

Q50=[ 16 11 10 16 24 40 51 61;

12 12 14 19 26 58 60 55;

14 13 16 24 40 57 69 56;
14 17 22 29 51 87 80 62;

18 22 37 56 68 109 103 77;

24 35 55 64 81 104 113 92;

49 64 78 87 103 121 120 101;

72 92 95 98 112 100 103 99];

if quality > 50

QX = round(Q50.(ones(8)((100-quality)/50)));

QX = uint8(QX);

elseif quality < 50

QX = round(Q50.(ones(8)(50/quality)));

QX = uint8(QX);

elseif quality = = 50 QX = Q50;

end

DCT_matrix8 = dct(eye(8));

iDCT_matrix8 = DCT_matrix8';

dct_restored = zeros(row,coln);

QX = double(QX);

for i1=[1:8:row]

for i2=[1:8:coln]

zBLOCK=I(i1:i1+7,i2:i2+7);

win1=DCT_matrix8*zBLOCK*iDCT_matrix8; dct_domain(i1:i1+7,i2:i2+7)=win1;

end

end

for i1=[1:8:row]

for i2=[1:8:coln]
win1 = dct_domain(i1:i1+7,i2:i2+7); win2=round(win1./QX); dct_quantized(i1:i1+7,i2:i2+7)=win2;

end

end

for i1=[1:8:row]

for i2=[1:8:coln]

win2= dct_quantized(i1:i1+7,i2:i2+7); win3 = win2.*QX; dct_dequantized(i1:i1+7,i2:i2+7) = win3;

end

end

for i1=[1:8:row]

for i2=[1:8:coln]

win3 = dct_dequantized(i1:i1+7,i2:i2+7); win4=iDCT_matrix8*win3*DCT_matrix8;


dct_restored(i1:i1+7,i2:i2+7)=win4;

end

end

I2=dct_restored;

K=mat2gray(I2);

figure(1);

imshow(I1);

title('original image'); figure(2);

imshow(K);

title('restored image from dct');

EXP 12

if(exist('reg_maxdist','var')==0), reg_maxdist=0.2;

end if(exist('y','var')==0), figure, imshow(I,[]);

[y,x]=getpts; y=round(y(1)); x=round(x(1));


end

J = zeros(size(I));

Isizes = size(I);

reg_mean = I(x,y);

reg_size = 1;

neg_free = 10000;

neg_pos=0;

neg_list = zeros(neg_free,3);

pixdist=0;

neigb=[-1 0; 1 0; 0 -1;0 1];

while(pixdist<reg_maxdist&&reg_size<numel(I))

for j=1:4,

xn = x +neigb(j,1); yn = y +neigb(j,2);

ins=(xn>=1)&&(yn>=1)&&(xn<=Isizes(1))&&(yn<=Isizes();

if(ins&&(J(xn,yn)==0))

neg_pos = neg_pos+1;

neg_list(neg_pos,:) = [xn yn I(xn,yn)]; J(xn,yn)=1;

end

end

end

if(neg_pos+10>neg_free), neg_free=neg_free+10000; neg_list((neg_pos+1):neg_free,:)=0;

dist = abs(neg_list(1:neg_pos,3)-reg_mean);if(exist('reg_maxdist','var')==0), reg_maxdist=0.2;


end if(exist('y','var')==0), figure, imshow(I,[]);

[y,x]=getpts; y=round(y(1)); x=round(x(1));

end

J = zeros(size(I));

Isizes = size(I);

reg_mean = I(x,y);

reg_size = 1;

neg_free = 10000;

neg_pos=0;

neg_list = zeros(neg_free,3);

pixdist=0;

neigb=[-1 0; 1 0; 0 -1;0 1];

while(pixdist<reg_maxdist&&reg_size<numel(I))

for j=1:4,

xn = x +neigb(j,1); yn = y +neigb(j,2);

ins=(xn>=1)&&(yn>=1)&&(xn<=Isizes(1))&&(yn<=Isizes();

if(ins&&(J(xn,yn)==0))

neg_pos = neg_pos+1;

neg_list(neg_pos,:) = [xn yn I(xn,yn)]; J(xn,yn)=1;

end

end

end

if(neg_pos+10>neg_free), neg_free=neg_free+10000; neg_list((neg_pos+1):neg_free,:)=0;


dist = abs(neg_list(1:neg_pos,3)-reg_mean);

[pixdist, index] = min(dist); J(x,y)=2; reg_size=reg_size+1;

reg_mean= (reg_mean*reg_size + neg_list(index,3))/(reg_size+1);

x = neg_list(index,1); y = neg_list(index,2);

end

neg_list(index,:)=neg_list(neg_pos,:); neg_pos=neg_pos-1;

J=J>1;

EXP 13

file=aviinfo('movie1.avi');

frm_cnt=file.NumFrames

h = waitbar(0,'Please wait...');

for i=1:frm_cnt

frm(i)=aviread(filename,i);

frm_name=frame2im(frm(i));

frm_name=rgb2gray(frm_name);

filename1=strcat(strcat(num2str(i)),str2); imwrite(frm_name,filename1);

waitbar(i/frm_cnt,h)

end close(h)

EXP 14

myvoice = audiorecorder (8000,24,1); disp ('Start to speak.');

recordblocking (myvoice,3) ;

disp('End of recording') ;
play (myvoice);

voice_vector = getaudiodata(myvoice);

figure(1)

plot (voice_vector)

title('Own Voice in Time Domain');

EXP 16

erosion

clc;

po=imgetfile;

I = imread(po);

originalBW = I;

se = strel('disk',11);

erodedBW = imerode(originalBW,se);

imshow(originalBW),

figure, imshow(erodedBW);

dilation

clc;

po=imgetfile;

I = imread(po);

se = strel('ball',5,5);

I2 = imdilate(I,se);

imshow(I),

title('Original')

figure, imshow(I2),
title('Dilated')

EXP 17

he = imread('hestain.png');

imshow(he), title('H&E image');

text(size(he,2),size(he,1)+15,...

'Image courtesy of Alan Partin, Johns Hopkins University', ...

'FontSize',7,'HorizontalAlignment','right');

cform = makecform('srgb2lab');

lab_he = applycform(he,cform);

ab = double(lab_he(:,:,2:3));

nrows = size(ab,1);

ncols = size(ab,2);

ab = reshape(ab,nrows*ncols,2);

nColors = 3;

% repeat the clustering 3 times to avoid local minima

[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...

'Replicates',3);

pixel_labels = reshape(cluster_idx,nrows,ncols);

imshow(pixel_labels,[]), title('image labeled by cluster index');

segmented_images = cell(1,3);

rgb_label = repmat(pixel_labels,[1 1 3]);

for k = 1:nColors

color = he;
color(rgb_label ~= k) = 0;

segmented_images{k} = color;

end

imshow(segmented_images{1}), title('objects in cluster 1');

imshow(segmented_images{2}), title('objects in cluster 2');

EXP 18

BW = imread('circles.png');

imshow(BW);

BW2 = bwmorph(BW,'remove');

figure

imshow(BW2);

BW3 = bwmorph(BW,'skel',Inf);

figure

imshow(BW3) ;

BW1 = Array(imread('circles.png'));

figure

imshow(BW1) ;

BW2 = bwmorph(BW1,'remove');

figure

imshow(BW2) ;

BW3 = bwmorph(BW1,'skel',Inf);

figure

imshow(BW3);

You might also like