Plot Boxes then Plot them in the original photo ?

3 views (last 30 days)
I want to plot only the following boxes (5,6,7,8,9,10,12,13) from 19 box which their coordinates stored in the attached file "Boxes".
The color of the boxes should be as the following:
5,6 ==> Same color
7,8 ==> same color
9,10 ==> same color
12,13 ==> same color
I tried the following code to plot the specific mentioned boxes (5,6,7,8,9,10,12,13) , but the size of the boxes are too big compared with their original size(you can see their original size when you plot all the boxes):
%% Plot bboxes
p=cellfun(@polyshape, boxes);
plot(p); xlabel x; ylabel y
set(gca, 'YDir','reverse')
After ploting the boxes I want to plot them on the original photo(the attached one).??
  6 Comments
DGM
DGM on 24 May 2022
Edited: DGM on 24 May 2022
load boxes.mat
A = imread('Image.jpg');
imshow(A); hold on
boxes = num2cell(boxes,[1 2]);
p = cellfun(@polyshape, boxes);
p = p([5 6 7 8 9 10 12 13]);
colororder(repelem(lines(7),2,1))
plot(p);
xlabel x;
ylabel y
set(gca, 'YDir','reverse')

Sign in to comment.

Accepted Answer

DGM
DGM on 24 May 2022
Edited: DGM on 24 May 2022
The above comment should suffice if all you want is to plot with polyshape. If you actually want to modify the image without changing the image geometry or risking changing it outside of the specified boxes, then just modify the image directly instead of relying on the figure rendering as a cumbersome ad-hoc image compositor.
load boxes.mat
inpict = imread('Image.jpg');
imshow(inpict); hold on
alph = 0.4;
boxidx = [5 6 7 8 9 10 12 13];
boxcolors = repelem(lines,2,1);
for k = 1:numel(boxidx)
ROI = images.roi.Polygon(gca);
ROI.Position = boxes(:,:,boxidx(k));
mask = alph*createMask(ROI);
inpict = replacepixels(boxcolors(k,:),inpict,mask);
delete(ROI)
end
This uses MIMT replacepixels() to do the composition. You can do the same using basic tools, but it will be more verbose and you'll have to do all the class handling. This is an example of using basic tools to do composition using both logical and linear masks:
EDIT: something like this:
load boxes.mat
inpict = imread('Image.jpg');
imshow(inpict); hold on
alph = 0.4;
boxidx = [5 6 7 8 9 10 12 13];
boxcolors = repelem(lines,2,1);
inpict = im2double(inpict);
for k = 1:numel(boxidx)
ROI = images.roi.Polygon(gca);
ROI.Position = boxes(:,:,boxidx(k));
mask = alph*createMask(ROI);
inpict = permute(boxcolors(k,:),[1 3 2]).*mask + inpict.*(1-mask);
delete(ROI)
end
inpict = im2uint8(inpict);
  2 Comments
DGM
DGM on 25 May 2022
Edited: DGM on 25 May 2022
1:
The axes 'colororder' property is an Mx3 colormap which is used by certain plotting tools as a default source for object colors. Its what defines the default line colors used by plot() when no colors are explicitly specified. In the case of your example, it's also used by plot() when plotting the polyshape objects.
For sake of consistency, I decided to use the same colors as before, just reordered to suit. The default value of the axes colororder property is lines(7). So the following
colororder(repelem(lines(7),2,1))
takes lines(7) (a 7x3 colormap) and duplicates each row to form a 14x3 colormap. This modified colormap is assigned to the axes colororder property by using the colororder() function.
The result is then that as each polyshape is plotted, the colors they are given get drawn from a list of duplicates. So the first and second shapes have the same color, and so on.
2:
You must have something else plotted in the same axes simultaneously, though it might not be intentional (or visible). If you had other things being displayed prior with 'hold on', that would be one cause, or if you modified the example code I gave in particular ways, it might also cause it. There can only be one image object in the axes when using the ROI tools. If there is a bunch of preceding code that had been plotting stuff in the axes, it would probably be a good idea to clear the axes/figure prior to starting this routine.
clf % clear the figure and child axes
% then do everything
Otherwise, you might want to show how exactly you're implementing the process.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!