2017mc303 (Lab 4-7)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

MCT-453: Machine Vision

Implementation of point/pixel operations in Matlab


Lab Session 4:
Objective: This lab session will be about implementing intensity transformation pixel/point based such as
image negative, image scaling, power law and intensity level slicing.

Intensity Transformation in Spatial Domain:


Image Negative:

Image negative can be obtained by inverting intensity values.


g(x,y) = (L-1) – f(x,y)
Here
L is the intensity levels (255 etc)
For Binary Image, perform image negative and show the results

bw = imread('text.png');
bw2 = ???;
imshowpair (bw,bw2,'montage') % imshowpair show two images side by side

For gray scale Image, perform image negative and show the results

>> I = imread('cameraman.tif');
>> J = ???;
imshowpair(I,J,'montage')

Alteratively,
>> imcomplement( image )
Can perform image negative with built in matlab function.

Lab Activity 01:


1. From internet, search out two x-ray images and apply image negative for enhancements purpose
 First Image
%% 1 (i) without built in fnc
cd ('E:\MV images')
a = imread('Xray1.jpg');
L = 256;
J = (L - 1) - a; % J= negative image
imshowpair(a,J,'montage')
title('Original & Negative Image')
%% with built in fnc
cd ('C:\MV images')
I = imread('Xray1.jpg');
J = imcomplement (I);
imshowpair(I,J,'montage')
title('X-ray and its negative')
 2nd Image:

1. cd ('C:\MV images')
2. a = imread('Leg_Xray.jpg');
3. L = 256;
4. J = (L - 1) - a; % J= negative image
5. imshowpair(a,J,'montage')
6. title('Original & Negative Image')
7. %% with built in fnc
8. cd ('C:\MV images')
9. I = imread('Leg_Xray.jpg');
10. J = imcomplement (I);
11. imshowpair(I,J,'montage')
12. title('Leg X-ray and its negative')

2. Search out a binary images from internet, and apply image negative,

%% 2 Without built-in function


cd ('E:\MV images')
a = imread('binary_image.jpg');
subplot(1,2,1),
imshow(a);
title('original image');
L = 256;
J = (L - 1) - a; % J= negative image
subplot(1,2,2),
imshow(J);
title('Negative Image')
%% 2 with built in function
cd ('E:\MV images')
I = imread('binary_image.jpg');
subplot (1,2,1)
imshow(I)
title('original Image')
J= imcomplement (I);
subplot (1,2,2)
imshow(J)
title('Negative Image')

Image Intensity Scaling:


g(x,y) = a. f(x,y)

Steps:
1. Read any image with overall poor illumination such as office_2.jpg
2. Convert image to grayscale (rgb2gray)
3. Map values from 0-255 to 0-1
4. Apply scaling formula, the value of 1 < a > 5
5. Do reverse mapping from 0-1 to 0-255
6. Display both image and observe effects of the value of a and stop increasing value when white details
begin to lost.

I = imread('office_2.jpg');
J = double(rgb2gray(I));
M = J/255;
K = 1.5.*M;
O = uint8(K*255)
imshowpair(J,K, 'montage')

Power Law transformation:

Apply power law transformation to MRI image of human spine (fractured_spine.tif)

All the images to be used below can be download from the link given chapter 3

http://www.imageprocessingplace.com/DIP-3E/dip3e_book_images_downloads.htm
I = imread(fractured_spine.tif');
.

…………………………….

Apply power law transformation to aerial office image (office_2.jpg)

I = imread('office_2.jpg');
J1 = rgb2gray(I);
J = im2double(J1);
K = 2*(J.^0.5);
imshowpair(J, K, 'montage');
Activity 02:

Apply power law transformation to aerial satellite image (washed_out_aerial_image.tif) as shown below,
display all images in a subplot image with appropriate titles and gamma values with shortest code.

Enter Source Code


cd('E:\softcopies\8thsemester\Machinevision\DIP3E_Original_Images_CH03')
I = imread('Fig0309(a)(washed_out_aerial_image).tif');
subplot (2,2,1)
imshow(I)
title('original Satellite image')
J = im2double(I);
K = 1*(J.^3);
subplot (2,2,2)
imshow(K)
title('c=1 & ?=3')
K1 = 1*(J.^4);
subplot (2,2,3)
imshow(K1)
title('c=1 & ?=4')
K2 = 1*(J.^5);
subplot (2,2,4)
imshow(K2)
title('c=1 & ?=5')
Alternatively;

Imadjust command can be used to apply power law transformation or gamma correction.

J = imadjust(i, [ ], [ ], 5); % last argument is gamma value

Imadjust command can be used to apply image negative.


J = imadjust(a,[],[1;0]);

Intensity Level Slicing:


Highlighting a specific range of intensities in an image often is of interest.
Lab Activity 03: Write a m- file which takes a low contrast image as input and apply a intensity-level slicing
using transformation of Fig 3.11(a). Secondly, preserve the region of kidney and blood vessels and dark out all
other regions using transformation similar of Fig3.11(b). Your function file should display original image,
resultant images in one figure as above.
Enter Source Code
%% Activtiy 3
clear all;
clc;
cd ('E:\soft copies\8th semester\Machine vision\DIP3E_Original_Images_CH03')
I=imread('Fig0312(a)(kidney).tif');
%imtool(a)
for a=1:828; %scan row
for b=1:720; %scan column
if (I(a,b)<230&&I(a,b)>=130)
j(a,b)=255;
k(a,b)=I(a,b);
else
j(x,y)=0;
end
end
end
imshow([I,j,k])
MCT-453: Machine Vision
Implementing Spatial Domain filtering for Noisy Images
Lab Session 5
Objective:
We will add different noise to images and apply smoothing filtering and other type of filters be
implement.

Spatial Domain Filtering of Images


Image Noises:
Different noises such as salt and pepper, Gaussian, and speckle can be added to image using imnoise(I, ‘type’,
value)

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


>> J = imnoise(I,'gaussian',0,0.002)
>> J = imnoise(I,'speckle',0.002)
Apply above three noise models to image (eight.tif) with different value parameters and show the results

Image Smoothening Using averaging filter:


Filtered_image = imfilter( image, mask, padding )

Mask generation:
1. Using simple matrix to represent mask
h=1/9*ones(3); for simple averaging 3x3 mask
2. Using fspecial function to generate built in masks
h= fspecial(type, parameters)
For averaging 3x3 mask,
h = fspecial(‘average’,[3 3])
For applying filter to image, use imfilter (image, mask, padding options)
blurred = imfilter(I,h,'replicate');

Apply smoothing filtering to any image(eight.tif) with different filter sizes (5*5,15*15,35*35)
with different padding options zero, replicate, symmetric, periodic
Lab Activity 01: Implementing Averaging filter
Write an function to
1. Read a grayscale image (eight.tif).
2. Add three type of image noises with given values in Section imnoise to generate three
noisy images.
3. Apply averaging filters of sizes 5x5, 9x9, 15x15 with replicate padding on original image
and three noised images
4. Display original image, noisy images and their respective smoothed images with
appropriate titles in a subplot (4*3).
Note: Code should not exceed 20 lines.
Source Code: (15 lines code)
%% Activity # 1
Img_1=imread('eight.tif');
Img_2=imnoise(Img_1,'salt & pepper',0.2);
Img_3=imnoise(Img_1,'gaussian',0,0.002);
Img_4=imnoise(Img_1,'speckle',0.002);
FilterSizes={[1 1] [5 5] [9 9] [15 15] [1 1] [5 5] [9 9] [15 15] [1 1] [5 5] [9
9] [15 15] [1 1] [5 5] [9 9] [15 15]};
Names={'Original Image','5x5 on original','9x9 on original','15x15 on
original','Salt & Pepper Noise','5x5 on S&P Noise','9x9 on S&P Noise','15x15 on
S&P Noise','Gaussian Noise','5x5 on Gaussian Noise','9x9 on Gaussian
Noise','15x15 on Gaussian Noise','Speckle Noise','5x5 on Speckle Noise','9x9 on
Speckle Noise','15x15 on Speckle Noise'};
Images={Img_1 Img_1 Img_1 Img_1 Img_2 Img_2 Img_2 Img_2 Img_3 Img_3 Img_3 Img_3
Img_4 Img_4 Img_4 Img_4};
for i=1:16; % total 16 images
e=fspecial('average',FilterSizes{i});
subplot(4,4,i)
f=imfilter(Images{i},e,'replicate');
imshow(f)
title(Names(i));
end

Lab Activity 02: Smoothing filter implementation from scratch


Write an function to apply an aveaging/smoothing filter from scratch without using built in
matlab function imfilter. Mathematical foundation and filtering mechanics is given below.
Source code:
img= imread('eight.tif');
[row,col]=size(img);
for i=4:1:row-3; % 7 by 3
for j=4:1:col-3;
x=img(i-3:i+3,j-3:j+3);
c=(x)';
c=sort(c);
ave=sum(c)/49;
img(i,j)=ave;
end
end
imshow(img)
3 by 3

Lab Activity 03: Advantage of median filter over averaging filter for noise removal
Write an m file to remove salt & pepper noise from image (circuit board). Apply averaging
filter and median filter
(medfilt2(img, [3, 3])) and display their results. (image in CH03 folder)
Source Code:
Cd('E:\softcopies\8thsemester\Machinevision\DIP3E_Original_Images_CH0)
I=imread('Fig0335(a)(ckt_board_saltpep_prob_pt05).tif');
J= imnoise(I,'salt & pepper',0.02);
subplot(1,3,1);
imshow(J)
title('original image with noise')
% avaerage filtering
h = fspecial('average',[3 3]);
O=imfilter(J,h);
subplot(1,3,2);
imshow (O)
title('Average filtering')
% median filtering
M=medfilt2(J, [3 3])
subplot(1,3,3);
imshow(M)
title('Median filtering')
MCT-453: Machine Vision
Matlab GUI development for machine vision processing tasks
Lab Session 6
Objective:
Matlab guide will be used to develop a graphical user interface (GUI) for vision processing
tasks such a image filtering, edge detection etc.

Activity 01: GUI Development for filters:


In this activity, an Image Filtering Application will be designed in Matlab guide. The user can
browse images from any folder in the computer and can choose various filter types, border
padding options, filter sizes and the filtered image is displayed by pressing Apply button.
Enter Source Code
% --- Executes on button press in pushbutton1.

global inp_Image
[F,P] = uigetfile('*.*');
Path=fullfile(P,F);
inp_Image=imread(Path);
axes(handles.axes1);
imshow(inp_Image);

% --- Executes on button press in pushbutton2.

global inp_Image
M=round(get(handles.slider2,'value'));
K=get(handles.popupmenu1,'value');
switch K
case 1
K=fspecial('average',[M M]);
case 2
K=fspecial('gaussian',[M M]);
case 3
b=Image;
[r c]=size(inp_Image);
for i=2:r-1;
for j=2:c-1;
mat=[inp_Image(i-1,j-1),inp_Image(i-1,j),inp_Image(i-
1,j+1),inp_Image(i,j-1),inp_Image(i,j),inp_Image(i,j+1),inp_Image(i+1,j-
1),inp_Image(i+1,j),inp_Image(i+1,j+1)];
mat=sort(mat);
b(i,j)=mat(5);
end
end
case 4
g=[0 1 0;1 -4 1;0 1 0];
K= fspecial(inp_Image,g);
end
N=get(handles.popupmenu2,'value');
switch N
case 1
N=imfilter(inp_Image,K,'replicate');
case 2
N=imfilter(inp_Image,K,'circular');
case 3
N=imfilter(inp_Image,K);
case 4
N=imfilter(inp_Image,K,'symmetric');
end
axes(handles.axes2)
imshow(N)
Filter options: Average, Gaussian, Laplacian ,Median, etc

Border Padding: Zero , Replicate, Symmetric,Circular

Filter effect: Filter size increment (9*9, 15*15, ……, 25*25)

Useful Functions:

get(handles.popupmenu, ‘value’); axes(handles.axes1); uigetfile(‘.*.’); fullfile(prt1, prt2),


set(handles.axes1,'visible','off')

GUI Video Demo:


https://youtu.be/p3Ph9rE6v7Y

GUI Help file:

GUI Help.pdf
MCT-453: Machine Vision
Template Matching Technique for Object Detection
Lab Session 7
Objective:
Simple object detection by implementing template matching technique using normalized cross
correlation on input image and given template.

Problem Definition:

Suppose that you are a manager Quality Control Cell at a circuit board manufacturing company.
One of your responsibilities is to ensure your subordinates checks the presence of all the IC
components on one thousand boards daily. But this process is laborious and your employee
turnover rate is very high and it results in lower production.

Luckily, you have learned in Machine Vision-453 that cross-correlation can be used for
template matching: a template is multiplied with regions of a larger image to measure how
similar each region is to the template. The template of an IC (temp.jpg) and the image of board
(circuit.jpg) is provided. We will use cross-correlation to find the ICs in the board.

Activity 01: Template Matching:


Write a function(circuit1, temp1) to
 First read an input image (circuit1) and given template image (temp1).
 Apply gray conversion on both images
 Apply Normalized Cross Correlation (normxcorr2) between both images to detect all
instance of template image in input image
 Visualize result of above step using 3D function surf (c)
 Apply local maxima function and some thresholding value to detect only peaks where
correlation has maximum value
 Draw input image along with bounding boxes of template size at each object location
 Display the total number of template objects found on title of figure.

MATLAB CODE:
cd ('E:\soft copies\8th semester\Machine vision\lab 7')
Input_img=imread('Copy of circuit1.PNG');
I1= rgb2gray(Input_img);
imshow(I1);
I=size(I1);
Temp_img=imread('Copy of temp01.png');
I2= rgb2gray(Temp_img);
imshowpair(I1,I2,'montage')
C= normxcorr2(I2,I1);
figure
surf(C); shading flat
[ypeak,xpeak]=find(C>0.75);
y= ypeak; x=xpeak;
for n=1:length(y)-1;
if (abs(ypeak(n)-ypeak(n+1))<5);
y(n)=0;
x(n)=0;
end
end
x=nonzeros(x);
y=nonzeros(y);
%
yoffset= y-size(Temp_img,1);
xoffset= x-size(Temp_img,2);
figure;
imshow(I1);
for n=1:length(y);
A= imrect(gca, [xoffset(n)+1 , yoffset(n)+1 ,
size(Temp_img,2),size(Temp_img,1)]);
end
title(['No of template found =',num2str(n)]);
Result:

Useful Functions:

normxcorr2( Img, tempI ); surf (c); imrect (gca, [x,y ,x1, y1]); findpeaks ()

Video Demo: https://youtu.be/C3o5qhDgR6o

You might also like