user1809923
user1809923

Reputation: 1255

Connect several imrect

I'd like to use several interactive rectangles (imrect) on an axis. Some of these rectangles are supposed to be connected. That is:

I however do not know how to do this. I can't find a callback which is exectued once a color of a rectangle has been changed. I also don't know how to modify the appearance of a rectangle (other than the color) to show, that the rectangle has been selected. And, sadly, I don't know how to detect when a user clicked into an imrect.

Thank you!

Upvotes: 1

Views: 151

Answers (1)

Shai
Shai

Reputation: 114866

A partial answer:
Suppose you have several groups of rectangles, represented as a cell array (cell element per group) with each group is a ni-by-4 array representing the ni rectangles. For example:

gRects = { [ 20 10 200 300; 40 60 200 100 ], ...
           [ 50 50 150 150 ], ...
           [ 150 200 30 50 ; 150 10 100 100 ; 200 30 40 100 ] };

That is, you have three groups, 2 rectangles in the first group, one in the second and three in the third.

You can now plot them, and store handles for future modification

ng = numel( gRects ); %// how many groups
clrs = rand( ng,3 ); %// randomly select a color per group
img = imread('cameraman.tif'); %// a backgroud ?
figure;
imshow( img, 'border', 'tight' );hold on;
grH = cell( 1, ng );
for gi=1:ng
    ni = size( gRects{gi}, 1 ); %// num rects in current group
    grH{gi} = zeros( 1, ni );
    for ri = 1:ni
        grH{gi}(ri) = rectangle('Position',gRects{gi}(ri,:),...
            'EdgeColor', clrs(gi,:), 'LineWidth', 2, 'LineStyle', ':' );
    end
end

enter image description here
Now, if you want to "highlight" one of the groups, for example the third one:

gi = 3;
ni = size( gRects{gi}, 1 ); %// num rects in current group    
for ri = 1:ni
    set( grH{gi}(ri),...
        'LineWidth', 4, 'LineStyle', '-' );
end

enter image description here

Upvotes: 2

Related Questions