Mouse Selection with the

Mouse Selection with the

Post by Je » Sat, 07 Jan 2006 00:26:12


Hi,

try this :

figure

uicontrol('style','frame','units','normalized',...
'position',[.02 .02 .07 .07])

for n=1:10

x=rand;
y=rand;

rectangle('position',[10*x 10*y x+5 y+5],...
'facecolor',rand(3,1),'buttondownfcn',...
['title(num2str(gcbo));'...
'set(findobj(''style'',''frame''),'...
'''backgroundcolor'',get(gcbo,''facecolor''));']);

end

Hope this helps
Je
 
 
 

Mouse Selection with the

Post by Nevine Jac » Sat, 07 Jan 2006 01:24:24

Hi Jerome,

Thanks a lot for the help. Now that we are at this point, let me give
you more details of what I want the code to do.

Instead of displaying the color of the selected rectangle in the
left-bottom corner, could I know which rectangle was clicked? For
example, I click the 5th rectangle in the image and I would like the
result "5". Also, could this be done without 'facecolor', as the
portions of the image below the rectangle is concealed by the
facecolor?

I shall use your suggestion as a guideline and keep working along
those lines. Do help me when you find the time.

Thanks,
Nevine

 
 
 

Mouse Selection with the

Post by Je » Sat, 07 Jan 2006 01:31:46


What do you mean by "the 5th rectangle" ?

Did you see that the title of the graph change when you click on any
rectangle ? It gives you the handle of the selected rectangle (see
gcbo).

Je
 
 
 

Mouse Selection with the

Post by Nevine Jac » Sat, 07 Jan 2006 02:03:50

I have a binary image which have several objects in them. The objects
have been labelled using the "bwlabel" function and it's bounding box
determined using the "regionprops" function.

Here's a gist of the code (using your suggestion):

**********

[L,maxi] = bwlabel(bin_image);

stats = regionprops(L,'BoundingBox');

for n=1:maxi

h(n) = rectangle('position',stats(n).BoundingBox,...
'facecolor',rand(3,1),'buttondownfcn',...
['title(num2str(gcbo));'...
'set(findobj(''style'',''frame''),'...
'''backgroundcolor'',get(gcbo,''facecolor''));']);

end

**********

So by me selecting certain rectangle, I need to emiminate the object
associated with the rectangle. For example:
1) if I select the 5th rectangle, I need to associate the click with
" h(5) "
2) then using this value of h(5), I would know that it corresponds to
the 5th rectangle (or 5th object)
3) Using this result, I could eliminate the 5th object by using the
command L(L==5) = 0;

So essentially my question right now is, how to perform step (1)?

Thanks so much for your help,
Nevine
 
 
 

Mouse Selection with the

Post by Je » Sat, 07 Jan 2006 17:11:47

Hi,

read this one :

Edward Cooke, "Obtaining the number of a 'labeled' object in an image by mouse?" #, 7 Feb 2005 7:13 pm </WebX?50@@.eefb11f>


Je
 
 
 

Mouse Selection with the

Post by Nevine Jac » Sun, 08 Jan 2006 00:12:22

Hi Jerome,

Thanks for the suggestion, but I would like to stick with
BoundingBoxes. The reason is because some of the objects and are
really thin and long (for example a line which is 1 pixel thick and
600 pixels long and oriented at 45 degrees, in which case, it would
be more convenient if one could click anywhere in the bounding box
rather than clicking exactly on the 1 pixel thick object).

So according to what you mentioned in the previous threads, instead
of me clicking in the bounding box and the color of the box appearing
on the sides of the window, ideally, i would like a certain value pop
out. And I probably could use that value to determine the particular
object.

Currently, I have developed an inefficient working code using the
"ginput" function but I would like something more elegant as those
mentioned by you.

Thanks,
Nevine
 
 
 

Mouse Selection with the

Post by Je » Sun, 08 Jan 2006 02:31:22

Try this :

figure

uicontrol('style','frame',...
'units','normalized',...
'position',[.02 .02 .07 .07])

rectangle('position',[1 1 5 5],...
'edgecolor',rand(3,1),...
'linewidth',2)

rectangle('position',[1 -6 5 5],...
'edgecolor',rand(3,1),...
'linewidth',2)

set(gca,'buttondownfcn','bdf',...
'xlim',[0 7],'ylim',[-10 10]);

Create a bdf.m function as :

function bdf

h=flipud(findobj('type','rectangle'));

cp=get(gca,'currentpoint');

flag=zeros(length(h),1);

for n=1:length(h)

pos=get(h(n),'position');
flag(n)=inpolygon(cp(1,1),cp(1,2),...
[pos(1) pos(1)+pos(3) pos(1)+pos(3) pos(1)],...
[pos(2) pos(2) pos(2)+pos(4) pos(2)+pos(4)]);


end

idx=find(flag);
title(num2str(idx))

col=get(h(idx(1)),'edgecolor');
set(findobj('style','frame'), ...
'backgroundcolor',col)

Je
 
 
 

Mouse Selection with the

Post by Nevine Jac » Sun, 08 Jan 2006 05:43:02

Hi Jerome,

Thanks a lot for the help. The code works well when I use a plain
figure. But I'm not able to get it right when I use my image.

Here's the portion of my code which I modified according to your
code.

*****************

[L,maxi] = bwlabel(f); % f is binary image
stats = regionprops(L,'BoundingBox');

uicontrol('style','frame',...
'units','normalized',...
'position',[.02 .02 .07 .07])


for n=1:maxi
rectangle('position',stats(n).BoundingBox,...
'edgecolor',rand(3,1),...
'linewidth',2)
end

set(gca,'buttondownfcn','bdf', 'xlim',...
[1 size(L,2)],'ylim',[1 size(L,1)]);

% the "bdf" function is unaltered

*********************

I am an amateur when it comes to uicontrols and similar stuff and
hence the reason for all these questions. Sorry about that.

Thanks,
Nevine
 
 
 

Mouse Selection with the

Post by Nevine Jac » Sun, 08 Jan 2006 05:54:09

Also would your code work if one rectangle occured within another
rectangle? The reason I ask this is because the "inpolygon" function
would give a "true" result for both the inner and the outer
rectangle(if I clicked the "inner" rectangle).

- Nevine
 
 
 

Mouse Selection with the

Post by Nevine Jac » Sun, 08 Jan 2006 06:50:16

It also does not seem to work for very small rectangles (rectangles
nearly the size of the mouse pointer or smaller).

Thanks,
Nevine
 
 
 

Mouse Selection with the

Post by Kranth » Sat, 14 Jan 2006 18:10:06

you can store the recatangle index along with the handle of the
rectangle in the 'UserData' field, and use it when the rectangle is
clicked.
 
 
 

Mouse Selection with the

Post by Nevine Jac » Sat, 21 Jan 2006 01:24:37

i All,

I had forsaken this part of the code for some time now and I have
been in the process of resurrecting it for the last two days. With
great help from Jerome and significant others, I have advanced well
but I am now stuck at a certain point and would appreciate your help.
(The details below are fairly long but they should be simple to
comprehend. So don't get vexed by looking at the long message)

Here's what I require the code to do:
1) Draw rectangles (yellow colored) around the objects of the binary
image
2) Once a rectangle is selected by mouse clicking, the rectangle
should be of different color (green) and the object below it should
be deleted (need not be displayed though).
3) Once I am done selecting the rectangles (that is, objects) to be
deleted, there must be a way by which no more rectangles can be
selected and the new image with objects removed is displayed
(probably by clicking a "Done with Selection" button on the Figure
Window).

Here's what the code does:
1) Draws rectangles (yellow colored) around the objects of the binary
image
2) Once a rectangle is selected by mouse clicking, the rectangle is
of green color BUT the object below it is not deleted.
3) There is NO "Done with Selection" button.

Here's my code (I've modified it a bit to keep it simple):

******************************

% Declaring these variables as Global
global Ltemp stats

% img is a binary image with objects in them
[L,maxi] = bwlabel(img);
stats = regionprops(L,'BoundingBox');

colormap(gray)

im=image(L);
set(im,'hittest','off');

% Plots yellow rectangles around each object
for i = 1:maxi
rectangle('Position',stats(i).BoundingBox,
'EdgeColor','y','linewidth',2,'hittest','off');
end

set(gca,'buttondownfcn','bdf');

************************************

function bdf

% Declares the variables to be global (again)
global Ltemp stats

h=flipud(findobj('type','rectangle'));

% Gets position of mouse click
cp=get(gca,'currentpoint');
flag=zeros(length(h),1);

% Checks which rectangle the mouse click corresponds to
for n=1:length(h)
pos=get(h(n),'position');
flag(n)=inpolygon(cp(1,1),cp(1,2),...
[pos(1) pos(1)+pos(3) pos(1)+pos(3) pos(1)],...
[pos(2) pos(2) pos(2)+pos(4) pos(2)+pos(4)]);
end

idx=find(flag);

% Displays the rectangle number (object number) as the title
title(num2str(idx))

% Get handle of the original image
h1 = get(gca);

% Store value of original image in Ltemp
% Ltemp is equal to original value of L, for the first time
Ltemp = get(h1.Children(end),'CData');

% Delete object clicked by mouse
Ltemp(Ltemp==idx) = 0;

% Set Original Value of Image to Ltemp
set(h1.Children(end),'CData',Ltemp);

% Set color of selected rectangle to green
rectangle('Position',stats(idx).BoundingBox, 'EdgeColor','g');

*****************************************

From the above codes, I see that each time I click on a rectangle,
the object below is deleted and the new image stored in Ltemp. So
after selecting several rectangles, I finally see that the final
value of Ltemp has all those selected objects deleted. This is
ofcourse what I want, but, how do I stop selecting the rectangles and
finally display a new image with all objects deleted?

Also, this works only when I use the "image" function. It does not
work for imshow. That is, when I replace the line "im = image(L)" by
"im = imshow(L)", I get an error.

I would