پروژه پردازش تصویر : تشخیص اجزای صورت (محدوده ی صورت، چشمها، بینی ، دهان)
راهنمایی:
تذکر: لطفا" برای اخذ نتیجه ی مطلوب, هنگام استفاده یا کپی کدها در محیط برنامه ی متلب فونت کدها را تغییر ندهید.
این پروژه از هفت mfile تشکیل شده است 3 تصویر نمونه تشکیل شده است ، در صورت تمایل از تصاویر انتخابی دلخواه هم میتوانید استفاده کنید:
Mfile هاو توضیحاتشان به شرح زیر میباشد.
————————-
1))
buildDetector.m :
در این mفایل تابع buildDetector یک شی(object) برای کشف بخشهای مختلف صورت میسازد.
متغیر خروجی: detector
متغیرهای ورودی تابع:
thresholdFace: MergeThreshold برای تشخیص آستانه ی صورت، مقدار پیشفرض 1
thresholdParts: MergeThreshold جهت تشخیص مرزهای اجزای صورت،پیشفرض 1
stdsize: سایز صورت تعدیل شده ،با مقدار پیشفرض 176
———————–
(2)
: در این پروژه به2 جعبه ابزار پردازش تصویر و checkToolboxes.m
بینایی ماشین نیاز است.تابع ret دسترسی به این دو toolbox راچک میکند .
———————–
(3)
start.m
با فراخوانی checkToolboxes در صورت عدم وجود جعبه ابزارهای مورد نیاز پیغام مناسب صادر، در غیر اینصورت تصویر مشخص شده در تابع
Img=imread('image.jpg');
را load ، و پس از اعمال تغییرات مورد نظر توسط توابع فراخوانی شده، تصویر نهایی را نمایش میدهدjpg .image نام و پسوند فایل تصویر مورد نظر است که میتواند با توجه بنام تصویر ورودی تغییر کند.مثلا" reza.jpg یا mahsa.png
بنابراین اگر قصد استفاده از تصاویرمختلف دارید،فراموش نکنید نام فایل تصویر را در این تابع باید تغییر دهید.
——————————-
(4)
detectFaceParts.m
[bbox,bbX,faces,bbfaces] = detectFaceParts(detector,X,thick)تابع
که مو'لفه های ورودی ها و خروجیهای آن در متن کد مشخص شده اجزای صورت را شناسایی میکند.
——————————-
(5)
detectRotFaceParts.m
تشخیص محدوده ی صورت با تخمین و شناسایی آستانه های دوار صورت
——————————–
(6)
drawFourPoints.m
رسم خطوط با توجه به چهارنقطه' شناسایی شده ی برای محدوده های هر یک از اجزای صورت
——————————–
(7)
mergeFourPoints.m
ادغام محدوده های مشخص شده برای اجزای صورت وآماده سازی آن جهت نمایش در تصویر خروجی
—————————-
علاوه بر این mflle ها نیاز به تصویر ورودی داریم که 3 نمونه تصویر مناسب برای استفاده در ذیل قرار داده شده است.
نحوه' استفاده و اجرا:
ابتدا پوشه ای با نام و در محل دلخواهایجاد میکنیم. مثلا" پوشه ی myfolder
کد های هر یک از m فایلها را بصورت جداگانه copy و هر کدام را در یک پنجره ی new scrip مجزابا نام مشخص شده در بالا برای هر فایل paste کنید.و همه را
در پوشه ی myfolder ذخیره کنید. روی هر یک از تصاویر که در پایین قرار داده شده راست کلیک و گزینه ی:
Save image as را انتخاب و تصاویر را به پوشه ی myfolder آدرسدهی و ذخیره کنید. بنا براین در این پوشه 7 mfile بعلاوه' یک یا چند تصویر خواهید داشت.
تمام محتویات پوشه را کپی و در current folder متلب paste کنید.
در command window برنامه متلب عبارت start را تاپ کرده و دکمه ی اینتر را بزنید. خروجی برنامه با انتخاب تصویر mahsa.png بصورت عکس زیر خواهد بود:
تذکر: بعد از هر بار اجرا اگر قصد تغییر تصویر ورودی را دارید ابتدابایستی
دستور clear را در command window اجرا کنید. با این دستور مقادیر نسبت داده شده به متغیرهاپاک شده و برای اجرای مجدد برنامه آماده میشود.
سورس کد:
% buildDetector.m
% buildDetector: build face parts detector object
%
% detector = buildDetector( thresholdFace, thresholdParts, stdsize )
%
%Output parameter:
% detector: built detector object
%
%
%Input parameters:
% thresholdFace (optional): MergeThreshold for face detector (Default: 1)
% thresholdParts (optional): MergeThreshold for face parts detector (Default: 1)
% stdsize (optional): size of normalized face (Default: 176)
%
%
%Example:
% detector = buildDetector();
% img = imread('img.jpg');
% [bbbox bbimg] = detectFaceParts(detector,img);
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function detector = buildDetector( thresholdFace, thresholdParts, stdsize )
if( nargin < 1 )
thresholdFace = 1;
end
if( nargin < 2 )
thresholdParts = 1;
end
if( nargin < 3 )
stdsize = 176;
end
nameDetector = {'LeftEye'; 'RightEye'; 'Mouth'; 'Nose'; };
mins = [[12 18]; [12 18]; [15 25]; [15 18]; ];
detector.stdsize = stdsize;
detector.detector = cell(5,1);
for k=1:4
minSize = int32([stdsize/5 stdsize/5]);
minSize = [max(minSize(1),mins(k,1)), max(minSize(2),mins(k,2))];
detector.detector{k} = vision.CascadeObjectDetector(char(nameDetector(k)), 'MergeThreshold', thresholdParts, 'MinSize', minSize);
end
detector.detector{5} = vision.CascadeObjectDetector('FrontalFaceCART', 'MergeThreshold', thresholdFace);
————————————————-
% checkToolboxes.m
function ret = checkToolboxes(req)
% reqToolboxes = {'Computer Vision System Toolbox', 'Image Processing Toolbox'};
% checkToolboxes(reqToolboxes)
info = ver;
s=size(info);
flg = zeros(size(req));
reqSize = size(req,2);
for i=1:s(2)
for j=1:reqSize
if( strcmpi(info(1,i).Name,req{1,j}) )
flg(1,j)=1;
end
end
end
ret = prod(flg);
————————————————————————
% start.m
reqToolboxes = {'Computer Vision System Toolbox', 'Image Processing Toolbox'};
if( ~checkToolboxes(reqToolboxes) )
error('detectFaceParts requires: Computer Vision System Toolbox and Image Processing Toolbox. Please install these toolboxes.');
end
img = imread('mahsa.png');
detector = buildDetector();
[bbox bbimg faces bbfaces] = detectFaceParts(detector,img,2);
figure;imshow(bbimg);
for i=1:size(bbfaces,1)
figure;imshow(bbfaces{i});
end
% Please uncoment to run demonstration of detectRotFaceParts
%{
img = imrotate(img,180);
detector = buildDetector(2,2);
[fp bbimg faces bbfaces] = detectRotFaceParts(detector,img,15,2);
figure;imshow(bbimg);
for i=1:size(bbfaces,1)
figure;imshow(bbfaces{i});
end
%}
———————————————————————
% detectFaceParts.m
% detectFaceParts: detect faces with parts
%
% [bbox,bbX,faces,bbfaces] = detectFaceParts(detector,X,thick)
%
%Output parameters:
% bbox: bbox(:, 1: 4) is bounding box for face
% bbox(:, 5: 8) is bounding box for left eye
% bbox(:, 9:12) is bounding box for right eye
% bbox(:,13:16) is bounding box for mouth
% bbox(:,17:20) is bounding box for nose
% please see the documentation of the computer vision toolbox for details of the bounding box.
% bbX: image with found faces which are shown as boxes
% faces: found faces stored as cell array
% bbfaces: found faces with boxes stored as cell array
%
%
%Input parameters:
% detector: the detection object built by buildDetector
% X: image data which should be uint8
% thick(optional): thickness of bounding box (default:1)
%
%
%Example:
% detector = buildDetector();
% img = imread('img.jpg');
% [bbbox bbimg] = detectFaceParts(detector,img);
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [bbox,bbX,faces,bbfaces] = detectFaceParts(detector,X,thick)
if( nargin < 3 )
thick = 1;
end
%%%%%%%%%%%%%%%%%%%%%%% detect face %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Detect faces
bbox = step(detector.detector{5}, X);
bbsize = size(bbox);
partsNum = zeros(size(bbox,1),1);
————————————————————————-
% detectRotFaceParts.m
% detectRotFaceParts: detect faces with parts rotating input image
%
% function [fourpoints,bbX,faces,bbfaces] = detectRotFaceParts(detector,X,thick,rotate)
%
%Output parameters:
% fourpoints: four corner points of face, left eye, right eye, mouth, and norse
% fourpoints(:,1:2): (x,y) of top left corner of face
% fourpoints(:,3:4): (x,y) of top right corner of face
% fourpoints(:,5:6): (x,y) of bottom right corner of face
% fourpoints(:,7:8): (x,y) of bottom left corner of face
% fourpoints(:,9:16): (x,y) set of four corner of left eye
% fourpoints(:,17:24): (x,y) set of four corner of right eye
% fourpoints(:,25:32): (x,y) set of four corner of mouth
% fourpoints(:,33:40): (x,y) set of four corner of nose
% fourpoints(:,41): degrees which the faces were found
% bbX: image with found faces which are shown as boxes
% faces: found faces stored as cell array
% bbfaces: found faces with boxes stored as cell array
%
% NOTE: fourpoints of detectRotFaceParts are diferent from bbox of detectFaceParts
%
%
%Input parameters:
% detector: the detection object built by buildDetector
% X: image data which should be uint8
% thick(optional): thickness of bounding box (default:1)
% rotate(optional): degree step of rotation (default:15)
%
%Example:
% detector = buildDetector(2,2);
% img = imread('img.jpg');
% [fp bbimg] = detectRotFaceParts(detector,img);
%
%
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [fourpoints,bbX,faces,bbfaces] = detectRotFaceParts(detector,X,thick,rotate)
if( nargin < 4 )
rotate = 15;
end
if( nargin < 3 )
thick = 1;
end
rotate = [0:rotate:360-rotate/2];
srcOrg = [size(X,2);size(X,1)]/2+0.5;
————————————————————————-
% % % drawFourPoints.m
% drawFourPoints draws bounding boxes on input image
%
% Y = drawFourPoints( X, fourpoints, thick )
%
%Output parameter:
% Y: image drawn bounding boxes
%
%
%Input parameter:
% X: input image
% fourpoints: fourpoints data to be drawn
% thick(optional): thickness of bounding boxes (defualt:1)
%
%
%Example:
% detector = buildDetector(2,2);
% img = imread('img.jpg');
% fp = detectRotFaceParts(detector,img);
% bbimg = drawFourPoints(img, fp);
%
%
%NOTE: This function is internally called in detectRotFaceParts.
%
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Y = drawFourPoints( X, fourpoints, thick )
if( nargin < 3 )
thick = 1;
end
Y = X;
if( size(fourpoints,1) > 0 )
boxColor = [[0,255,0]; [255,0,255]; [255,0,255]; [0,255,255]; [255,255,0]; ];
M=int32(fourpoints);
if( thick >= 0 )
t = (thick-1)/2;
t0 = -int32(ceil(t));
t1 = int32(floor(t));
else
t0 = 0;
t1 = 0;
end
for k=5:-1:1
shapeInserter = vision.ShapeInserter('Shape','Lines','BorderColor','Custom','Antialiasing',true,'CustomBorderColor',boxColor(k,:));
N = horzcat(M(:,(k-1)*8+1:(k-1)*8+8),M(:,(k-1)*8+1:(k-1)*8+2));
for i=t0:t1
NN = N;
NN(:,[1,3,5,7,9]) = N(:,[1,3,5,7,9]) + i;
Y = step(shapeInserter, Y, NN);
NN = N;
NN(:,[2,4,6,8,10]) = N(:,[2,4,6,8,10]) + i;
Y = step(shapeInserter, Y, NN);
end
end
end
————————————————————————
% % mergeFourPoints.m
% mergeFourPoints merges multiple detection of same face
%
% dst = mergeFourPoints(src)
%
%Output parameter:
% dst: merged fourpoints data
%
%
%Input parameter:
% src: fourpoints data to be merged
%
%
%NOTE: This function is internally called in detectRotFaceParts
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function dst = mergeFourPoints(src)
if( size(src,1) > 0 )
pos = zeros(size(src,1),2);
pos(:,1) = mean(src(:,[1,3,5,7]),2);
pos(:,2) = mean(src(:,[2,4,6,8]),2);
pos = horzcat(pos,[1:size(src,1)]');
dst = [];
while( ~isempty(pos) )
if( size(pos,1) == 1 )
dst = vertcat(dst,src(pos(1,3),:));
pos = [];
else
tmp = src(pos(1,3),:);
rad = norm(pos(1,1:2) – tmp(1,1:2)) + norm(pos(1,1:2) – tmp(1,3:4)) + norm(pos(1,1:2) – tmp(1,5:6)) + norm(pos(1,1:2) – tmp(1,7:8));
th = rad / 8;
p = 1;
for i=2:size(pos,1)
rad = norm(pos(1,1:2) – pos(i,1:2));
if( rad < th )
tmp = vertcat(tmp,src(pos(i,3),:));
p = horzcat(p,i);
end
end
num = tmp > 0;
tmp = sum(tmp) ./ sum(num);
dst = vertcat(dst,tmp);
pos(p,:) = [];
end
end
else
dst = src;
end
Mahsa.png
Mona.jpg
Reza.jpg
4