2017mc303 (Lab 4-7)
2017mc303 (Lab 4-7)
2017mc303 (Lab 4-7)
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.
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,
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')
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');
.
…………………………….
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.
Imadjust command can be used to apply power law transformation or gamma correction.
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 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.
global inp_Image
[F,P] = uigetfile('*.*');
Path=fullfile(P,F);
inp_Image=imread(Path);
axes(handles.axes1);
imshow(inp_Image);
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
Useful Functions:
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.
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 ()