這是我的整個代碼。我在這裏做的是試圖找到圖像和我從圖像中減去的模板之間的匹配點。如何在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
我想根據選定的卡顯示連接對象的總和。
謝謝。
請發表[mcve]。 'bwlabel'將返回找到的組件的數量,所以這只是對這兩個數字求和的問題。 – beaker
謝謝。我編輯過,以防你仍然想要幫助! – steve
@steve你的代碼只是一遍又一遍地找到同一個圖像'Iopened'的連接組件,'sum'沒有做任何事情,但它看起來應該爲該圖像打印正確數量的組件。你想要你的代碼做什麼不同?另外,如果你正在迴應某人,你應該像@beaker那樣對他們進行ping,以便他們得到通知。否則,他們不知道你已經回覆了他們(一般來說......有例外)。 – beaker