How to work in a specific area in an image ?

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

 Respuesta aceptada

Ameer Hamza
Ameer Hamza el 2 de Mayo de 2018
Editada: Ameer Hamza el 2 de Mayo de 2018
smallImage = imcrop(originalImage, rectangle);

23 comentarios

Theodor Al Saify
Theodor Al Saify el 2 de Mayo de 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
Ameer Hamza el 2 de Mayo de 2018
"i found the desired object in image and drew the rectangle"
How did you draw the rectangle?
Theodor Al Saify
Theodor Al Saify el 2 de Mayo de 2018
using the line function
Ameer Hamza
Ameer Hamza el 2 de Mayo de 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?
Use the rows and columns:
smallImage = originalImage(row1:row2, column1:column2, :);
Theodor Al Saify
Theodor Al Saify el 2 de Mayo de 2018
Editada: Theodor Al Saify el 2 de Mayo de 2018
This is the code that i used to draw the rectangle it uses a list of x and y .
Ameer Hamza
Ameer Hamza el 2 de Mayo de 2018
Editada: Ameer Hamza el 2 de Mayo de 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
Theodor Al Saify el 2 de Mayo de 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
Theodor Al Saify el 2 de Mayo de 2018
1 , 1 parameter is not correct it should be the bottom left of the detected object
Ameer Hamza
Ameer Hamza el 2 de Mayo de 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
Ameer Hamza el 2 de Mayo de 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
Theodor Al Saify el 2 de Mayo de 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
Theodor Al Saify el 2 de Mayo de 2018
this is the code i am using , i changed the parameters of your code in order to find the correct coordinate .
Ameer Hamza
Ameer Hamza el 2 de Mayo de 2018
So is your problem solved?
Theodor Al Saify
Theodor Al Saify el 2 de Mayo de 2018
no still i can't find the right starting point to start the rectangle
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
Theodor Al Saify el 2 de Mayo de 2018
Same result :( i tired all the combination possible
Ameer Hamza
Ameer Hamza el 3 de Mayo de 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
Theodor Al Saify el 3 de Mayo de 2018
Editada: Theodor Al Saify el 3 de Mayo de 2018
img5
Theodor Al Saify
Theodor Al Saify el 3 de Mayo de 2018

Image2

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
Theodor Al Saify el 3 de Mayo de 2018
Thank you it works .
Ameer Hamza
Ameer Hamza el 3 de Mayo de 2018
You are welcome.

Iniciar sesión para comentar.

Más respuestas (2)

Theodor Al Saify
Theodor Al Saify el 2 de Mayo de 2018

0 votos

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
Theodor Al Saify el 2 de Mayo de 2018
Editada: Theodor Al Saify el 2 de Mayo de 2018

0 votos

when using ur code i get this result. the starting point of the rectangle is not correct

2 comentarios

Ameer Hamza
Ameer Hamza el 2 de Mayo de 2018
@Theodor Al Saify, please use the comment section for discussion.
Theodor Al Saify
Theodor Al Saify el 2 de Mayo de 2018
Ok no problem

Iniciar sesión para comentar.

Categorías

Más información sobre Image Processing Toolbox en Centro de ayuda y File Exchange.

Preguntada:

el 2 de Mayo de 2018

Comentada:

el 3 de Mayo de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by