You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to work in a specific area in an image ?
2 views (last 30 days)
Show older comments
After using the object detection technique (https://fr.mathworks.com/help/vision/examples/object-detection-in-a-cluttered-scene-using-point-feature-matching.html) and after i found the desired object in image and drew the rectangle. How can i start working inside the rectangle, for example to draw the contour of the object inside the green rectanle? I am new to the image processing tools and to matlab
Accepted Answer
Ameer Hamza
on 2 May 2018
Edited: Ameer Hamza
on 2 May 2018
smallImage = imcrop(originalImage, rectangle);
23 Comments
Theodor Al Saify
on 2 May 2018
The problem is i don't have a rectangle, i have a line that was drawen. if i try smallImage = imcrop(originalImage, line); i get an error.
Ameer Hamza
on 2 May 2018
"i found the desired object in image and drew the rectangle"
How did you draw the rectangle?
Theodor Al Saify
on 2 May 2018
using the line function
Ameer Hamza
on 2 May 2018
Still, if you have two edges of the line, you can construct a rectangle. Can you show, what are the values you used to draw the line?
Image Analyst
on 2 May 2018
Use the rows and columns:
smallImage = originalImage(row1:row2, column1:column2, :);
Theodor Al Saify
on 2 May 2018
Edited: Theodor Al Saify
on 2 May 2018

This is the code that i used to draw the rectangle it uses a list of x and y .
Ameer Hamza
on 2 May 2018
Edited: Ameer Hamza
on 2 May 2018
Try the following lines
BB = [1, 1, size(boxImage,2) size(boxImage,1)];
newImage = imcrop(sceneImage, BB);
figure;
imshow(newImage);
or as @Image Analyst suggested,
newImage = sceneImage(1:size(boxImage,1), 1:size(boxImage,2), :);
figure;
imshow(newImage);
Theodor Al Saify
on 2 May 2018
the first solution cropped a piece of the whole detected object. in the second solution i received an Index exceeds matrix dimensions error.
Theodor Al Saify
on 2 May 2018
1 , 1 parameter is not correct it should be the bottom left of the detected object
Ameer Hamza
on 2 May 2018
Can you attach the live script you are using for this question? It will be easy to give a proper answer if the actual image is available.
Ameer Hamza
on 2 May 2018
@Theodor Al Saify, it is very difficult to give a correct line of code because I don't know what the variable name represent. If you can attach the file, it will be easy to give the correct answer.
Theodor Al Saify
on 2 May 2018
clc; warning('off','Images:initSize:adjustingMag'); boxImageRGB = imread('img5.JPG'); boxImage = rgb2gray(boxImageRGB); figure; imshow(boxImage); title('Image of a Box'); sceneImageRGB = imread('img2.JPG'); sceneImage = rgb2gray(sceneImageRGB); figure; imshow(sceneImage); title('Image of a Cluttered Scene');
boxPoints = detectSURFFeatures(boxImage); scenePoints = detectSURFFeatures(sceneImage); figure; imshow(boxImage); title('100 Strongest Feature Points from Box Image'); hold on; plot(selectStrongest(boxPoints, 100)); figure; imshow(sceneImage); title('300 Strongest Feature Points from Scene Image'); hold on; plot(selectStrongest(scenePoints, 300)); [boxFeatures, boxPoints] = extractFeatures(boxImage, boxPoints); [sceneFeatures, scenePoints] = extractFeatures(sceneImage, scenePoints); boxPairs = matchFeatures(boxFeatures, sceneFeatures); 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]; % top-left again to close the polygon newBoxPolygon = transformPointsForward(tform, boxPolygon); figure; imshow(sceneImage); hold on; line(newBoxPolygon(:, 1), newBoxPolygon(:, 2), 'Color', 'y'); title('Detected Box'); fprintf('new Box Polygon x ') disp(newBoxPolygon(:, 1)); fprintf('new Box Polygon y ') disp(newBoxPolygon(:, 2));
%crop the detected zone BB = [newBoxPolygon(1, 1), newBoxPolygon(1, 1), size(boxImage,1) size(boxImage,2) ]; newImage = imcrop(sceneImage, BB); figure; imshow(newImage);
% newImage = sceneImage(1:size(boxImage,1),1:size(boxImage,2),:); % figure; % imshow(newImage);
Theodor Al Saify
on 2 May 2018
this is the code i am using , i changed the parameters of your code in order to find the correct coordinate .
Ameer Hamza
on 2 May 2018
So is your problem solved?
Theodor Al Saify
on 2 May 2018
no still i can't find the right starting point to start the rectangle
Ameer Hamza
on 2 May 2018
It appears that you made a mistake in this line. the second element should be newBoxPolygon(1, 2).
BB = [newBoxPolygon(1, 1), newBoxPolygon(1, 2), size(boxImage,1) size(boxImage,2) ];
try it now.
Theodor Al Saify
on 2 May 2018
Same result :( i tired all the combination possible

Ameer Hamza
on 3 May 2018
Please also share img2.jpg and img5.jpg as I am not able to replicate the problem as described by you.
Theodor Al Saify
on 3 May 2018
Edited: Theodor Al Saify
on 3 May 2018

img5
Theodor Al Saify
on 3 May 2018
Image2
Ameer Hamza
on 3 May 2018
There!!! Now I am able to see properly what is going on. Replace BB line with following
BB = [min(newBoxPolygon(:, 1)), min(newBoxPolygon(:, 2)), size(boxImage,1) size(boxImage,2) ];
Theodor Al Saify
on 3 May 2018
Thank you it works .
Ameer Hamza
on 3 May 2018
You are welcome.
More Answers (2)
Theodor Al Saify
on 2 May 2018
i am working on this image, i detected the desired object and drew a line around it. Now i want to crop the detected object to start working in it's area.

Theodor Al Saify
on 2 May 2018
Edited: Theodor Al Saify
on 2 May 2018
when using ur code i get this result. the starting point of the rectangle is not correct

2 Comments
Ameer Hamza
on 2 May 2018
@Theodor Al Saify, please use the comment section for discussion.
Theodor Al Saify
on 2 May 2018
Ok no problem
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)