Image Processing Lesson
Image Processing Lesson
Unit 2 Histogram
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
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
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
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
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
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
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
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
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
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
Unit 2 Histogram
Unit 2 Histogram
Unit 2 Histogram
Unit 2 Histogram
[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