How to add dashed line to RGB image
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Arthur Vieira
le 22 Fév 2022
Commenté : Arthur Vieira
le 25 Fév 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.
0 commentaires
Réponse acceptée
Simon Chan
le 22 Fév 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 commentaires
Simon Chan
le 23 Fév 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
Plus de réponses (1)
yanqi liu
le 23 Fév 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 commentaires
yanqi liu
le 24 Fév 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);
Voir également
Catégories
En savoir plus sur Computer Vision Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!