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.
0 个评论
采纳的回答
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
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
更多回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!