How to delete inserted shapes via user interface
9 views (last 30 days)
Show older comments
I am currently working on making a GUI application using GUIDE that allows the user to draw bounding boxes around objects in an image. I am using the getrect and insertShape functions to draw the boxes, these are called after the user presses a GUI button. Now, I would like to add a functionality that allows the user to remove previously drawn boxes, again after pressing a dedicated GUI button. I couldn't find a way to delete shapes, are their any functions that allow us to do so? Optimally, the user should be able to click the border of any box and it should be deleted. I hope there is a way to do this.
Currently, the values obtained by the getrect function are stored in an n-by-4 array. This array is then used as an input argument for the insertShape function which is called after every drawing of a box. Now, what I want to do is to get an object handle by clicking on a shape, which allows me to retrieve the 4 values used to draw the selected box. Then I could delete the row that corresponds to these values from my array. Unfortunately, I have not found a way to do this. Hopefully, somebody here knows a good way to do this.
Thanks in advance.
1 Comment
Shruti Shivaramakrishnan
on 13 Jun 2017
This link might provide further information on deleting the box line-wise: https://www.mathworks.com/matlabcentral/newsreader/view_thread/246242
Answers (1)
DGM
on 12 Sep 2023
Why is this a ridiculous idea? The output of insertShape() is a raster image. There aren't shape objects to select. There's nothing to delete but the image content itself, and unless you've saved prior copies of the image, there's nothing to revert that image region to.
That should all be plainly obvious, but let's "delete" an "object" anyway.
% some inputs
inpict = imread('peppers.png');
shapetype = 'rectangle';
linewidth = 15;
nshapes = 2;
deletenum = 1; % which shape to remove
CT = jet(nshapes);
% prepare
wpict = inpict; % keep a copy of the original
CT = CT*255; % because insertShape() is dumb like that
% display the image
imshow(wpict,'border','tight')
% draw shapes
%R = zeros(nshapes,4); % for interactive use
R = [39 32 111 74; 288 157 202 172]; % for forum demo only
%R = [49 48 289 195; 211 148 260 207]; % overlapping example
for k = 1:nshapes
% if you were doing this interactively, you'd do this
% but interactive things can't be run on the forum, so use the pre-generated R
%R(k,:) = getrect(gca);
wpict = insertShape(wpict,shapetype,R(k,:),'LineWidth',linewidth,'color',CT(k,:));
imshow(wpict,'border','tight')
end
% delete the specified shape
% create a mask which selects the object
mask = zeros(size(wpict));
mask = insertShape(mask,shapetype,R(deletenum,:),'LineWidth',linewidth,'color',[1 1 1]);
mask = repmat(any(logical(mask),3),[1 1 3]);
% replace that region
wpict(mask) = inpict(mask);
imshow(wpict,'border','tight')
Well, okay. We deleted a selected rectangle, so what's wrong with that? First, this was only possible because we kept track of all of the shape coordinates. Second, we kept a copy of the unadulterated image.
Okay, but that doesn't seem like such a big deal. What's wrong with that? Easy. This only works if the objects don't overlap or touch. Let's say these two rectangles overlapped:

In the general case where you need to consider many potentially overlapping and/or transparent shapes in the same image, and you want to delete any one from the set, the way you would do it is by keeping track of all the parameters used to create each shape, and then re-generating the image from the beginning.
That's why you wouldn't do it this way. If you want to be able to drag and drop paragraphs of text, don't write your novel on a Selectric. If you want dynamic object manipulation, don't work in a raster image.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

