Clear Filters
Clear Filters

Problem with 'InsertShape'?

16 views (last 30 days)
Tong Wang
Tong Wang on 21 Jul 2018
Edited: DGM on 23 Nov 2023
I want to add masks over my processed image, as you can see, after using 'InsertShape' i get such a strange pic, i marked the area which is wrong generated, you can see inside the yellow circle the lines are totally mishandled, inside the blue rectangle lines are almostly broken...
This is code
for j = 1:MaskNum
Points = Masks{2,j}; % Get all vertexes of a mask
[a,~] = size(Points);
B{j} = reshape(Points.',1,a*2); % Reshape the vertex matrix to fit the format of insertshape
end
% Fullimage is the processed image
withline = insertShape(FullImage,'Polygon',B,'LineWidth',8,'color',color*255);
imwrite(withline,s)
How to fix this problem?
  3 Comments
Antje Nuthmann
Antje Nuthmann on 9 Sep 2023
my workaround was to insert each shape separately using a loop across all polygons.
DGM
DGM on 10 Sep 2023
@Antje Nuthmann, bug or not, nothing will come of this question until someone provides enough information to allow troubleshooting. We need to know what specific inputs to insertShape() cause the behavior.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 9 Sep 2023
There is probably extra points in the outlines, "Points". If you had attached your data, we could have helped you.
  1 Comment
DGM
DGM on 10 Sep 2023
Edited: DGM on 10 Sep 2023
I have to question whether a user can enter a valid Mx2 vertex list that would be expected to cause the vertex ordering of the outer polygon that forms the border line to become twisted. It looks like the end miter is flipped inside out, causing the self-intersection. The user has no control over that ordering.
At least that's what OP's image and (incomplete) code seems to show. I don't have CVT, so I can't dig through it, but at first glance, it looks like a bug. No telling that the other comments are the same though.
On the other hand, I'd question how a bug like that could hide.

Sign in to comment.


DGM
DGM on 10 Sep 2023
Edited: DGM on 10 Sep 2023
Well, unless I'm missing something regarding intended usage, that's a bug.
% a blank image with the same geometry as OP's image
inpict = zeros([896 1586 3],'uint8');
% approximately the same vertices, but not necessarily the same order
% i'm consistently not providing a redundant closing vertex
points = {[111 104; 117 284; 138 373; 416 356; 496 471; 766 624; 814 585; 821 527; 795 474; 614 351; 410 61]; % white
[884 118; 793 360; 1059 465; 1395 448; 1462 266; 1177 37]; % blue
[286 407; 253 645; 188 787; 669 872; 771 806; 847 770; 853 712]; % red
[1182 645; 987 742; 1410 823; 1435 780; 1459 619]}; % green
colors = [1 1 1; 0 0 1; 1 0 0; 0 1 0];
% call insertShape()
outpict = insertShape(inpict,'Polygon',points,'LineWidth',8,'color',colors*255);
% view the result and annotate the specified vertices
imshow(outpict,'border','tight'); hold on
for k = 1:numel(points)
plot(points{k}(:,1),points{k}(:,2),'mo','linewidth',3)
end
% zoom in on the notable areas
c = [271 523; 852 736; 1084 694; 838 245; 1175 34; 1461 616]; % roi centers
for k = 1:size(c,1)
ROIstack{k} = imcrop(outpict,[c(k,:)-100 200 200]);
end
figure
montage(ROIstack)
% try Antje Nuthmann's workaround
outpict = inpict;
for k = 1:numel(points)
outpict = insertShape(outpict,'Polygon',points{k}, ...
'LineWidth',8,'color',colors(k,:)*255);
end
figure
imshow(outpict,'border','tight')
There's no funny business in vertex data here. The specified polygons contain no duplicate vertices, and are not self-intersecting, yet the derived polygon which forms the border is self-intersecting. This fails regardless of whether the polygon is closed with a duplicate vertex (which shouldn't be necessary).
When I tried this exact same routine using peppers.png and a simple set of vertices defining rectangles, I got no such folded miters. I tried it with other irregular polygons, and it seems sometimes they'd fold.
Anybody with CVT should be able to try submitting a bug report. Feel free to link to this thread if you do. I'm not going to try to do bug reports on things I can't run outside the forum.
  2 Comments
Image Analyst
Image Analyst on 10 Sep 2023
I havev that toolbox but I don't have the poster's data. Not sure why he didn't upload it originally or wouldn't upload it despite being asked a few times. But anyway, this was 5 years ago and we will never get the data so I consider the subject closed.
DGM
DGM on 10 Sep 2023
Edited: DGM on 23 Nov 2023
I don't have OP's data either, but I can replicate the problem with my own lookalike data as shown above. While more points of reference from other users might be of additional benefit, OP's data is no longer necessary.
OP is gone, but others have made more recent remarks, so I don't see OP as being the user in need. Either way, a bug is not a problem specific to a user. It's something that should be fixed regardless of whether the last person to publicly complain about it gave up and went away.
Of course, I'm still open to someone telling me why this is expected behavior (not that I would accept the notion that it should be). I'm not familiar with using insertShape(), because I don't have CVT. All I know is the documentation, and I know the documentation has (in the case of this specific function) been wrong before. That creates a need for certainty that's part of why I would want to dig deeper.
EDIT: I went ahead and submitted a bug report anyway.

Sign in to comment.

Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!