How to add dashed line to RGB image

5 visualizaciones (últimos 30 días)
Arthur Vieira
Arthur Vieira el 22 de Feb. de 2022
Comentada: Arthur Vieira el 25 de Feb. de 2022
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.

Respuesta aceptada

Simon Chan
Simon Chan el 22 de Feb. de 2022
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 comentarios
Simon Chan
Simon Chan el 23 de Feb. de 2022
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 el 25 de Feb. de 2022
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.

Iniciar sesión para comentar.

Más respuestas (1)

yanqi liu
yanqi liu el 23 de Feb. de 2022
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 comentarios
Arthur Vieira
Arthur Vieira el 23 de Feb. de 2022
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 el 24 de Feb. de 2022
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);

Iniciar sesión para comentar.

Categorías

Más información sobre Convert Image Type en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by