2

在 Matlab 关于estimateGeometricTransform 的帮助中说,如果这个函数有一些错误,它可能会返回状态(例如'matchedPoints1 和matchedPoints2 输入不包含足够的点。')它应该这样写:

[___,status] = estimateGeometricTransform(matchedPoints1,matchedPoints2,transformType) 

但就我而言,它不起作用。

 [tform, status] = estimateGeometricTransform(matchedBoxPoints, matchedScenePoints, ...
     'projective');

在该命令之后,我使用“IF”语句来检查状态,如果它不等于一个,我需要执行一些命令。但我只是在控制台中得到错误: MATCHED_POINTS1 和 MATCHED_POINTS2 没有足够的积分。 程序停止。

怎么了?

video = 'sample.avi';
hVideoSource = vision.VideoFileReader(video, 'ImageColorSpace', 'Intensity');
hVideoOut = vision.VideoPlayer('Name', 'Detected Box');     

boxImage = imread('cross2.jpg');
boxImage = rgb2gray(boxImage);

boxPoints = detectSURFFeatures(boxImage, 'NumOctaves', 10, 'NumScaleLevels', 15);
figure; imshow(boxImage);
title('100 Strongest Feature');
hold on;
plot(boxPoints.selectStrongest(100));

while ~isDone(hVideoSource)
    sceneImage = step(hVideoSource);
    sceneImage = imadjust(sceneImage);
bw = edge(sceneImage,'canny', 0.15, 2);
bw = imfill(bw,'holes');
se = strel('disk',1); 
bw = imopen(bw,se);
[B,L] = bwboundaries(bw);
stats = regionprops(L,'Centroid','EquivDiameter');
for k = 1:length(B)
 boundary = B{k};
 radius = stats(k).EquivDiameter/2;
 xc = stats(k).Centroid(1);
 yc = stats(k).Centroid(2);
 theta = 0:0.01:2*pi;
 Xfit = radius*cos(theta) + xc;
 Yfit = radius*sin(theta) + yc;

 if (radius > 10)
     rect = [xc-radius yc-radius radius*2 radius*2];
     croppedImage = imcrop(sceneImage, rect);
     rgbImage = cat(3,sceneImage,sceneImage,sceneImage);
     rgbImage = insertShape(rgbImage, 'Circle', [xc yc radius], 'Color', 'green');
     rgbImage = insertShape(rgbImage, 'Rectangle', rect, 'Color', 'yellow');    
 end

end
scenePoints = detectSURFFeatures(croppedImage, 'NumOctaves', 10, 'NumScaleLevels', 15);


    [boxFeatures, boxPoints] = extractFeatures(boxImage, boxPoints);
    [sceneFeatures, scenePoints] = extractFeatures(croppedImage, scenePoints);
    boxPairs = matchFeatures(boxFeatures, sceneFeatures);
    matchedBoxPoints = boxPoints(boxPairs(:, 1), :);
    matchedScenePoints = scenePoints(boxPairs(:, 2), :);


[tform, ~, ~, status] = estimateGeometricTransform(matchedBoxPoints, matchedScenePoints, ...
     'affine');
if (status ~= 1)
boxPolygon = [1, 1;...                           % top-left
        size(boxImage, 2), 1;...                 % top-right
        size(boxImage, 2), size(boxImage, 1);... % bottom-right
        1, size(boxImage, 1);...                 % bottom-left
        1, 1];                   % top-left again to close the polygon
    newBoxPolygon = transformPointsForward(tform, boxPolygon);

Poly = [newBoxPolygon(1,1) newBoxPolygon(1,2) newBoxPolygon(2,1) newBoxPolygon(2,2) ...
        newBoxPolygon(3,1) newBoxPolygon(3,2) newBoxPolygon(4,1) newBoxPolygon(4,2)...
        newBoxPolygon(5,1) newBoxPolygon(5,2)];

croppedImage = cat(3,croppedImage,croppedImage,croppedImage);
croppedImage = insertShape(croppedImage, 'Polygon', Poly, 'Color', 'green');
imshow(croppedImage);
else
croppedImage = cat(3,croppedImage,croppedImage,croppedImage);
croppedImage = insertText(croppedImage, [0 0], 'NO', 'TextColor', 'red');
imshow(croppedImage);
end

step(hVideoOut, rgbImage);
croppedImage = rgb2gray(croppedImage);
end
release(hVideoSource);
release(hVideoOut);
4

1 回答 1

3

之前还有其他可选输出status

[tform,inlierpoints1,inlierpoints2]  = estimateGeometricTransform(...)

您调用函数的方式status实际上包含inlierPoints1.

要获取状态并防止函数引发错误,您必须做的是:

[tform, ~, ~, status] = estimateGeometricTransform(matchedBoxPoints, matchedScenePoints, ...
     'projective');

更好的方法是在调用之前检查点数estimateGeometricTransform

if size(boxPairs, 1) >= 3
   tform = estimateGeometricTransform(..., 'affine')
else
   % skip this frame
end

对于投影,使用 4 而不是 3。

于 2013-12-25T21:02:51.693 回答