matlab-image-program

Lab.1

Write a Program to Read an Image.

% Read RGB image from graphics file.

im = imread('street2.jpg');

% Display image with true aspect ratio

image(im); axis image

% Use ginput to select corner points of a rectangular

% region by pointing and clicking the mouse twice

p = ginput(2);

% Get the x and y corner coordinates as integers

sp(1) = min(floor(p(1)), floor(p(2))); %xmin

sp(2) = min(floor(p(3)), floor(p(4))); %ymin

sp(3) = max(ceil(p(1)), ceil(p(2))); %xmax

sp(4) = max(ceil(p(3)), ceil(p(4))); %ymax

% Index into the original image to create the new image

MM = im(sp(2):sp(4), sp(1): sp(3),:);

% Display the subsetted image with appropriate axis ratio

figure; image(MM); axis image

% Write image to graphics file.

imwrite(MM,'street2_cropped.tif')

Lab.2

Write a program to display an Image.

files = dir(fullfile('C:','Program Files','MATLAB','images\*.dcm'));

for i = 1 : numel(files)

[X, map] = dicomread(fullfile('C:','Program Files','MATLAB','images',files(i).name));

if ~isempty(map)

subplot(2,2,i);

subimage(X,map);

else

subplot(2,2,i);

imshow(X, map);

end

end

Lab-3

Write a program to convert an color image(RGB) to gray scale image.

for k=1:500

Ic=imread(['Image' num2str(k) '.tif']);

Ig=rgb2gray(Ic);

imwrite(Ig,['Image' num2str(k) 'A.tif'],'tif')

end

Lab-4

Write a program to perform rotation for 45 degree on the image.

A=imread('panda2.jpg');

x1=zeros([size(A,1)*size(A,2) 1]);

x2=zeros([size(A,2)*size(A,1) 1]);

%Specify the degree

deg=90;

%Change the image size

C=uint8(zeros([size(A,1) size(A,2) 3 ]));

m=1;

%Find the midpoint

midx=ceil((size(C,1)+1)/2);

midy=ceil((size(C,2)+1)/2);

for i=1:size(A,1)

i1=i-midx;

for j=1:size(A,2)

%convert from cartesian to polar

[t,r]=cart2pol(i1,j-midy);

%Convert from radians to degree and add the degree value

t1=rad2deg(t)+deg;

%Convert from degree to radians

t=deg2rad(t1);

%Convert to Cartesian Co-ordinates

[x,y]=pol2cart(t,r);

x1(m)=round(x+midx);

x2(m)=round(y+midy);

m=m+1;

end

end

%check whether the values are within the image size.

x1(find(x1 < 1))=1;

x2(find(x2 < 1))=1;

n=1;

for i=1:size(A,1)

for j=1:size(A,2)

C(x1(n),x2(n),:)=A(i,j,:);

n=n+1;

end

end

imshow(C);

I got holes in between when I rotated at 45 degrees. So I used ceil , floor and round function to convert the decimals into whole numbers.

45 degrees

Lab-5

Write a program to perform blurring (blue operation) on image.

blue = [0 1; 0 0.7]

Above value represent the true value for blue operation.

So Syntax for blue operation is:-

image(truecolor_image)

axis equal % Display the image using square pixels

image(0 1; 0 0.7)

Lab-6

Write a program to produce noise over the image.

Syntax

J = imnoise(I,type)

J = imnoise(I,type,parameters)

J = imnoise(I,'gaussian',M,V)

J = imnoise(I,'localvar',V)

J = imnoise(I,'localvar',image_intensity,var)

J = imnoise(I,'poisson')

J = imnoise(I,'salt & pepper',d)

J = imnoise(I,'speckle',v)

gpuarrayJ = imnoise(gpuarrayI,___)

Program

= imread('eight.tif');

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

figure, imshow(I)

figure, imshow(J)

Add noise to an image, performing operation on a GPU.

I = gpuArray(imread('eight.tif'));

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

figure, imshow(I);

figure, imshow(J);

ab-7

Write a program to ruduce noise over the image by using filter

1. Read in the image and display it.

2. I = imread('eight.tif');

imshow(I)

3. Add noise to it.

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

figure, imshow(J)

5. Filter the noisy image with an averaging filter and display the results.

6. K = filter2(fspecial('average',3),J)/255;

figure, imshow(K)

7. Now use a median filter to filter the noisy image and display the results. Notice that medfilt2 does a better job of removing noise, with less blurring of edges.

8. L = medfilt2(J,[3 3]);

figure, imshow(L)

Removing Noise By Adaptive Filtering

The wiener2 function applies a Wiener filter (a type of linear filter) to an image adaptively, tailoring

Read in an image. Because the image is a truecolor image, the example converts it to grayscale.

1. RGB = imread('saturn.png');

I = rgb2gray(RGB);

2. J = imnoise(I,'gaussian',0,0.025);

imshow(J)

Portion of the Image with Added Gaussian Noise

3. Remove the noise, using the wiener2 function. Again, the figure only shows a portion of the image

4. K = wiener2(J,[5 5]);

figure, imshow(K)

Portion of the Image with Noise Removed by Wiener Filter

Lab-8

Write a program to perform scaling of 10% over an image

Syntax

B = imresize(A, scale)

gpuarrayB = imresize(gpuarrayA,scale)

B = imresize(A, [numrows numcols])

[Y newmap] = imresize(X, map, scale)

[...] = imresize(...,method)

[...] = imresize(..., parameter, value, ...)

Program

I = imread('rice.png');

J = imresize(I, 0.1); //scale image by 10%

figure, imshow(I), figure, imshow(J)

Shrink image by factor of two, performing the operation on a GPU.

I = im2double(gpuArray(imread('rice.png')));

J = imresize(I, 0.5);

figure, imshow(I), figure, imshow(J)

Shrink by factor of two using nearest-neighbor interpolation. This is the fastest method, but it has the lowest quality.

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

Resize an indexed image

[X, map] = imread('trees.tif');

[Y, newmap] = imresize(X, map, 0.5);

imshow(Y, newmap)

Resize an RGB image to have 64 rows. Let imresize calculate the number of columns necessary to preserve the aspect ratio.

RGB = imread('peppers.png');

RGB2 = imresize(RGB, [64 NaN]);

Resize an RGB image, performing the operation on a GPU.

RGB = gpuArray(im2single(imread('peppers.png')));

RGB2 = imresize(RGB, 2);

Lab-9

Write a program to perform smoothing of an image.

x = linspace(0,100,2^8);

y = cos(x/10)+(x/50).^2 + randn(size(x))/5;

y([70 75 80]) = [5.5 5 6];

[z,s] = smoothn(y); % Regular smoothing

zr = smoothn(y,'robust'); % Robust smoothing

subplot(121), plot(x,y,'r.',x,z)

title('Regular smoothing')

subplot(122), plot(x,y,'r.',x,zr)

title('Robust smoothing')

Lab-10

Write a program to edge of image.

Syntax

BW = edge(I)

gpuarrayBW = edge(gpuarrayI)

BW = edge(I,'sobel')

BW = edge(I,'sobel',thresh)

BW = edge(I,'sobel',thresh,direction)

[BW,thresh] = edge(I,'sobel',...)

BW = edge(I,'prewitt')

BW = edge(I,'prewitt',thresh)

BW = edge(I,'prewitt',thresh,direction)

[BW,thresh] = edge(I,'prewitt',...)

BW = edge(I,'roberts')

BW = edge(I,'roberts',thresh)

[BW,thresh] = edge(I,'roberts',...)

BW = edge(I,'log')

BW = edge(I,'log',thresh)

BW = edge(I,'log',thresh,sigma)

[BW,threshold] = edge(I,'log',...)

BW = edge(I,'zerocross',thresh,h)

[BW,thresh] = edge(I,'zerocross',...)

BW = edge(I,'canny')

BW = edge(I,'canny',thresh)

BW = edge(I,'canny',thresh,sigma)

[BW,threshold] = edge(I,'canny',...)

Program

= imread('circuit.tif');

BW1 = edge(I,'prewitt');

BW2 = edge(I,'canny');

imshow(BW1);

Prewitt Method

figure, imshow(BW2)

Canny Method

Find the edges using the Prewitt method, performing the operation on a GPU.

I = gpuArray(imread('circuit.tif'));

BW = edge(I,'prewitt');

figure, imshow(BW)