How to add dashed line to RGB image

5 次查看(过去 30 天)
I have an image that is a 1464 x 1464 x 3 uint8 matrix. I also have a binary image 1464 x 1464 logical with an single solid blob. I wish to overlay the blob's perimeter as a dashed line.
So far I've been using imoverlay() to add the perimeter of the blob to the image. But how do I make the outline dashed?
I = imread('coins.png');
I2 = imresize(I, [1464 1464]); % Emulate larger images, which seem to cause more issues.
BW = imbinarize(I2);
BW2 = bwconvhull(BW);
imshow(BW2);
overlay = uint8(bwperim(BW2));
I3 = imoverlay(I2, overlay, [.3 1 .3]);
figure(13);
clf;
tiledlayout(2, 2, 'TileSpacing', 'none', 'Padding', 'none');
nexttile(1);
imshow(I2);
nexttile(2);
imshow(BW2);
nexttile(3);
imshow(bwperim(BW2));
nexttile(4);
imshow(I3);
Bare in mind that in my application I'm processing frames of a video. I wish to produce a video like the original, but with detected features overlayed. This also means the final solution should maintain the image resolution, like in the example above for a solid line.
I've tried using plot() to achieve this, given it allows overlaying dashed line. But I can't seem to get the frame back cropped correctly, with same resolution and no extra white border.
% Following from above code:
coords = bwboundaries(BW2);
figure(14);
set(gcf, 'MenuBar', 'none'); % Prevents mouse hover from activating context menu on top-right. It would show on final video.
set(gcf,'color','w');
imshow(I2);
hold on;
plot(coords{1}(:,2), coords{1}(:,1), 'g--');
hold off;
frameData = getframe(gca);
frame = frameData.cdata; % Incorrecly cropped image.
Even if I use getframe(gca) I will get a weirdly cropped image.

采纳的回答

Simon Chan
Simon Chan 2022-2-22
Add the following line:
figure(14);
set(gcf, 'MenuBar', 'none'); % Prevents mouse hover from activating context menu on top-right. It would show on final video.
set(gcf,'color','w');
imshow(I2);
hold on;
axis image; % <--- Add this line
plot(coords{1}(:,2), coords{1}(:,1), 'g--');
hold off;
  3 个评论
Simon Chan
Simon Chan 2022-2-23
Then apply the same thing to all the figures as follows. Attached file for your reference.
I = imread('coins.png');
I2 = imresize(I, [1464 1464]); % Emulate larger images, which seem to cause more issues.
BW = imbinarize(I2);
BW2 = bwconvhull(BW);
coords = bwboundaries(BW2);
figure
set(gcf, 'MenuBar', 'none'); % Prevents mouse hover from activating context menu on top-right. It would show on final video.
set(gcf,'color','w');
for k = 1:4
if k == 1
imshow(I2);
elseif k == 2
imshow(BW2);
elseif k == 3
imshow(bwperim(BW2));
elseif k == 4
imshow(I2);
hold on;
plot(coords{1}(:,2), coords{1}(:,1), 'g--');
hold off;
end
axis image;
frame = getframe(gcf);
im = frame2im(frame);
[X,cmap] = rgb2ind(im,256);
if k == 1
imwrite(X, cmap, 'frame.gif', 'gif', 'Loopcount', 1);
else
imwrite(X, cmap, 'frame.gif', 'gif', 'WriteMode', 'append');
end
end
Arthur Vieira
Arthur Vieira 2022-2-25
Sorry, maybe I didn't explain what I meant by resolution. I actually meant size. The original frame is 1464x1464 (I2), but the final image (im) has a size dependent on the size the figure is displayed with. I worked around it by making the figure larger than the final size I wanted and then used imresize() to shrink it. In my case it was ok to have some quality loss in the final video, which might arise from resizing from whatever arbitrary size the figure is in.

请先登录,再进行评论。

更多回答(1 个)

yanqi liu
yanqi liu 2022-2-23
yes,sir,may be just insert line to image,such as
clc;
close all;
clear all;
I = imread('coins.png');
I2 = imresize(I, [1464 1464]); % Emulate larger images, which seem to cause more issues.
BW = imbinarize(I2);
BW2 = bwconvhull(BW);
imshow(BW2);
overlay = uint8(bwperim(BW2));
I3 = imoverlay(I2, overlay, [.3 1 .3]);
···
% Following from above code:
coords = bwboundaries(BW2);
figure
imshow(I2);
hold on;
plot(coords{1}(:,2), coords{1}(:,1), 'g--');
hold off;
% use this
xy = [coords{1}(:,2), coords{1}(:,1)]; xy2 = xy';
I3 = insertShape(I2,'Line',xy2(:)','Color','Green','LineWidth',2);
figure;imshow(I3);
  2 个评论
Arthur Vieira
Arthur Vieira 2022-2-23
Thanks for the answer. The function insertShape() I don't think supports dashed lines, am I right? I had also seen it before.
yanqi liu
yanqi liu 2022-2-24
yes,sir,may be use point to replace,such as
clc;
close all;
clear all;
I = imread('coins.png');
I2 = imresize(I, [1464 1464]); % Emulate larger images, which seem to cause more issues.
BW = imbinarize(I2);
BW2 = bwconvhull(BW);
imshow(BW2);
overlay = uint8(bwperim(BW2));
I3 = imoverlay(I2, overlay, [.3 1 .3]);
% Following from above code:
coords = bwboundaries(BW2);
figure
imshow(I2);
hold on;
plot(coords{1}(:,2), coords{1}(:,1), 'g--');
hold off;
% use this
xy = [coords{1}(:,2), coords{1}(:,1)]; xy2 = xy';
I3 = insertShape(I2,'Line',xy2(:)','Color','Green','LineWidth',2);
figure;imshow(I3);
tm = xy2'; tm = tm(1:5:end,:);
I3 = insertShape(I2,'FilledCircle',[tm 2*ones(length(tm),1)],'Color','Green');
figure;imshow(I3);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by