100
Fundamentals of Image Processing & Computer Vision with MATLAB By Ali Ghanbarzadeh [email protected] 1

Fundamentals of Image Processing & Computer Vision with MATLAB

Embed Size (px)

Citation preview

Fundamentals of Image Processing & Computer Vision with MATLAB

By

Ali [email protected]

1

Introduction to MATLABImage ProcessingImage Processing MATLAB ToolboxImage Processing ExamplesComputer VisionComputer Vision MATLAB ToolboxComputer Vision ExamplesApplicationsAlternatives to MATLABOverview2

MATLAB(matrixlaboratory) is amulti-paradigmnumerical computing environment andfourth-generation programming language developed by MathWorks.

MATLAB allowsmatrixmanipulations, plotting offunctionsand data, implementation ofalgorithms, creation ofuser interfaces, and interfacing with programs written in other languages, includingC,C++, Java, Fortran andPython.

Introduction to MATLAB

3

MATLAB User Interface

Command WindowCurrent FolderWorkspace

Introduction to MATLAB

4

>> clc Clears command window

>> help Displays help text in Command Window

>> clear Clears variables and functions from memory.removes all variables from the workspace.

>> clear a variable/variablesIntroduction to MATLAB5

>> verDisplays MathWorks product family header information, followed by the current MATLAB, Simulink and toolbox version information

to write comments use %

>> %This is a comment

Introduction to MATLAB6

Basic Arithmatics ( + , - , * , / )

Variables

every variable is an array or matrix

>> x=10Creates a 1-by-1 matrix named x and stores the value 10 in its element.Once a variable is entered into the system, you can refer to it later.Variables must have values before they are used.Introduction to MATLAB7

When an expression returns a result that is not assigned to any variable, the system assigns it to a variable named ans, which can be used later.

>> 212/23ans = 9.2174

You can use this variable ans

>> ans+5ans = 14.2174Introduction to MATLAB8

who command displays all the variable names you have used>> whoYour variables are:a ans b x

whos command gives more information>> whos Name Size Bytes Class a 1x1 8 double ans 1x1 8 double b 1x1 8 double x 1x1 8 doubleIntroduction to MATLAB9

Constants

>> pians = 3.1416

>> epsans = 2.2204e-16

Introduction to MATLABSymbolic Math Toolbox

>> x=1/3x = 0.3333

>> x=sym(1/3)x = 1/3 >> double(x)ans = 0.3333

10

Trigonometric Functions

>> sin(pi/6)ans = 0.5000

>> cos(0) + tan(pi/4)ans = 2

Introduction to MATLAB

11

>> exp(1)ans = 2.7183

>> log(exp(1))ans = 1

>> log10(1000)ans = 3

Introduction to MATLAB>> 4^3ans = 64

>> sqrt(81)ans = 9

>> nthroot(625,4)ans = 5

>> log2(1024)ans = 10

>> factorial (5)ans = 120

12

>> m(3)ans = 3

>> length(m)ans = 5

>> sum(m)ans = 15

Introduction to MATLAB>> m=[1 2 3 4 5]m = 1 2 3 4 5

>> m'ans = 1 2 3 4 5

>> min(m)ans = 1

>> max(m)ans = 5

>> mean(m)ans = 3Vectors13

>> a.*bans = 3 16 35 60 91

>> p=a.^2p = 1 16 49 100 169

>> sqrt(p)ans = 1 4 7 10 13Introduction to MATLAB

Vectors>> a=1:3:15a = 1 4 7 10 13

>> a(2:5)ans = 4 7 10 13

>> b=(3:7)b = 3 4 5 6 714

>> n(2,3)ans = 4

>> n(3,:)ans = 7 8 6

>> max(n)ans = 7 9 6Introduction to MATLAB

>> min(n)ans = 2 5 1

>> sum(n)ans = 12 22 11

>> sum(sum(n))ans = 45Matrices>> n=[3 5 1;2 9 4;7 8 6]n = 3 5 1 2 9 4 7 8 6

>> n(1:3,2:3)ans = 5 1 9 4 8 6

15

>> diag(n)ans = 3 9 6

>> det(n)ans = 99.0000

Introduction to MATLAB

>> triu(n)ans = 3 5 1 0 9 4 0 0 6

>> tril(n)ans = 3 0 0 2 9 0 7 8 6Matrices>> numel(n)ans = 9

>> size(n)ans = 3 3

>> n'ans = 3 2 7 5 9 8 1 4 6

16

>> (30-20)*(rand(5,1))+20ans = 21.5761 29.7059 29.5717 24.8538 28.0028

>> ones(2)ans = 1 1 1 1

Introduction to MATLAB

>> zeros(3)ans = 0 0 0 0 0 0 0 0 0

>> eye(3)ans = 1 0 0 0 1 0 0 0 1

Matrices>> rand(2)ans = 0.8147 0.1270 0.9058 0.9134

>> rand(3,2)ans = 0.6324 0.5469 0.0975 0.9575 0.2785 0.9649

17

>> std(data)ans = 26.0109>> var(data)ans = 676.5667Introduction to MATLAB

Statistics>> data=[12,54,23,69,31,76]data = 12 54 23 69 31 76>> sort(data)ans = 12 23 31 54 69 76>> median(ans)ans = 42.500018

Functions

>> area_rectangle=inline('2*(a+b)','a','b')area_rectangle = Inline function: area_rectangle(a,b) = 2*(a+b)

>> area_rectangle(5,7)ans = 24

Introduction to MATLABfunction max = mymax(n1, n2, n3)max = n1;if(n2 > max) max = n2;endif(n3 > max) max = n3;enddisp(max);19

Calculus differentiation

>> syms x

>> f=inline('sin(x)+2*cos(x)','x')f = Inline function: f(x) = sin(x)+2*cos(x)

>> diff(f(x),x) ans =cos(x) 2*sin(x)

Introduction to MATLAB>> syms x y

>> f=inline('sin(x)+cos(y)','x','y')f = Inline function: f(x,y) = sin(x)+cos(y)

>> diff(f(x,y),x)ans =cos(x)20

Calculus - integral

>> syms x>> f=inline('x/sin(x^2)','x')f = Inline function: f(x) = x/sin(x^2)

>> integral=int(f(x),x)integral =- log(- x*2*i - x*exp(x^2*i)*2*i)/2 + log(x*2*i - x*exp(x^2*i)*2*i)/2

Introduction to MATLAB

>> pretty(integral) 2 2 log(- x 2 i - x exp(x i) 2 i) log(x 2 i - x exp(x i) 2 i)- ------------------------------ + ---------------------------- 2 221

Calculus integral

>> syms x

>> f=inline('(x^3)+2','x')f = Inline function: f(x) = (x^3)+2 >> integral=int(f(x),x,2,4)integral =64

Introduction to MATLAB

22

Calculus limit

>> syms x

>> f=inline('sin(x)/x','x')f = Inline function: f(x) = sin(x)/x>> limit(f(x),x,0)ans =1

Introduction to MATLAB

>> syms x

>> g=inline('(x+x^2)/((2*x^2)+3*x) ','x')g = Inline function: g(x) = (x+x^2)/((2*x^2)+3*x) >> limit(g(x),x,Inf)ans =1/223

Plotting

>> x=[4 6 8 11 12];>> y=[1 3 3 5 7];>> plot(x,y)>> bar(y)>> pie(x)Introduction to MATLAB24

input

>> a=input('enter value for array: ')enter value for array: [3 4 9 0 2]a = 3 4 9 0 2

Introduction to MATLAB>> name=input('enter your name: ','s')enter your name: Aliname =Ali

>> whos Name Size Bytes Class name 1x19 38 char25

Decisions

>> a = 100; if a < 20 disp('less than 20'); else disp('not less than 20'); end disp(a);

not less than 20 100Introduction to MATLAB>> grade = 'B'; switch(grade) case 'A' disp('A'); case 'B' disp('B'); case 'C' disp('C'); otherwise disp('Invalid grade'); end26

Loops

>> for a = 1.0: -0.1: 0.0 disp(a)end

1 0.9000 0.8000 . 0.1000 0Introduction to MATLAB>> a = 10;while( a < 20 ) disp(a) a = a + 2;end

10 12 14 16 1827

Image Processing is processing of images using mathematical operations by using any form of signal processing for which the input is an image, a series of images, or a video, and the output of image processing may be either an image or a set of characteristics or parameters related to the image.

Image processing usually refers to digital image processing, but optical and analog image processing also are possible.

Closely related to image processing are computer graphics and computer vision. In computer graphics, images are manually made from physical models of objects instead of being acquired via imaging devices. Computer vision is consideredhigh-level image processing out of which a machine/computer/software intends to decipher the physical contents of an image or a sequence of images.Image Processing 28

FeaturesImage Processing Toolbox supports a diverse set of image typesimage analysisimage segmentationimage enhancement noise reductiongeometric transformations

Visualization functions and apps let you explore images and videos,examine a region of pixels, adjust color and contrast,create contours or histograms

Image Processing MATLAB Toolbox

29

The basic data structure in MATLAB is the array.

MATLAB stores most images as two-dimensional arrays (i.e., matrices), in which each element of the matrix corresponds to a single pixel in the displayed image.

Images in MATLAB

30

Some images, such as truecolor images,require a three-dimensional array, wherethe first plane in the third dimensionrepresents the red pixel intensities,the second plane represents the greenpixel intensities, and the third planerepresents the blue pixel intensities.

Images in MATLAB31

I=imread('balls.jpg');imtool(I);

Exploring Images (Measurement)

32

Exploring Images (Inspect Pixels Value)

33

An image histogram is a type of histogram that acts as a graphicalrepresentation of the tonal distribution in a digital image.The horizontal axis of the graph represents the tonal variations, while thevertical axis represents the number of pixels in that particular tone.

Histogram of Image

34

Clc;clear;I = imread('pout.tif'); %reading an imagefigure;subplot(2,2,1);imshow(I); %shows the imagesubplot(2,2,2);imhist(I); %Views the distribution of image pixel intensitiesI2 = histeq(I); %improves the contrast in an image,using the histeq %function.subplot(2,2,3);imshow(I2);subplot(2,2,4);imhist(I2);Enhancement (1)35

imwrite (I2,'pout2.png','png'); %Writes the newly adjusted image to a %file.imfinfo('pout2.png'); %Gives the information about the image

Enhancement (1)

36

clc; clear;I = imread('pout.tif'); %reading an imagefigure;subplot(2,2,1)imshow(I) %shows the imagesubplot(2,2,2)imhist(I) %Views the distribution of image pixel intensities I3=imadjust(I); %Stretches the histogramsubplot(2,2,3)imshow(I3)subplot(2,2,4)imhist(I3)Enhancement (2)37

Enhancement (2)38

histeqBy default, MATLAB transforms the probability distribution to a uniform distribution, which means all the intensity ranges have equivalent probabilities. The histogram is modified to have the counts of all pixels close to each other (uniform distribution)

imadjustimadjust just "Stretches" the histogram of an image in order to have the intensity range of the image fill the entire available range (normally 0-255).

histeq vs. imadjust39

clc; clear;I=imread('cameraman.tif');subplot(1,3,1)imshow(I)title('original') V= I(end:-1:1,:); % Vertical Flipsubplot(1,3,2)imshow(V)title('vertical')

Flip40

H= I(:,end:-1:1); % Horizontal Flipsubplot(1,3,3)imshow(H)title('Horizontal')

Flip

41

scale = 0.5;R = imresize(I,scale); % Resizingfigure; imshow(R)title('Resized') theta = 35;Rotation = imrotate(I,theta); % Rotatingfigure; imshow(Rotation)title('Rotated')Imwrite(Rotation,'r.png','png') % writes image data to the file % specified by filenameRotate, Resize42

Rotate, Resize

43

Thresholding44

The approach for thresholding color images is to designate a separate threshold for each of the RGB components of the image and then combine them with an AND operation.

Thresholding

45

clc; clear;I=imread('balls.jpg'); red=I(:,:,1); %extracts the red channelgreen=I(:,:,2); %extracts the green channelblue=I(:,:,3); %extracts the blue channelfigure;subplot(1,3,1); imshow(red); title('red');subplot(1,3,2); imshow(green); title('green');subplot(1,3,3); imshow(blue); title('blue');

Thresholding46

RGB Channels

Thresholding

47

figure;pix=impixel(I); %the tool for getting pixel information

Thresholding

48

maxPixels=max(pix); %finds the maximum of columnsminPixels=min(pix); %finds the minimum of columns out = red=minPixels(1) &... green=minPixels(2) &... blue= minPixels(3); %trying to thershold the selected segment figure;imshow(out);

Thresholding49

Thresholding50

out2 = imfill(out,'holes'); % filling the holesfigure;imshow(out2);title('without holes'); stats = regionprops(out2); %information about the segment

Thresholding51

Thresholding52

In signal processing, a filter is a device or process that removes from a signal some unwanted component or feature.

fspecial Creates predefined 2-D filter

Filters

53

medfilt2 2-D median filteringThe median filter is a nonlinear digital filtering technique, often used to remove noise.

Filters

54

clc; clear;I=imread('cat.png');figure;imshow(I);title('Original Image'); motion_f=fspecial('motion',10);I2=imfilter(I,motion_f);figure;imshow(I2);title('Blurred Image');

I3=imsharpen(I2,'amount',5);figure;imshow(I3);title('Sharpened Image');

Sharpening55

Sharpening

56

clc; clear;I=imread('mo.jpg');I=rgb2gray(I);figure;imshow(I);Filter Noise

57

N=imnoise(I,'salt & pepper',0.25);figure,imshow(N);

average_f=fspecial('average');Filtered=imfilter(N,average_f);figure,imshow(Filtered);

Filter Noise

58

Filtered=medfilt2(N);figure,imshow(Filtered);

Filter Noise

59

clc; clear;a=imread('shelf.png');b=rgb2gray(a);figure; imshow(a);h=[-1 -1 -1;2 2 2; -1 -1 -1]; %filter for horizontal linesc1=imfilter(b,h);figure; imshow(c1);h=[-1 2 -1;-1 2 -1; -1 2 -1]; %filter for vertical linesc2=imfilter(b,h);figure; imshow(c2);

Line Detection60

Line Detection61

d=imadd(c1,c2); %adding two imagesfigure; imshow(d);

Line Detection

62

clc; clear;a=imread('house.jpg');b=rgb2gray(a); %converts the RGB image to grayscalefigure; imshow(b);

Edge Detection

63

e=edge(b,'sobel'); %edge detection with 'sobel'figure; imshow(e);

Edge Detection

64

e=edge(b,'canny'); %edge detection with 'canny'figure; imshow(e);

Edge Detection

65

h=fspecial('laplacian');e=imfilter(b,h);figure; imshow(e);

Edge Detection

66

Computer vision is a field that includes methods for acquiring, processing, analyzing, and understanding images and, in general, high-dimensional data from the real world in order to produce numerical or symbolic information, e.g., in the forms of decisions.

As a scientific discipline, computer vision is concerned with the theory behind artificial systems that extract information from images. The image data can take many forms, such as video sequences, views from multiple cameras, or multi-dimensional data from a medical scanner. Computer Vision67

Computer Vision System Toolbox provides algorithms, functions, and apps for the design and simulation of computer vision and video processing systems. You can perform object detection and tracking, feature detection and extraction, feature matching, camera calibration, and motion detection tasks.

The system toolbox also provides tools for video processing, including video file I/O, video display, object annotation, drawing graphics.Computer Vision MATLAB Toolbox68

clc; clear;A = imread('circlesBrightDark.png');figure;imshow(A);Rmin = 30;Rmax = 65;[centersBright, radiiBright] = imfindcircles(A,[Rmin Rmax],'ObjectPolarity','bright');[centersDark, radiiDark] = imfindcircles(A,[Rmin Rmax],'ObjectPolarity','dark');figure;imshow(A);viscircles(centersBright, radiiBright,'EdgeColor','b');viscircles(centersDark, radiiDark,'LineStyle','--');

Find Bright & Dark Circles69

Find Bright & Dark Circles

70

Optical character recognition (optical character reader) (OCR) is the mechanical or electronic conversion of images of typed, handwritten or printed text into machine-encoded text.

data entry from printed paper data records, whether passport documents, invoices, bank statements, computerised receipts, business cards, mail, printouts of static-data, or any suitable documentation.

used in machine processes such as machine translation, text-to-speech and text mining.

OCR is a field of research in pattern recognition, artificial intelligence and computer vision.

OCR (Optical Character Recognition)71

clc; clear;

I = imread('ocr_test.png');ocrResults = ocr(I); %OCR for getting the text out of an imagerecognizedText = ocrResults.Text; %the Text fieldfigure;imshow(I);disp(recognizedText) %displays the whole text in Command Window

OCR72

OCR73

boxed = insertObjectAnnotation(I,'rectangle',ocrResults.WordBoundingBoxes, ocrResults.WordConfidences); %showing the boxes around the words with the %relevant word confidencefigure;imshow(boxed)

OCR74

OCRRan ZhangXuemin (Sherman) Shen

Mobile ElectricVehicles

Online Charging and Discharging75

The cascade object detector uses the Viola-Jones algorithm to detect people's faces, noses, eyes, mouth, or upper body.

detector = vision.CascadeObjectDetector creates a System object, detector, that detects objects using the Viola-Jones algorithm. The ClassificationModel property controls the type of object to detect. By default, the detector is configured to detect faces.

BBOX = step(detector,I) returns BBOX, an M-by-4 matrix defining M bounding boxes containing the detected objects. This method performs multiscale object detection on the input image, I. Each row of the output matrix, BBOX, contains a four-element vector, [x y width height], that specifies in pixels, the upper-left corner and size of a bounding box. The input image I, must be a grayscale or truecolor (RGB) image.Cascade Object Detection76

clc; clear; for c=1:7I=imread(num2str(c),'jpg'); detector=vision.CascadeObjectDetector; box = step(detector, I);out = insertObjectAnnotation(I,'rectangle',box,'face');figure; imshow(out);end

Face Detection77

Face Detection78

clc; clear;I1=imread('b2.png');I2=imread('sm.jpg');detector=vision.CascadeObjectDetector('EyePairBig');detector.MergeThreshold=10;box1 = step(detector, I1);box2 = step(detector, I2);out1 = insertObjectAnnotation(I1,'rectangle',box1,'eyes');figure; imshow(out1);out2 = insertObjectAnnotation(I2,'rectangle',box2,'eyes');figure; imshow(out2);

Face Feature Detection(Eyes)79

Face Feature Detection(Eyes)

80

clc; clear;I1=imread('b2.png');I2=imread('sm.jpg');detector=vision.CascadeObjectDetector('Mouth');detector.MergeThreshold=200; box1 = step(detector, I1);box2 = step(detector, I2); out1 = insertObjectAnnotation(I1,'rectangle',box1,'mouth');figure; imshow(out1);out2 = insertObjectAnnotation(I2,'rectangle',box2,'mouth');figure; imshow(out2);

Face Feature Detection(Mouth)81

Face Feature Detection(Mouth)

82

clc; clear;I1=imread('b2.png');I2=imread('sm.jpg');detector=vision.CascadeObjectDetector('Nose');detector.MergeThreshold=50;box1 = step(detector, I1);box2 = step(detector, I2);out1 = insertObjectAnnotation(I1,'rectangle',box1,'nose');figure; imshow(out1);out2 = insertObjectAnnotation(I2,'rectangle',box2,'nose');figure; imshow(out2);

Face Feature Detection(Nose)83

Face Feature Detection(Nose)

84

clc; clear;info = imaqhwinfo('winvideo');info.DeviceInfo(1)info.DeviceInfo.SupportedFormatsvid = videoinput('winvideo',1,'YUY2_640x480');preview(vid)Stop(vid)Video Acquisition85

clc; clear;faceDetector = vision.CascadeObjectDetector();obj =imaq.VideoDevice('winvideo', 1, 'YUY2_640x480','ROI', [1 1 640 480]);set(obj,'ReturnedColorSpace', 'rgb');figure('menubar','none','tag','webcam');Face Detection from live video stream86

while (true) frame=step(obj); bbox=step(faceDetector,frame); boxInserter = vision.ShapeInserter('BorderColor','Custom',... 'CustomBorderColor',[255 255 0]); videoOut = step(boxInserter, frame,bbox); imshow(videoOut,'border','tight'); f=findobj('tag','webcam');

Face Detection from live video stream87

if (isempty(f)); [hueChannel,~,~] = rgb2hsv(frame); hold off noseDetector = vision.CascadeObjectDetector('Nose'); faceImage = imcrop(frame,bbox); noseBBox = step(noseDetector,faceImage); videoInfo = info(obj); ROI=get(obj,'ROI'); VideoSize = [ROI(3) ROI(4)]; tracker = vision.HistogramBasedTracker; initializeObject(tracker, hueChannel, bbox); release(obj); close(gcf) break end % End of if pause(0.05)end % End of While

Face Detection from live video stream88

Pattern Recognition is a branch of machine learning that focuses on the recognition of patterns andregularities in data, although it is in some casesconsidered to be nearly synonymouswith machine learning.

Pattern Recognition algorithms generally aimto provide a reasonable answer for all possibleinputs and to perform "most likely" matching ofthe inputs, taking into account their statistical variation.

Pattern Recognition

89

Biometric Image Processing and Recognition

Applications

90

Biometric Image Processing and Recognition

Applications

91

Medical Imaging

Applications

92

Surveillance

Applications

93

Industrial ControlApplications

94

Robotics

Applications

95

Computer Aided Surgery

Applications

96

Geospatial ComputingApplications

97

Military

Applications

98

MathematicaGNU OctaveR (programming language)ScilabSciPy & NumPy

Alternatives to MATLAB99

Thanks!Any Question?100