简体   繁体   中英

matlab code for image cropping without using inbuilt function

The below code is for cropping the excess white part of an image.(for the purpose of reducing the image size ). ie if "A" is present in the image then all the excess white portion at the top ,bottom, left and right would be removed.

In this code i am not able to understand the use of "sum" function,please help in that..

 %  Find the boundary of the image
  [y2temp x2temp] = size(bw);
 x1=1;
 y1=1; 
  x2=x2temp; 
  y2=y2temp;


 % Finding left side blank spac es
 cntB=1;
 while (sum(bw(:,cntB))==y2temp)
  x1=x1+1;
 cntB=cntB+1;
    end


  % Finding right side blank spaces
 cntB=1;
  while (sum(bw(cntB,:))==x2temp)
 y1=y1+1;
 cntB=cntB+1;
end


 % Finding upper side blank spaces
cntB=x2temp;
 while (sum(bw(:,cntB))==y2temp)
x2=x2-1;
cntB=cntB-1;
end


 %  Finding lower side blank spaces
 cntB=y2temp;
 while (sum(bw(cntB,:))==x2temp)  
 y2=y2-1;
 cntB=cntB-1;  
 end


 % Crop the image to the edge
  bw2=imcrop(bw,[x1,y1,(x2-x1),(y2-y1)]);

This code probably does the same thing, in less lines.

bw_bin=bw==1; %make it binary
row = all(bw_bin); %checks if they are all one
column = all(bw_bin');
bw=bw(find(column==0,1,'first'):find(column==0,1,'last'),find(row==0,1,'first'):find(row==0,1,'last')); %ake only the rows and columns where this is not the case

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM