This MATLAB code for try catch not works properly.

try
boxPairs = matchFeatures(data1, data2);
catch
if boxPairs == 0
disp('there was an error')
else
plot(x,y)
end
end
This is the part of my program.
When i run above code, it completely runs without any error. But no operation is happening,. I checked my workspace. I am getting
boxPairs = 0
but i am not getting anything as disp in command window. What changes needed in program.?

1 Comment

  • "But no operation is happening" &nbsp what do you expect to happen?
  • "it completely runs without any error" &nbsp if so "not getting anything as disp in command window" is what to expect from this code

Sign in to comment.

 Accepted Answer

Hi Nimisha,
boxPairs can never be equal to 0. boxPairs is an M-by-2 matrix containing the indices of matched features. However, if no matches were found, then boxPairs is empty. So your error checking should look like this:
boxPairs = matchFeatures(data1, data2);
if isempty(boxPairs)
disp('No matches found')
else
plot(x,y)
end
The error you are getting comes from the estimateGeometricTransform, because it is getting empty points, because boxPairs was empty.

More Answers (1)

I think you've not understood how try ... catch ... end works. The code between try and catch will run until it encounters an error. By error, I mean something that stops the code running (usually an error instruction) and results in a red error message at the command line. When that happens, the code inside the catch ... end executes. However, if no error happens the body of the catch| is never executed.
If matchFeatures would create an error, then boxPairs would not even exists (unless it's predeclared before the try). Therefore your if statement inside the catch would be itself an error, trying to access an undeclared variable.
Most likely what happens is that matchFeatures returns 0 but does not error, so the catch statements are not executed, and your program ends. Maybe you meant just this:
boxPairs = matchFeatures(data1, data2);
if boxPairs == 0
disp('there was an error')
else
plot(x,y)
end

4 Comments

boxImage = imread('data1.jpg');
boxImage = boxImage(:,:,3);
sceneImage = imread('data3.jpg');
sceneImage = sceneImage(:,:,3);
boxPoints = detectSURFFeatures(boxImage);
scenePoints = detectSURFFeatures(sceneImage);
[boxFeatures, boxPoints] = extractFeatures(boxImage, boxPoints);
[sceneFeatures, scenePoints] = extractFeatures(sceneImage, scenePoints);
boxPairs = matchFeatures(boxFeatures, sceneFeatures);
if boxPairs == 0
disp('there was an error')
else
matchedBoxPoints = boxPoints(boxPairs(:, 1), :);
matchedScenePoints = scenePoints(boxPairs(:, 2), :);
figure;
showMatchedFeatures(boxImage, sceneImage, matchedBoxPoints, ...
matchedScenePoints, 'montage');
title('Putatively Matched Points (Including Outliers)');
[tform, inlierBoxPoints, inlierScenePoints] = ...
estimateGeometricTransform(matchedBoxPoints, matchedScenePoints, 'affine');
figure;
showMatchedFeatures(boxImage, sceneImage, inlierBoxPoints, ...
inlierScenePoints, 'montage');
title('Matched Points (Inliers Only)');
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];
newBoxPolygon = transformPointsForward(tform, boxPolygon);
figure;
imshow(sceneImage);
hold on;
line(newBoxPolygon(:, 1), newBoxPolygon(:, 2), 'Color', 'y');
title('Detected Box');
end
In this code i checked manually. Value of boxPairs is []
here i am still getting error,
Error using coder.internal.errorIf (line 9)
MATCHED_POINTS1 and MATCHED_POINTS2 do not have enough points.
Error in estimateGeometricTransform>checkRuntimeStatus (line 168)
coder.internal.errorIf(status==statusCode.NotEnoughPts, ...
Error in estimateGeometricTransform (line 150)
checkRuntimeStatus(statusCode, status);
Error in data (line 46)
[tform, inlierBoxPoints, inlierScenePoints] = ...
How to resolve it..?
I have no idea about the error you're getting (I'm not familiar with the computer vision toolbox) and I don't see what that's got to do with your original question, but isn't the error message clear enough? I understand it as: you don't have enough points in your feature to make a match.
I have a one image data1 and another one image data2. If the content of data1 image matches with part of data2 image, then finally matched points should shown as a square box in image, otherwise it should shown as an error message as in disp.!
I would suggest you start a new question as what you're now asking hasn't got anything to do with the title of the question. People familiar with the subject may not look at this question because of its title.
Unfortunately, as I've said, I don't know anything about the computer vision toolbox, so can't help you there.

Sign in to comment.

Tags

Asked:

on 17 Feb 2015

Edited:

on 19 Feb 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!