imrect object - delete notification

After creating an 'imrect' interactive object, in the context menu there is a delete option. Is there a callback to register when the 'imrect' object is deleted.
What I have so far:
im_rect = imrect(ax, draw_rect);
rectId = addNewPositionCallback(im_rect, @(pos)positionUpdateCallback(pos));
So I have access to the position update callback id. But I don't see how can I register any notifiers for the delete event. There is a 'removeNewPositionCallback', but I don't know whether this callback will be triggered on a delete. I checked both 'imrect' and 'imroi' help sections.
Maybe an alternative would be to add a custom delete command to the context menu, with a callback. Has anyone tried that ?

2 件のコメント

Image Analyst
Image Analyst 2016 年 11 月 4 日
Why do you need to use the delete option?
Gerti Tuzi
Gerti Tuzi 2016 年 11 月 4 日
Because it is an interactive tool, for which I need to be notified when the user interacts with it. The drawn box highlights a section of the image. Hence, when the user says "no", I would like my model/data to also reflect that.

サインインしてコメントする。

 採用された回答

Adam
Adam 2016 年 11 月 4 日
編集済み: Adam 2016 年 11 月 4 日

0 投票

Unfortunately imrect and its similar functions are rather arcaic and don't really conform to more modern Matlab graphics objects, but it does derive from 'handle' and the handle class includes the following event
ObjectBeingDestroyed
which you can listen to as e.g.
addlistener( im_rect , 'ObjectBeingDestroyed', @(src,evt) disp( 'Rectangle died' ) )

6 件のコメント

Gerti Tuzi
Gerti Tuzi 2016 年 11 月 4 日
A similar approach to that, which I tried, was to extend the 'imrect' class, and add a listener that gets executed in the destructor of this extended class. The problem, that I didn't mention, is that I am trying to label unique objects through a sequence of images. Using this tool, when a new image is loaded - or rather, when the rectangles disappear - they are also destroyed. But this doesn't mean that the user is rejecting these objects. So ideally I would have liked to listen to the interactive "delete" event, as opposed to the destruction of the 'imrect' object object itself.
Adam
Adam 2016 年 11 月 4 日
I'm not sure I understand what the difference is between listening to the imrect getting deleted or the instruction that causes the imrect to be deleted, unless you want to stop the imrect from being deleted when the user chooses to delete it.
Gerti Tuzi
Gerti Tuzi 2016 年 11 月 4 日
When the user generates a rectangle on top on an image, the underlying image data belongs to an object of interest. When the user selects the "delete" option from the context menu, he is saying "this is an invalid object" which means do not keep the underlying image data (the portion selected via the rectangle). On the other hand, if the user draws a rectangle - pointing that the underlying image data belongs to an object of interest - and if the user simply loads a new image, the object of interest generated from the previous image must be kept. However, "imrects" are destroyed when they a removed from display. Keeping a blind listener to the destructor does not tell me whether the user chose to delete it - i.e. an invalid object of interest - or whether the rectangle was destroyed because a new image was loaded on the same axis.
Gerti Tuzi
Gerti Tuzi 2016 年 11 月 4 日
Maybe a "keep/submit" event can be added to flag that the user is done drawing. At any rate, I will accept your answer as it does answer within the confines of the given object definition.
Adam
Adam 2016 年 11 月 4 日
編集済み: Adam 2016 年 11 月 4 日
Can you not catch this under the image load button? If an imrect object is existing and valid when the user loads a new image then do something with the old image to keep it or set some variable so that when the imrect is deleted then you will know whether it was the user deleting it or it being deleted as a result of a new image being loaded.
Gerti Tuzi
Gerti Tuzi 2016 年 11 月 4 日
Adam, please see my answer for a possible solution.

サインインしてコメントする。

その他の回答 (1 件)

Gerti Tuzi
Gerti Tuzi 2016 年 11 月 4 日

0 投票

For anyone that may need it, I was able to "override" the context's menu "delete" behavior.
1 - Extend "imrect" to your custom class. Note that "imrect" derives from "imroi"
classdef imrectcust < imrect
2 - Copy "imroi"'s 'addRoiDeleteMenuItem(obj, hGroup)' function into your extended class and rename it to something else. Note that this is a static function in "imroi"
3 - Instead of adding a new delete menu item, search 'hMenu''s children for the delete menu item's callback and set it to your desired callback.
function addCustRoiDeleteMenuItem(thisObj, hGroup)
...
% ---- This is code from the original 'addRoiDeleteMenuItem()' ----
% Because the children all have the same context menu handle, grab the
% first if there's more than one.
if iscell(hMenu)
hMenu = hMenu{1};
else
hMenu = hMenu(1);
end
% -------------
% Find the delete item, and over-write the call back method
for ii = 1:length(hMenu.Children)
hm = hMenu.Children(ii);
if(strcmpi(hm.Tag, 'delete cmenu item'))
% 'onUserDelete()' is the custom call back
hm.Callback = @(~,~) onUserDelete(thisObj);
end
end
end
Make sure that in your desired callback you also call your own destructor to retain original "imroi" behavior
function onUserDelete(thisObj)
... handle your custom "delete" behavior business
% Make sure you call your own destructor, which will destroy the object
thisObj.delete();
end
This should also allow you to add additional context menu items in the extended 'imrect' class.

カテゴリ

ヘルプ センター および File ExchangeData Type Identification についてさらに検索

質問済み:

2016 年 11 月 3 日

コメント済み:

2016 年 11 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by