📄 imscan.m
字号:
% Version : 4.1
% Author : Omid Bonakdar Sakhi
function im_out = imscan (net,im)
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% This function gets the network and an image , select some 27x18 windows
这功能拿网络和一图像 , 选择一些 27 x 18扇窗户
% of the image and send them to network .
% 图像而且将他们送到网络。
% The result of the network determins if the window is a face or not
% 网络 determins 的结果是否窗户是一个脸
% It is not as easy as that . follow the comments
% 它不是像那一样的容易。 跟随意见
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
REPORT_FILE = 'report.txt'; %name of the report file %报告的名字文件
SCAN_FOLDER = 'imscan\'; %folder for storing the detected faces %储存的文件夹那发现脸
UT_FOLDER = 'imscan\under-thresh\';
TEMPLATE = 'template.png';
%if the network result for a 27x18 window gets over thresh , that window is
%如果网络结果为这 27 x 18扇窗户克服打谷 , 窗户是
%detected as a face. %发现当做一脸。
thresh = 0.8;
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
warning off;
delete ([UT_FOLDER,'*.*']);
delete ([SCAN_FOLDER,'*.*']);
mkdir (UT_FOLDER);
mkdir (SCAN_FOLDER);
fid = fopen([SCAN_FOLDER,REPORT_FILE],'wt');
[m n]=size(im);
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% It is not possible to send all 27x18 windows in a photo to the network
把在一张相片中的 18扇窗户送给所有的 27 x 到网络是不可能的
% ( see notes in 'im2vec.m' %( 在 'im 2 vec.m' 中见到笔记
% so we should chose some windows in a large photo which the existent
% 因此我们应该在一张大的相片中选择一些窗户那存在的
% probapility of the face is more in these windows
% 脸的 probapility 在这些窗户中是更多
% A window is determined by it's center which is a pixel on the image.
% 一扇窗户是坚决的被它是一个图素的中心在那之上图像。
% Template is a 27x18 image or window containing a face without background
% 型板是这 27 x 18个图像或窗户包含没有背景的一个脸
% Correlation between the Template and the image will give us an image
% 相互关系在型板之间和那图像意志给予我们一图像
% which have peaks over the faces area.
% 哪一个有在脸区域上的峰巅。
% imregonalmax is MATLAB function which extract the peaks in the binary mask
% imregonalmax 是 MATLAB 功能榨出物峰巅在那二进位的面具
% each pixel in the binary mask will be used as a center for a window
% 每个图素在那二进位的面具意志为一扇窗户当做中心被用
%
C1 = adapthisteq(im,'NumTiles',[20 20]);
C2 = imread (TEMPLATE);
Co = conv2 (single(C1),single(C2),'same');
mask = imregionalmax(Co);
% four lines below erase the peaks from the border of the image
% 在下面的四条行消除峰巅从那边缘那图像
mask (1:14,:)=0;
mask (end-14:end,:)=0;
mask (:,1:9)=0;
mask (:,end-10:end)=0;
[LUTm LUTn]= find(mask);
Length = size(LUTm,1);
% two lines below show the image on the screen with the preprecessed
% 在下面的二条行表演那图像在那之上荧屏与 preprecessed
% centers in red % 中心在红色的
imshow(im);hold on
plot(LUTn,LUTm,'.r');pause(0);
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%~~~~~~~~~~~~~~~~~~~~~~~ SOS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% The predication of centers will not always work properly but it's good
% 中心的断定不会总是工作适当地但是它是善行
% for must of the times % 为一定时代
% If anyone have a suggestion about the above algorithm , I will
% 如果任何人有一个提议有关那上方运算法则 , 我意志
% be glad to hear % 高兴听到
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Now that we predict some centers for the windows , these loops find the
% 既然我们为窗户预测一些中心 , 这些环发现那
% maximum result of the network around these centers which is the exact
% 最大的在这些中心哪一个是周围的网络产生那精确的
% location of the face . remember each 27x18 window is determined by it's
% 脸的位置。 记得每个 27 x 18扇窗户是坚决的被它是
% center . % 中心。
% type spiral(18) in the command window and compare the numbers in it with
% 类型螺旋形之物 (18) 在指令窗户中和比较那数字在它里面与
% the location of the centers you see on screen with yellow color.
% 中心的位置你见到在荧屏之上与黄色的颜色。
xy_ = zeros(m,n);
t = cputime;
SEARCH = [zeros(4,18);spiral(18);zeros(5,18)];
for i=1:Length
iter=0;
itermax=5;
result_max=-1;
while (iter~=itermax)
iter=iter+1;
[offm offn]=find(SEARCH==iter);
n_=LUTn(i)-9+offn;
m_=LUTm(i)-13+offm;
plot(n_,m_,'.y');pause(0);
try
imcut = im(m_-13:m_+13,n_-9:n_+8);
P = im2vec(imcut);
result_ = sim(net,P);
fprintf(fid,'\n@x=%5.2f,y=%5.2f\tr=%5.2f\n',n_,m_,result_);
if result_max<result_
n__=n_;
m__=m_;
result_max=result_;
imcut_ = imcut;
end
if result_max>0
itermax=50;
end
end
if (iter == 20 && result_max<thresh)
break;
end
end
if result_max>thresh
xy_(m__,n__)=result_max;
plot(n__,m__,'.g');
text(n__,m__,num2str(result_max,'%2.2f'),'Color',[1 1 1],'BackgroundColor',[0 0 0]);
fprintf(fid,'****************\n');
imwrite(imcut_,[SCAN_FOLDER,'@',int2str(m__),',',int2str(n__),' (',int2str(fix(result_max*100)),'%).png']);
else
imwrite(imcut_,[UT_FOLDER,'@',int2str(m__),',',int2str(n__),' (',int2str(fix(result_max*100)),'%).png']);
end
end
hold off
e = (cputime-t)/Length;
fprintf(fid,'\nTotal Time : %5.2f',e*Length);
fprintf(fid,'\nTotal Pixels : %5.2f',Length);
fprintf(fid,'\n~~~~~~~~~~~~~~~~~~~~\n\nAuthor : Omid Sakhi');
fprintf(fid,'\nomid.sakhi@gmail.com');
fclose(fid);
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% now we have and image called xy_ , to draw the green rectangles we should
% 现在我们有和图像呼叫 xy_, 到平局绿色的长方形我们应该
% find the centers of rectangles which is pixles with nwtrork results
% 找用 nwtrork 是 pixles 的长方形的中心结果
% over threshold % 在门槛之上
xy_ = xy_>thresh;
xy_ = imregionalmax(xy_);
xy_ = imdilate (xy_,strel('disk',2,4));
[LabelMatrix,nLabel] = bwlabeln(xy_,4);
CentroidMatrix = regionprops(LabelMatrix,'centroid');
xy_ = zeros(m,n);
for i = 1:nLabel
xy_(fix(CentroidMatrix(i).Centroid(2)),...
fix(CentroidMatrix(i).Centroid(1))) = 1;
end
xy_ = drawrec(xy_,[27 18]);
im_out (:,:,1) = im;
im_out (:,:,2) = im;
im_out (:,:,3) = im;
for i = 1:m
for j=1:n
if xy_(i,j)==1
im_out (i,j,1)=0;
im_out (i,j,2)=255;
im_out (i,j,3)=0;
end
end
end
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -