computer vision in matlab lab#3 by engr. muhammad saqib

12
Computer Vision in Matlab lab#3 By Engr. Muhammad Saqib

Upload: kory-may

Post on 17-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Computer Vision in Matlab lab#3 By Engr. Muhammad Saqib

Computer Vision in Matlablab#3By Engr. Muhammad Saqib

Page 2: Computer Vision in Matlab lab#3 By Engr. Muhammad Saqib

Histogram of color image

%% Matlab script for the histogram of color image clc; % Clear the command window.close all; % Close all figures (except those of imtool.)imtool close all; % Close all imtool figures.clear; % Erase all existing variables.workspace; % Make sure the workspace panel is showing.fontSize = 20; % Read in standard MATLAB color demo image.rgbImage = imread('peppers.png');[rows columns numberOfColorBands] = size(rgbImage);subplot(2, 2, 1);imshow(rgbImage, []);title('Original Color Image', 'Fontsize', fontSize);

Page 3: Computer Vision in Matlab lab#3 By Engr. Muhammad Saqib

Histogram of color image contd..redPlane = rgbImage(:, :, 1);greenPlane = rgbImage(:, :, 2);bluePlane = rgbImage(:, :, 3); % Let's get its histograms.[pixelCountR grayLevelsR] = imhist(redPlane);subplot(2, 2, 2);plot(pixelCountR, 'r');title('Histogram of red plane', 'Fontsize', fontSize); [pixelCountG grayLevelsG] = imhist(greenPlane);subplot(2, 2, 3);plot(pixelCountG, 'g');title('Histogram of green plane', 'Fontsize', fontSize); [pixelCountB grayLevelsB] = imhist(bluePlane);subplot(2, 2, 4);plot(pixelCountB, 'b');title('Histogram of blue plane', 'Fontsize', fontSize);

Page 4: Computer Vision in Matlab lab#3 By Engr. Muhammad Saqib

Binary images using thresholding % Matlab script for binary image

I=imread('trees.tif'); % Read in 1st imageT=im2bw(I, 0.1); % perform thresholdingsubplot(1,3,1), imshow(I); % Display original

image subplot(1,3,2), imshow(T); % Display thresholded

image

Page 5: Computer Vision in Matlab lab#3 By Engr. Muhammad Saqib

Thresholding contd.. A=imread('toycars1.png'); % Read in 1st imageB=imread('toycars2.png'); % Read in 2nd imageAbw=im2bw(A); % convert to binaryBbw=im2bw(B); % convert to binary subplot(1,3,1), imshow(Abw); % Display 1st image subplot(1,3,2), imshow(Bbw); % Display 2nd image

Output = xor(Abw, Bbw); % xor images images subplot(1,3,3), imshow(Output); % Display result

Page 6: Computer Vision in Matlab lab#3 By Engr. Muhammad Saqib

Thresholding contd..

I=imread('coins.png'); % read in imagelevel = graythresh(I); % get OTSU thesholdIt = im2bw(I, level); % theshold imageimshow(It); % display it 

Page 7: Computer Vision in Matlab lab#3 By Engr. Muhammad Saqib

Adaptive thresholding in matlab%% Adaptive thresholding  clear;close all;im1=imread('page.png');im2=imread('tshape.png');bwim1=adaptivethreshold(im1,11,0.03,0);bwim2=adaptivethreshold(im2,15,0.02,0);subplot(2,2,1);imshow(im1);subplot(2,2,2);imshow(bwim1);subplot(2,2,3);imshow(im2);subplot(2,2,4);imshow(bwim2);

Page 8: Computer Vision in Matlab lab#3 By Engr. Muhammad Saqib

Function for adaptive threshold

function bw=adaptivethreshold(IM,ws,C,tm)if (nargin<3) error('You must provide the image IM, the window size ws, and C.');elseif (nargin==3) tm=0;elseif (tm~=0 && tm~=1) error('tm must be 0 or 1.');end IM=mat2gray(IM); if tm==0 mIM=imfilter(IM,fspecial('average',ws),'replicate');else mIM=medfilt2(IM,[ws ws]);endsIM=mIM-IM-C;bw=im2bw(sIM,0);bw=imcomplement(bw);

Page 9: Computer Vision in Matlab lab#3 By Engr. Muhammad Saqib

Image Morphology

•Dilationt=imread('text.tif'); % Original Imagesq=ones(3,3); % structuring elementtd=imdilate(t,sq);subplot(1,2,1),imshow(t)subplot(1,2,2),imshow(td)

Page 10: Computer Vision in Matlab lab#3 By Engr. Muhammad Saqib

Image Morphology contd..

•Erosion

c=imread('circbw.tif'); %read original image

sq=ones(3,3); %structuring elementce=imerode(c,sq);subplot(1,2,1),imshow(c)subplot(1,2,2),imshow(ce)

Page 11: Computer Vision in Matlab lab#3 By Engr. Muhammad Saqib

An application: boundary detection

•Assignment #1Question#1Write a matlab code for the following

Page 12: Computer Vision in Matlab lab#3 By Engr. Muhammad Saqib

Opening and Closing• Assignment #2• Question#2 Implement this code in matlab and discuss the

results in detailcr=[0 1 0;1 1 1;0 1 0];sq=ones(3,3);test=zeros(10,10);test(2:6,2:4)=1;test(3:5,6:9)=1;test(

8:9,4:8)=1;test(4,5)=1imopen(test,sq)imopen(test,cr)imclose(test,sq)imclose(test,cr)