Вы находитесь на странице: 1из 92

Lachong University, Faculty of Electromechanical and Electronic

Phan Nhu Quan, PhD


May 2017
Course goals

Provide the background of the digital image processing,


histogram, image enhancement in the spatial and frequency
domain, image restoration, basis edge detection and
advanced edge detection, spatial frequency filtering, detect
and recognize the object
Outline
Unit 1 Read and display the image

Unit 2 Histogram

Unit 3 Processing the RGB image

Unit 4 Change the properties of the RGB image

Unit 5 Add noise to the image

Unit 6 Count the number of object

Unit 7 Edge Detection

Unit 8 Detect and identify the image objects


Unit 1. Read and display the image

What is the digital image? Digital Camera True


image objective

True color image (RGB image: Red channel, Green channel, Blue channel; in general
the bit depth of RGB image about 24)
Each channel houses one pixels matrix (coordinate, brightness level)
Gray image (gray scale) ((R+G+B)/3)->brightness density ->belong to bit depth (8bit,
16bit, 32bit, 64bit) (rgb2gray)
Black and white image (0, 1), 0=black, 1=white
What is the Pixel?
Picture element or picture point is the smallest portion of an image
Digital image processing? change the properties of the picture, resize, make it
more bright or dim, change the characteristic of the picture, enhancement,
Unit 1. Read and display the image

Image processing with matlab


imread function is used to read image from graphics file
Syntax: A=Imread(path\name.jpg); % (png, bmp, tif, jpeg)
Imshow function is used to display image in Handle Graphics figure
Syntax: Imshow(A);
Example: A=imread('D:/quan.jpg');
imshow(A);
uigetfile function is used to open file dialog box, create an browse (or
dialog box), enabling the user select an picture
Syntax: [filename, pathname] = uigetfile({'*.m';'*.slx';'*.mat';'*.*'},'File Selector');

Example:
[filename, pathname] = uigetfile({'*.jpg';'*.png';'*.bmp'; '*.tif';'*.*'},'File Selector');
Unit 1. Read and display the image
Unit 1. Read and display the image
Design the guide
Unit 1. Read and display the image

Determine Values of Individual Pixels in Images?


imshow quan.jpg
pixelvalues = impixel;
Impixel function : when called impixel with no input arguments, impixel
associates itself with the image in the current axes
Select 10 points on the image to read the pixel values

pixelvalues =

6 5 3
227 210 194
214 186 165
184 171 152
236 221 214
232 215 197
140 141 99
203 186 166
162 117 98
92 57 37
Unit 1. Read and display the image

How to get the information of the image?


strcat(path, filename) join two strings together (Concatenate strings)
imfinfo --> Filename; FileModDate; FileSize; Format; FormatVersion; Width;
Height; BitDepth; ColorType
function pushbutton_open_Callback(hObject, eventdata, handles)
[filename path]=uigetfile({'*.jpg';'*.png';'*.bmp';'*.jpeg';'*.tif';'*.*'},'File Selector');
full=strcat(path,filename);
A=imread(full);
inf=imfinfo(full);
%inf.Width;
%inf.Height;
set(handles.edit_width,'string',inf.Width);
set(handles.edit_height,'string',inf.Height);
imshow(A)
Unit 1. Read and display the image
Unit 1. Read and display the image

How to open Webcam?


% Open Webcam
close all; clear all; clc;
camObj = webcam;
% Preview a stream of image frames.
preview(camObj);
% Acquire and display a single image frame.
img = snapshot(camObj);
imshow(img);
How to open Camera?
% open camera, corresponding the type of camera
obj1 = videoinput('winvideo', 1,'YUY2_640x480');
set(obj1,'ReturnedColorSpace','rgb');
src_obj1 = getselectedsource(obj1);
get(src_obj1);
vidRes = get(obj1, 'VideoResolution');
nBands = get(obj1, 'NumberOfBands');
hImage = image( zeros(vidRes(2), vidRes(1), nBands) );
preview(obj1, hImage);
Outline
Unit 1 Read and display the image

Unit 2 Histogram

Unit 3 Processing the RGB image

Unit 4 Change the properties of the RGB image

Unit 5 Add noise to the image

Unit 6 Count the number of object

Unit 7 Edge Detection

Unit 8 Detect and identify the image objects


Unit 2. Histogram

What is histogram?
Image size = 256256=65536 pixels
256 pixels Each cell (one pixel) represents the
brightness density, coordination,
Exam: Bit depth=8bit28=256 levels
Picture 256 pixels Level 0: has 1 pixels
Level 1: has 120 pixels

120 Pixels .
100 Level 255: has 100 pixels
Plot the levels corresponding with
1 pixels histogram
0 1 255 Levels
imhist (A) : imhist function is used to display histogram of image data
Unit 2. Histogram
Design the guide
Unit 2. Histogram
function pushbutton_open_Callback(hObject, eventdata, handles)
global A
[filename, path] = ...
uigetfile({'*.jpg';'*.bmp';'*.png';'*.tif';'*.*'},'File Selector');
if ~isequal(filename,0)
A=imread([path,filename]);
axes(handles.axes1)
imshow(A);
else
return
end
info=imfinfo(fullfile(path,filename));
set(handles.edit_name,'String',filename);
set(handles.edit_size,'String',info.FileSize);
set(handles.edit_width,'String',info.Width);
set(handles.edit_height,'String',info.Height);
set(handles.edit_bit,'String',info.BitDepth);
set(handles.edit_type,'String',info.ColorType);
Unit 2. Histogram
function pushbutton_histogram_Callback(hObject, eventdata, handles)
global A
ColorType=get(handles.edit_type,'String');
figure;
switch ColorType
case 'grayscale'
imhist(A);
title('Histogram of gray image');
case 'truecolor'
subplot(3,1,1)
imhist(A(:,:,1));
title('Histogram of red channel');
subplot(3,1,2)
imhist(A(:,:,2));
title('Histogram of green channel');
subplot(3,1,3)
imhist(A(:,:,3));
title('Histogram of blue channel');
end
Unit 2. Histogram
Unit 2. Histogram
Outline
Unit 1 Read and display the image

Unit 2 Histogram

Unit 3 Processing the RGB image

Unit 4 Change the properties of the RGB image

Unit 5 Add noise to the image

Unit 6 Count the number of object

Unit 7 Edge Detection

Unit 8 Detect and identify the image objects


Unit 3. Processing the RGB image
3.1. Color channel processing

clc;
clear all;
close all;
% image input
[fname path]=uigetfile('*.jpg','Select an Image');
fname=strcat(path,fname);
subplot(3,3,1);
im=imread(fname);
im=imresize(im,[256 256]);
imshow(im);
title('Orignal Image')
Unit 3. Processing the RGB image
3.1. Color channel processing
% analyzing color channel
imR=im;
imR(:,:,2:3)=0;
subplot(3,3,2);
imshow(imR);
title('Red Channel Image')

imG=im;
imG(:,:,1:2:3)=0;
subplot(3,3,3);
imshow(imG);
title('Green Channel Image')

imB=im;
imB(:,:,1:2)=0;
subplot(3,3,4);
imshow(imB);
title('Blue Channel Image')
Unit 3. Processing the RGB image
3.1. Color channel processing
% Gray conversion by Matlab
imGray=rgb2gray(im);
subplot(3,3,5);
imshow(imGray);
title('Gray Image Conversion by Matlab')
% gray conversion by color channel extraction
imGrayR=im(:,:,1);
subplot(3,3,6);
imshow(imGrayR);
title('Gray Image Conversion by Red Channel Separation')
% gray using formula
%imGrayFormula=0.2989*R+0.5870*G+0.1140*B;
imGrayFormula=0.2989*im(:,:,1)+0.5870*im(:,:,2)+0.1140*im(:,:,3);
subplot(3,3,7);
imshow(imGrayFormula);
title('Gray Image Conversion by Formula')
Unit 3. Processing the RGB image
3.1. Color channel processing
% Thresholding
% imGray(find(imGray<150))=0;
% subplot(3,3,8);
% imshow(imGray);
% title('Gray Image Thresholding')
for i=1:3
tmp=im(:,:,1);
tmp(find(tmp<150))=0;
im(:,:,i)=tmp;
end
subplot(3,3,8);
imshow(im);
title('Gray Image Thresholding')
Unit 3. Processing the RGB image
3.1. Color channel processing
Unit 3. Processing the RGB image
3.2. Design the guide
Unit 3. Processing the RGB image
function pushbutton_open_Callback(hObject, eventdata, handles)
global Img
[filename,pathname] = uigetfile({'*.*'});
if ~isequal(filename,0)
Info = imfinfo(fullfile(pathname,filename));
if Info.BitDepth == 24
Img = imread(fullfile(pathname,filename));
axes(handles.axes1)
imshow(Img)
else
msgbox('Select the RGB image');
return
end
else
return
end
guidata(hObject,handles);
set(handles.radiobutton_red,'Enable','on')
set(handles.radiobutton_green,'Enable','on')
set(handles.radiobutton_blue,'Enable','on')
set(handles.radiobutton_gray,'Enable','on')
set(handles.radiobutton_binary,'Enable','on')
set(handles.slider1,'Enable','on')
set(handles.edit1,'Enable','on')
Unit 3. Processing the RGB image
% When radiobutton_red is clicked, Red channel are selected
function radiobutton_red_Callback(hObject, eventdata, handles)
global Img
set(handles.radiobutton_green,'Value',0)
set(handles.radiobutton_blue,'Value',0)
set(handles.radiobutton_gray,'Value',0)
set(handles.radiobutton_binary,'Value',0)
set(handles.slider1,'Value',0)
set(handles.edit1,'String',0)
R = Img(:,:,1);
G = Img(:,:,2);
B = Img(:,:,3);
Red_Channel = cat(3,R,G*0,B*0);
axes(handles.axes1)
cla('reset')
imshow(Red_Channel)
title('Red Channel')
axes(handles.axes2)
imhist(R(:));
grid on
title('Histogram')
Unit 3. Processing the RGB image
% When radiobutton_green is clicked, Red channel are selected
function radiobutton_green_Callback(hObject, eventdata, handles)
global Img
set(handles.radiobutton_red,'Value',0)
set(handles.radiobutton_blue,'Value',0)
set(handles.radiobutton_gray,'Value',0)
set(handles.radiobutton_binary,'Value',0)
set(handles.slider1,'Value',0)
set(handles.edit1,'String',0)
R = Img(:,:,1);
G = Img(:,:,2);
B = Img(:,:,3);
Green_Channel = cat(3,R*0,G,B*0);
axes(handles.axes1)
cla('reset')
imshow(Green_Channel)
title('Green Channel')
axes(handles.axes2)
cla('reset')
imhist(G(:));
grid on
title('Histogram')
Unit 3. Processing the RGB image
% When radiobutton_blue is clicked, Green channel are selected
function radiobutton_blue_Callback(hObject, eventdata, handles)
global Img
set(handles.radiobutton_red,'Value',0)
set(handles.radiobutton_green,'Value',0)
set(handles.radiobutton_gray,'Value',0)
set(handles.radiobutton_binary,'Value',0)
set(handles.slider1,'Value',0)
set(handles.edit1,'String',0)
R = Img(:,:,1);
G = Img(:,:,2);
B = Img(:,:,3);
Blue_Channel = cat(3,R*0,G*0,B);
axes(handles.axes1)
imshow(Blue_Channel)
title('Blue Channel')
axes(handles.axes2)
imhist(B(:));
grid on
title('Histogram')
Unit 3. Processing the RGB image
% When radiobutton_gray is clicked, Blue channel are selected
function radiobutton_gray_Callback(hObject, eventdata, handles)
global Img
set(handles.radiobutton_red,'Value',0)
set(handles.radiobutton_green,'Value',0)
set(handles.radiobutton_blue,'Value',0)
set(handles.radiobutton_binary,'Value',0)
set(handles.slider1,'Value',0)
set(handles.edit1,'String',0)
Gray = rgb2gray(Img);
axes(handles.axes1)
imshow(Gray)
title('Grayscale Image')
axes(handles.axes2)
imhist(Gray(:));
grid on
title('Histogram')
Unit 3. Processing the RGB image
% When radiobutton_binary is clicked, the Grayscale are selected
function radiobutton_binary_Callback(hObject, eventdata, handles)
global Img bw
set(handles.radiobutton_red,'Value',0)
set(handles.radiobutton_green,'Value',0)
set(handles.radiobutton_blue,'Value',0)
set(handles.radiobutton_gray,'Value',0)
set(handles.slider1,'Value',0)
set(handles.edit1,'String',0)
Gray = rgb2gray(Img);
thresh = graythresh(Gray);
bw = im2bw(Gray,thresh);
set(handles.pushbutton_save,'Enable','on')
axes(handles.axes1)
cla('reset')
imshow(Gray)
title('Grayscale Image')
axes(handles.axes2)
cla('reset')
imshow(bw)
title('Binary Image')
set(handles.edit1,'String',thresh)
Unit 3. Processing the RGB image
% When slider1 is clicked, the Otsus method is used to get the threshold
function slider1_Callback(hObject, eventdata, handles)
global Img bw
set(handles.radiobutton_red,'Value',0)
set(handles.radiobutton_green,'Value',0)
set(handles.radiobutton_blue,'Value',0)
set(handles.radiobutton_gray,'Value',0)
set(handles.radiobutton_binary,'Value',0)
Gray = rgb2gray(Img);
thresh = get(hObject,'Value');
bw = im2bw(Gray,thresh);
set(handles.pushbutton_save,'Enable','on')
axes(handles.axes1)
cla('reset')
imshow(Gray)
title('Grayscale Image')
axes(handles.axes2)
cla('reset')
imshow(bw)
title('Binary Image')
set(handles.edit1,'String',num2str(thresh))
Unit 3. Processing the RGB image
% When pushbutton_save is clicked
function pushbutton_save_Callback(hObject, eventdata, handles)
global bw
[filename,pathname]=uiputfile({'*.jpg','JPEG Files(*.jpg)';...
'*.bmp','Bitmap Files(*.bmp)';'*.gif','GIF Files(*.gif)';...
'*.tif','TIFF Files(*.tif)';...
'*.*','all image file'},'Do you want to save the selected image?',Result_Image/');
imwrite(bw,[pathname,filename]);
Unit 3. Processing the RGB image
Unit 3. Processing the RGB image
Unit 3. Processing the RGB image
Unit 3. Processing the RGB image
Unit 3. Processing the RGB image
Unit 3. Processing the RGB image
Unit 3. Processing the RGB image
Outline
Unit 1 Read and display the image

Unit 2 Histogram

Unit 3 Processing the RGB image

Unit 4 Change the properties of the RGB image

Unit 5 Add noise to the image

Unit 6 Count the number of object

Unit 7 Edge Detection

Unit 8 Detect and identify the image objects


Unit 4. Change the properties of the RGB image

4.1. Change the RGB image to the negative image


Unit 4. Change the properties of the RGB image

4.1. Change the RGB image to the negative image


function pushbutton_open_Callback(hObject, eventdata, handles)
global w;

[FileName,PathName] = uigetfile({'*.jpg;*.JPG','JPG Files (*.jpg, *JPG)';


'*.tif;*.TIF','TIF Files (*.tif, *.TIF)';...
'*.gif;*.GIF','GIF Files (*.gif, *.GIF)';...
'*.png;*.PNG','PNG Files (*.png, *.PNG)';...
'*.bmp;*.BMP','BMP Files (*.bmp, *.BMP)';...
'*.*','All Files(*.*)'},'Select the input file');

if ~strcmp(FileName,'')
fullname = strcat(PathName,FileName);
w=imread(fullname);
axes(handles.axes1);
imshow(w);
end
Unit 4. Change the properties of the RGB image

4.1. Change the RGB image to the negative image


function pushbutton_process_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_process (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global w;
g = imcomplement(w);
axes(handles.axes2);
imshow(g);
Unit 4. Change the properties of the RGB image

4.1. Change the RGB image to the negative image


Unit 4. Change the properties of the RGB image

4.2. Balance the gray levels


Unit 4. Change the properties of the RGB image

4.2. Balance the gray levels


function pushbutton_open_Callback(hObject, eventdata, handles)
global w
[FileName,PathName] = uigetfile({'*.png;*.PNG','PNG Files (*.png, *.PNG)';
'*.jpg;*.JPG','JPG Files (*.jpg, *JPG)';...
'*.tif;*.TIF','TIF Files (*.tif, *.TIF)';...
'*.gif;*.GIF','GIF Files (*.gif, *.GIF)';...
'*.bmp;*.BMP','BMP Files (*.bmp, *.BMP)';...
'*.*','All Files(*.*)'},'Select the input file');
if ~strcmp(FileName,0)
fullname = strcat(PathName, FileName) ;
w=imread(fullname);
axes(handles.axes1);
imshow(w);
anhgoc = fullname;
end
Unit 4. Change the properties of the RGB image

4.2. Balance the gray levels

function pushbutton_process_Callback(hObject, eventdata, handles)


global w
nlev = get(handles.edit1,'string');
nlev = str2num(nlev);
[~ , ~, p] = size(w);
if p ==3
w= rgb2gray(w);
else
g = histeq(w,nlev);
g = histeq(w,nlev);
figure,
figure,
subplot(2,2,1); imshow(w);title ('Original Image')
subplot(2,2,1); imshow(w);
subplot(2,2,2); imhist(w);
subplot(2,2,2); imhist(w);
ylim('auto');
ylim('auto');
subplot(2,2,3); imshow(g); title ('Balanced Image')
subplot(2,2,3); imshow(g);
subplot(2,2,4); imhist(g);
subplot(2,2,4); imhist(g);
ylim('auto');
ylim('auto');
end
Unit 4. Change the properties of the RGB image

4.2. Balance the gray levels


Unit 4. Change the properties of the RGB image

4.2. Balance the gray levels


Unit 4. Change the properties of the RGB image

4.3. Reduce the contrast


Unit 4. Change the properties of the RGB image

4.3. Reduce the contrast


function pushbutton_open_Callback(hObject, eventdata, handles)
global w
[FileName,PathName] = uigetfile({'*.jpg;*.JPG','JPG Files (*.jpg, *JPG)';
'*.tif;*.TIF','TIF Files (*.tif, *.TIF)';...
'*.gif;*.GIF','GIF Files (*.gif, *.GIF)';...
'*.png;*.PNG','PNG Files (*.png, *.PNG)';...
'*.bmp;*.BMP','BMP Files (*.bmp, *.BMP)';...
'*.*','All Files(*.*)'},'Select the input file');

if ~strcmp(FileName,'')
fullname = strcat(PathName,FileName);
w=imread(fullname);
axes(handles.axes1);
imshow(w);
end
Unit 4. Change the properties of the RGB image

4.3. Reduce the contrast


function pushbutton_process_Callback(hObject, eventdata, handles)
global w;

E = get(handles.E,'string');
if ~strcmp(E,'')
E = str2num(E);

g = im2double(w);
m = mean2(g);
g1 = 1./(1+(m./(g + eps)).^E);
axes(handles.axes1);
imshow(w);
axes(handles.axes2);
imshow(g1);
else
msgbox('Enter the slope of the function E')

end
Unit 4. Change the properties of the RGB image

4.3. Reduce the contrast


Unit 4. Change the properties of the RGB image

4.3. Reduce the contrast


function pushbutton_open_Callback(hObject, eventdata, handles)
global w
[FileName,PathName] = uigetfile({'*.jpg;*.JPG','JPG Files (*.jpg, *JPG)';
'*.tif;*.TIF','TIF Files (*.tif, *.TIF)';...
'*.gif;*.GIF','GIF Files (*.gif, *.GIF)';...
'*.png;*.PNG','PNG Files (*.png, *.PNG)';...
'*.bmp;*.BMP','BMP Files (*.bmp, *.BMP)';...
'*.*','All Files(*.*)'},'Select the input file');

if ~strcmp(FileName,'')
fullname = strcat(PathName,FileName);
w=imread(fullname);
axes(handles.axes1);
imshow(w);
end
Unit 4. Change the properties of the RGB image

4.4. Log function


Unit 4. Change the properties of the RGB image

4.4. Log function


function pushbutton_open_Callback(hObject, eventdata, handles)
global w;
[FileName,PathName] = uigetfile({'*.jpg;*.JPG','JPG Files (*.jpg, *JPG)';
'*.tif;*.TIF','TIF Files (*.tif, *.TIF)';...
'*.gif;*.GIF','GIF Files (*.gif, *.GIF)';...
'*.png;*.PNG','PNG Files (*.png, *.PNG)';...
'*.bmp;*.BMP','BMP Files (*.bmp, *.BMP)';...
'*.*','All Files(*.*)'},'Select the input file');

if ~strcmp(FileName,'')
fullname = strcat(PathName,FileName);
w=imread(fullname);
axes(handles.axes1);
imshow(w);
end
Unit 4. Change the properties of the RGB image

4.4. Log function


function pushbutton_process_Callback(hObject, eventdata, handles)
global w
c = get(handles.c,'string');
c = str2num(c);
if c ~= 0
g = im2double(w);
g = c*log(1 + double(g));
axes(handles.axes2);
imshow(g);
else
msgbox('Enter the value C')
end
Unit 4. Change the properties of the RGB image

4.4. Log function


Unit 4. Change the properties of the RGB image

4.5. Exponential function


Unit 4. Change the properties of the RGB image

4.5. Exponential function


function pushbutton_open_Callback(hObject, eventdata, handles)
global w

[FileName,PathName] = uigetfile({'*.jpg;*.JPG','JPG Files (*.jpg, *JPG)';


'*.tif;*.TIF','TIF Files (*.tif, *.TIF)';...
'*.gif;*.GIF','GIF Files (*.gif, *.GIF)';...
'*.png;*.PNG','PNG Files (*.png, *.PNG)';...
'*.bmp;*.BMP','BMP Files (*.bmp, *.BMP)';...
'*.*','All Files(*.*)'},'Select the input file');;
if ~strcmp(FileName,'')
fullname = strcat(PathName,FileName);
w=imread(fullname);
axes(handles.axes1);
imshow(w);
end
Unit 4. Change the properties of the RGB image

4.5. Exponential function


function pushbutton_process_Callback(hObject, eventdata, handles)
global w
c= get(handles.c,'string');
c = str2num(c);
gamma= get(handles.gamma,'string');
gamma = str2num(gamma);
g = im2double(w);
g = c*(g.^gamma);
axes(handles.axes2);
imshow(g);
Unit 4. Change the properties of the RGB image

4.5. Exponential function


Outline
Unit 1 Read and display the image

Unit 2 Histogram

Unit 3 Processing the RGB image

Unit 4 Change the properties of the RGB image

Unit 5 Add noise to the image

Unit 6 Count the number of object

Unit 7 Edge Detection

Unit 8 Detect and identify the image objects


Unit 5. Add noise to the image
Unit 5. Add noise to the image

function pushbutton_open_Callback(hObject, eventdata, handles)


global w;
[FileName,PathName] = uigetfile({'*.png;*.PNG','PNG Files (*.png, *.PNG)';
'*.tif;*.TIF','TIF Files (*.tif, *.TIF)';...
'*.gif;*.GIF','GIF Files (*.gif, *.GIF)';...
'*.jpg;*.JPG','JPG Files (*.jpg, *JPG)';...
'*.bmp;*.BMP','BMP Files (*.bmp, *.BMP)';...
'*.*','All Files(*.*)'},'Select the input file');
if ~isequal(FileName,0)
fullname = strcat(PathName,FileName);
w=imread(fullname);
axes(handles.axes1);
imshow(w);
else
msgbox('Select an image to process!')
end
Unit 5. Add noise to the image

function popupmenu1_Callback(hObject, eventdata, handles)


selectnoise = get(handles.popupmenu1,'value');
switch selectnoise
case 3
set(handles.edit1, 'visible', 'off');
set(handles.text4, 'visible', 'off');
case 4
set(handles.edit1, 'visible', 'off');
set(handles.text4, 'visible', 'off');
otherwise
set(handles.edit1, 'visible', 'on');
set(handles.text4, 'visible', 'on');
end
Unit 5. Add noise to the image
function pushbutton_addnoise_Callback(hObject, eventdata, handles)
global w;
X = size(w);
if (length(X) == 3)
g = rgb2gray(w);
else case 3
g = w; g1= imnoise(w,'localvar',ones(size(w)));
end; axes(handles.axes1);
selectnoise = get(handles.popupmenu1,'value'); imshow(w);
d = get(handles.edit1,'string'); axes(handles.axes2);
d = str2num(d); imshow(g1);
switch selectnoise case 4
case 1 g1= imnoise(w,'poisson');
g1= imnoise(w,'salt & pepper',d); axes(handles.axes1);
axes(handles.axes1); imshow(w);
imshow(w); axes(handles.axes2);
axes(handles.axes2); imshow(g1);
imshow(g1); case 5
case 2 g1= imnoise(w,'speckle',d);
g1= imnoise(w,'gaussian',d); axes(handles.axes1);
axes(handles.axes1); imshow(w);
imshow(w); axes(handles.axes2);
axes(handles.axes2); imshow(g1);
imshow(g1); end;
Unit 5. Add noise to the image
Unit 5. Add noise to the image
Unit 5. Add noise to the image
Unit 5. Add noise to the image
Unit 5. Add noise to the image
Outline
Unit 1 Read and display the image

Unit 2 Histogram

Unit 3 Processing the RGB image

Unit 4 Change the properties of the RGB image

Unit 5 Add noise to the image

Unit 6 Count the number of object

Unit 7 Edge Detection

Unit 8 Detect and identify the image objects


Unit 6. Count the number of object
% read the image
I=imread('coins.png');
imshow(I);
title('Original image')
% change the RGB image into black & white image
level=0.5;
I2=im2bw(I,level); % level=0-1
imshow(I2)
BW=imfill(I2,'holes');
imshow(BW);
% count the number of objects
CC=bwconncomp(BW,8);
msgbox(['Number of objects',num2str(CC.NumObjects)]);
% statistics the characteristic of the white regions
s=regionprops(BW,'Centroid');
center=cat(1,s.Centroid);
% display
hold on
plot(center(:,1),center(:,2),'+r')
Unit 6. Count the number of object
Outline
Unit 1 Read and display the image

Unit 2 Histogram

Unit 3 Processing the RGB image

Unit 4 Change the properties of the RGB image

Unit 5 Add noise to the image

Unit 6 Count the number of object

Unit 7 Edge Detection

Unit 8 Detect and identify the image objects


Unit 7. Detect the image objects
7.1. Edge Detection
a=imread('quan.jpg');
j = imresize(a, 0.2);
b=rgb2gray(j);
figure(1)
imshow(b);
h=[1 0 -1; 2 0 -2; 1 0 -1]
c=imfilter(b,h);
figure(2)
imshow(c);
Unit 7. Detect the image objects
Unit 7. Detect the image objects
7.2. Canny Edge Detection
a=imread('quan.jpg');
j = imresize(a, 0.2);
b=rgb2gray(j);
figure(1)
imshow(b);
c=edge(b,'canny');
figure(2)
imshow(c)
d=bwmorph(c,'dilate',2);
figure(3)
imshow(d);
Unit 7. Detect the image objects
Unit 7. Detect the image objects
7.3. Prewitt Edge Detection
a=imread('quan.jpg');
j = imresize(a, 0.2);
b=rgb2gray(j);
figure(1)
imshow(b);
c=edge(b,'prewitt');
figure(2)
imshow(c)
d=bwmorph(c,'dilate',2);
figure(3)
imshow(d);
Unit 7. Detect the image objects
Outline
Unit 1 Read and display the image

Unit 2 Histogram

Unit 3 Processing the RGB image

Unit 4 Change the properties of the RGB image

Unit 5 Add noise to the image

Unit 6 Count the number of object

Unit 7 Edge Detection

Unit 8 Detect and identify the image objects


Unit 8. Detect the image objects
8.1. Detect object shape
[filename,ip_filepath] = uigetfile('*.tif;*.tiff;*.jpg;*.jpeg;*.png;*.gif;*.bmp','Select
Image file');
full_path = strcat(ip_filepath,filename);
ip_image = imread(full_path);
op_filepath = strcat('op_image\', filename);

[M,N] = size(ip_image);

detect_object(ip_image);
Unit 8. Detect the image objects
8.1. Detect object shape

function [] = detect_object(img)

outlined_img = img;
img = im2bw(img);% img must be RGB and high resolution
img = edge(img);

for mm = 1:size(outlined_img,1)
for nn = 1:size(outlined_img,2)
if ((img(mm,nn) == 1))
outlined_img(mm,nn,:) = [255 0 0]; % red outline in shapes
end
end
end

CC = bwconncomp(img);
STATS = regionprops(CC, 'all');

imshow(outlined_img);hold on;
Unit 8. Detect the image objects
8.1. Detect object shape for i = 1:CC.NumObjects
BB = STATS(i).BoundingBox;
BW = STATS(i).Image;% each connected object
BW = bwmorph(BW,'diag');
[H,T,R] = hough(BW);
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
Area = STATS(i).FilledArea;
Perimeter = STATS(i).Perimeter;
Roundness=(4*Area*pi)/(Perimeter.^2);
if(Roundness > 0.9)
text(BB(1),BB(2),'Circle','color','blue');
elseif(length(lines) == 3)
text(BB(1),BB(2),'Triangle','color','blue');
elseif(length(lines) == 4)
text(BB(1),BB(2),'Rectangle','color','blue');
elseif(length(lines) == 5)
text(BB(1),BB(2),'Pentagon','color','blue');
else
text(BB(1),BB(2),'Unknow','color','blue');
end
end
Unit 8. Detect the image objects
Unit 8. Detect the image objects

8.2. Coordinate Identification


% read the image
I= imread('InputImage.jpg');
% change the RGB image into gray image
J= rgb2gray(I);
% adjusting the gray image
J = imadjust(J);
% figure
imshow(J);
% change the gray image into binary image (black & white)
imagen =im2bw(J,.26);
imagen = bwareaopen(imagen,3000); % delete the object < 3000 pixel
figure
imshow(imagen);
[label,num] = bwlabel(imagen,8); % assign the lable to object
stats = regionprops(label,'all'); % get the properties of the object
imshow(I);
Unit 8. Detect the image objects

8.2. Coordinate Identification


for i = 1:num
if stats(i).Eccentricity < 0.28 % test the roundness of the objects
number = i;
area = stats(i).Area;
radius = sqrt(area/pi);
center = stats(i).Centroid;
t = 0 :0.1:2*pi;
x = center(1) + radius*cos(t);
y = center(2) + radius*sin(t);
hold on
plot(x,y,'r')
plot(center(1),center(2),'r+')
text(center(1)-10,center(2)-20,['X= ',num2str(center(1)),' Y=
',num2str(center(2))],'BackgroundColor',[.7 .9 .7])
end
end
Unit 8. Detect the image objects
References

1. Dwayne Phillips, Image Processing in C, R&D Publication, 2000.


2. Rafael C. Gonzalez, Richard E. Woods, Steven L. Eddins, Digital Image
Processing Using MATLAB, Prentice Hall, 2002.
3. Rafael C. Gonzalez, Richard E. Woods, Digital Image Processing, Prentice
Hall, 2002.

Вам также может понравиться