Вопрос

In the Matlab's help about estimateGeometricTransform says, that this function may return the status if it have some error (for example 'matchedPoints1 and matchedPoints2 inputs do not contain enough points.') It should be written like this:

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

But in my case it doesn't work.

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

After that command I'm using 'IF'-statement for checking the status and if it's not equal one, I need to do some commands. But I just get the error in the console: MATCHED_POINTS1 and MATCHED_POINTS2 do not have enough points. and the program stops.

What's wrong?

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);
Это было полезно?

Решение

There are other optional outputs before status:

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

The way you are calling the function, status actually contains inlierPoints1.

What you have to do to get the status and prevent the function from throwing an error is this:

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

A better way is to check the number of points before calling estimateGeometricTransform.

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

For projective, use 4 instead of 3.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top