2017-07-19 65 views
0

這是我的整個代碼。我在這裏做的是試圖找到圖像和我從圖像中減去的模板之間的匹配點。如何在Matlab中對不同圖像中的對象數進行求和?

第一個循環和第二個循環從原始圖像中提取所有卡片(第一張圖像與一組卡片) 代碼的下一部分是找到提取的卡片和模板之間的匹配點。 (selectCard) 然後我創建了for循環(selectCard),並且我將連接的組件應用到了當他們通過選擇標準時,對卡上剩餘的物體進行計數。

所以我需要獲得所有通過我所做選擇標準的牌的總數。基本上,我得到了每張牌的結果。不是所有的卡。我需要計算所有這些卡的結果。

for j=1:max(max(LabeledImage)) 

    [row, col] = find(LabeledImage==j); 

    len=max(row)-min(row)+2; 
    breadth=max(col)-min(col)+2; 
    Img=uint8(zeros([len breadth 3])); 
    sy=min(col)-1; 
    sx=min(row)-1; 

    for i=1:size(row,1) 
    x=row(i,1)-sx; 
    y=col(i,1)-sy; 
    Img(x,y,:)=grayImage(row(i,1),col(i,1),:); 
    end 
    mytitle=strcat('Card Number:',num2str(j)); 
    % figure,imshow(Img);title(mytitle); 
    Img=rgb2gray(Img); 
    pointsForSpade1 = detectHarrisFeatures(Img); 
    pointsForSpade2 = detectHarrisFeatures(Template_for_spade); 

    %extract neighborhood features 
    [featuresForSpade1,valid_pointsForSpade1] = 
    extractFeatures(Img,pointsForSpade1); 
    [featuresForSpade2,valid_pointsForSpade2] = 
    extractFeatures(Template_for_spade,pointsForSpade2); 
    %Match the features 
    indexPairs = matchFeatures(featuresForSpade1,featuresForSpade2); 
    %retrieve the locations of the corresponding points for each image. 
    matchedPointsForSpade1 = valid_pointsForSpade1(indexPairs(:,1),:); 
    matchedPointsForSpade2 = valid_pointsForSpade2(indexPairs(:,2),:); 
    % visualize the corresponding points. 

    figure,subplot(5,5,j) 


    showMatchedFeatures(Img,Template_for_spade,matchedPointsForSpade1,..... 
    matchedPointsForSpade2, 'montage'); 

count1 = matchedPointsForSpade1.Count; 
%disp(matchedPoints1); 
selectCard4 = find(count1>10); 
Total = 0; 
for e4= 1: selectCard4 
figure, subplot(5,5,j) 
showMatchedFeatures(Img(e4),Template_for_spade,matchedPointsForSpade1,... 
matchedPointsForSpade2, 'montage'); 
    title(Name); 


    % eliminate not needed hole on the card. 
    level = 0.57; 
    Img2 = imbinarize(Img, level); 

%Morphological operation 
ImgComp = imcomplement(Img2); 
se = strel('disk', 10); 
Iopened = imopen(ImgComp, se); 
%figure; 
%imshow(Iopened); 
[Label3, numObject4] = bwlabel(Iopened); 
TotalSpade = sum(numObject4); 

s = sprintf('\n card number: of this set has #%s spades',j, 
num2str(TotalSpade)); 
    disp(s); 
end 

    end 

我想根據選定的卡顯示連接對象的總和。

謝謝。

+4

請發表[mcve]。 'bwlabel'將返回找到的組件的數量,所以這只是對這兩個數字求和的問題。 – beaker

+0

謝謝。我編輯過,以防你仍然想要幫助! – steve

+0

@steve你的代碼只是一遍又一遍地找到同一個圖像'Iopened'的連接組件,'sum'沒有做任何事情,但它看起來應該爲該圖像打印正確數量的組件。你想要你的代碼做什麼不同?另外,如果你正在迴應某人,你應該像@beaker那樣對他們進行ping,以便他們得到通知。否則,他們不知道你已經回覆了他們(一般來說......有例外)。 – beaker

回答

1

這裏有三個主要問題。首先,初始化Total = 0;,然後將變量名稱更改爲TotalSpade。初始化更改爲:

TotalSpade = 0; 

其次,分配給TotalSpade被覆蓋以前的值。爲了累積總數,你需要改爲TotalSpade。另外,numObject4是一個標量,因此sum不會做任何事情。

TotalSpade = TotalSpade + numObject4; 

最後,如果您只想打印所有對象,則需要將打印語句放在循環之外。如果不指定文件描述符,則您也可以使用fprintf而不是sprintf + disp,因爲fprintf會打印到控制檯。

for e4 = 1:selectCard4 
    ... 
end 
fprintf('\n card number: of this set has #%s spades', j, 
    num2str(TotalSpade)); 
+0

謝謝貝克工作。完美。 – steve

+0

謝謝@beaker完美。我把Total_diamonds放在所有For循環後給了我想要的結果。感謝您的時間。因此,爲了打印每張卡片上的每個物體,我使用了以前的方法,並在使用您的卡片的同時打印所有卡片上的所有物體的總數。希望它能幫助某人一天。 – steve

相關問題