How to select a region if we click inside it?

Dear all,
I have a binary image same as the one I show here which contains few honeycomb regions connected to each other. Now I want a single honeycomb to be selected when the user makes a left click using the mouse inside the desired honeycomb. Any idea how
to do that?
Any suggestions will be appreciated.
Sincerely, Mueshoo

 Accepted Answer

Use WAITFORBUTTONPRESS and get(gca,'CurrentPoint') to detect the location of the click.
Then use INPOLYGON to detect which honeycomb was selected.

3 Comments

Thank you very much. I don't know how to use inpolygon and the help is not clear. Do you have some example?
For example the rows of V
V=[0 0 ; 1 0; 0 1]
are vertices of a triangle.
Since (0.1,0.2) lies inside the triangle, this returns true
>>inpolygon(0.1,0.2,V(:,1),V(:,2))
ans =
1
and conversely this returns false,
>> inpolygon(.1,1.2,V(:,1),V(:,2))
ans =
0
Thank you but still I couldn't do it. Let's say we have many objects in one image as I show here. Now if the user click by the mouse inside any object then this object will remain and all other objects will be removed. Is it possible to do that? Thank you very much for your time.
Mustafa

Sign in to comment.

More Answers (1)

Mustafa: Here's how to do it step by step:
  1. invert the image
  2. call imclearborder
  3. label the binary image with bwlabel
  4. call [column, row] = int32(ginput(1)) to let the user click somewhere
  5. get the label with labelNumber = labeledImage(row, column)
  6. extract the object with extractedObject = ismember(labeledImage, labelNumber)
  7. Make it binary again binaryImage = extractedObject > 0;
  8. display it with imshow(binaryImage, []);
See if you can program that up and attach your code if you have problems.

6 Comments

Thank you very much. I tried to solve it in a similar way and now it works perfectly. However, it is now working for my sample image that contain 6 objects as shown in the attached image. Do you know how to make a loop such that it can work for any number of objects in the image?
Please check my code
I = imread('regular_shapes.tif');
%
BW = im2bw(I);
%
b = bwboundaries(BW);
%
bb1 = b{1}; %x, y of the first object
bb2 = b{2}; %x, y of the 2nd object
bb3 = b{3}; %x, y of the 3rd object
bb4 = b{4}; %x, y of the 4th object
bb5 = b{5}; %x, y of the 5th object
bb6 = b{6}; %x, y of the 6th object
%
X_obj1 = bb1(:, 1);
Y_obj1 = bb1(:, 2);
X_obj2 = bb2(:, 1);
Y_obj2 = bb2(:, 2);
X_obj3 = bb3(:, 1);
Y_obj3 = bb3(:, 2);
X_obj4 = bb4(:, 1);
Y_obj4 = bb4(:, 2);
X_obj5 = bb5(:, 1);
Y_obj5 = bb5(:, 2);
X_obj6 = bb6(:, 1);
Y_obj6 = bb6(:, 2);
%
figure, imshow(BW);
hold on
[yCenter, xCenter] = ginput(1)
hold off
%
Selec_1 = inpolygon(xCenter,yCenter,X_obj1,Y_obj1);
Selec_2 = inpolygon(xCenter,yCenter,X_obj2,Y_obj2);
Selec_3 = inpolygon(xCenter,yCenter,X_obj3,Y_obj3);
Selec_4 = inpolygon(xCenter,yCenter,X_obj4,Y_obj4);
Selec_5 = inpolygon(xCenter,yCenter,X_obj5,Y_obj5);
Selec_6 = inpolygon(xCenter,yCenter,X_obj6,Y_obj6);
%
[L, num_Obj] = bwlabel(BW, 4);
Obj_1 = L ==1;
Obj_2 = L ==2;
Obj_3 = L ==3;
Obj_4 = L ==4;
Obj_5 = L ==5;
Obj_6 = L ==6;
%
if Selec_1 == 1
Obj = L ==1;
elseif Selec_2 == 1
Obj = L ==2;
elseif Selec_3 == 1
Obj = L ==3;
elseif Selec_4 == 1
Obj = L ==4;
elseif Selec_5 == 1
Obj = L ==5;
elseif Selec_6 == 1
Obj = L ==6;
end
%
figure, imshow(Obj);
I could solve it and made a for loop. Thank you very much.
Below is my code if any one interested on it.
clear all
clc
%
I = imread('regular_shapes.tif');
%
BW = im2bw(I);
%
figure, imshow(BW);
hold on
[yCenter, xCenter] = ginput(1)
hold off
%
b = bwboundaries(BW);
%
[L, num_Obj] = bwlabel(BW, 4);
%
for k = 1:num_Obj;
Obj = L ==k;
bb = b{k};
X_obj = bb(:, 1);
Y_obj = bb(:, 2);
Selec{k} = inpolygon(xCenter,yCenter,X_obj,Y_obj);
end
Selec;
%
Selec = [Selec{:}];
[value,index] = max(Selec);
%
Obj = L ==index;
%
figure, imshow(Obj);
help me, please.
I used this code but I need that the program show me the object in color the original image.
Have you any idea?
Try this
labeledImage = bwlabel(binaryImage, 8); % Label each blob so we can make measurements of it
% labeledImage is an integer-valued image where all pixels in the blobs have values of 1, or 2, or 3, or ... etc.
% Let's assign each blob a different color to visually show the user the distinct blobs.
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
imshow(coloredLabels);
Can you help me? please,
I need that the program recognize a image of another image, loading a image that is a circle and loading other that is a ellipse.
How I can make that the program recognize that the image is a circle or a ellipse ?

Sign in to comment.

Categories

Asked:

on 8 Jan 2014

Commented:

on 24 Oct 2016

Community Treasure Hunt

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

Start Hunting!