2017-07-19 64 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

相关问题