How to add dashed line to RGB image
조회 수: 13 (최근 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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/903185/image.jpeg)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/903190/image.jpeg)
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!