2017-09-11 4 views
0

以下のコードは、画像の余分な白い部分を切り抜くためのコードです(画像サイズを縮小するためです)。すなわち、「A」が画像内に存在する場合、上部、下部、左および右のすべての余分な白色部分が除去される。このコードで組み込み関数を使用しない画像切り抜き用のmatlabコード

私は「合計」機能の使用を理解することはできませんよ、という点で助けてくださいは...

% 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)]); 
+0

さらに[](https://www.mathworks.com/help/matlab/ref/sum.html) –

+2

も読んでください。ここでは、イメージ「bw」の行または列を合計します。インデックスは 'cntB'と呼ばれます。それらがすべて1である場合、合計は合計長さに等しい。それでwhileループを続けます。 – Gelliant

+0

ありがとう、私はコードを理解した。 – Vishal

答えて

0

このコードは、おそらく少ないラインで、同じことを行います。

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 
関連する問題