160 likes | 406 Views
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.
E N D
Computer Vision in Matlablab#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);
Histogram of color image contd.. redPlane = rgbImage(:, :, 1); greenPlane = rgbImage(:, :, 2); bluePlane = rgbImage(:, :, 3); % Let's get its histograms. [pixelCountRgrayLevelsR] = imhist(redPlane); subplot(2, 2, 2); plot(pixelCountR, 'r'); title('Histogram of red plane', 'Fontsize', fontSize); [pixelCountGgrayLevelsG] = imhist(greenPlane); subplot(2, 2, 3); plot(pixelCountG, 'g'); title('Histogram of green plane', 'Fontsize', fontSize); [pixelCountBgrayLevelsB] = imhist(bluePlane); subplot(2, 2, 4); plot(pixelCountB, 'b'); title('Histogram of blue plane', 'Fontsize', fontSize);
Binary images using thresholding % Matlab script for binary image I=imread('trees.tif'); % Read in 1st image T=im2bw(I, 0.1); % perform thresholding subplot(1,3,1), imshow(I); % Display original image subplot(1,3,2), imshow(T); % Display thresholded image
Thresholding contd.. A=imread('toycars1.png'); % Read in 1st image B=imread('toycars2.png'); % Read in 2nd image Abw=im2bw(A); % convert to binary Bbw=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
Thresholding contd.. I=imread('coins.png'); % read in image level = graythresh(I); % get OTSU theshold It = im2bw(I, level); % theshold image imshow(It); % display it
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);
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,[wsws]); end sIM=mIM-IM-C; bw=im2bw(sIM,0); bw=imcomplement(bw);
Image Morphology • Dilation t=imread('text.tif'); % Original Image sq=ones(3,3); % structuring element td=imdilate(t,sq); subplot(1,2,1),imshow(t) subplot(1,2,2),imshow(td)
Image Morphology contd.. • Erosion c=imread('circbw.tif'); %read original image sq=ones(3,3); %structuring element ce=imerode(c,sq); subplot(1,2,1),imshow(c) subplot(1,2,2),imshow(ce)
An application: boundary detection • Assignment #1 Question#1 Write a matlab code for the following
Opening and Closing • Assignment #2 • Question#2 Implement this code in matlab and discuss the results in detail cr=[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)=1 imopen(test,sq) imopen(test,cr) imclose(test,sq) imclose(test,cr)