如何使用MATLAB在一个图形中叠加多个图形图形?

时间:2018-09-23 11:23:40

标签: matlab edge-detection imshow

基于这个想法(图)enter image description here 我试图将所有这三个输出(边缘)绘制在一张具有不同颜色的单个图像中,以代表每个输出(而不是子图或imshowpair)。现在,我的输出仍然只是三个不同的图像。enter image description here enter image description here enter image description here

clear all
close all
clc
%% ground truth
img = imread('22o.jpg');
img_gray = rgb2gray(img);
img_ground_truth = imread('22g.jpg');
img_ground_truth = im2bw(img_ground_truth);
cc = img_ground_truth;
%cc = img_ground_truth(11:54, 112:171);

%% Detected part
img_edge = edge(img_gray, 'canny');
dd = img_edge;
%dd = img_edge(11:54, 112:171);

%% True pixel, false pixel, true negative 
[m n] = size(cc);
true_pixel = zeros(m,n);
false_pixel = zeros(m,n);
false_negative = zeros(m,n);
for i = 1:m
    for j = 1:n
        if (dd(i,j) == cc(i,j))
            true_pixel(i,j) = dd(i,j);          

        elseif (dd(i,j)~= cc(i,j))
            false_pixel(i,j) = dd(i,j);
        end
    end
end
for i = 1:m
    for j = 1:n
        if (cc(i,j)==1 && dd(i,j)==0)
            false_negative(i,j) = cc(i,j);
        end
    end
end
% subplot(2,3,1); imshow(cc); title('Gt');
% subplot(2,3,2); imshow(dd); title('Dc');
figure(1),imshow(true_pixel, 'ColorMap',[1 1 1;0 1 0]); 
title('True Pixel (TP)');
hold on
figure(2),imshow(false_pixel, 'ColorMap',[1 1 1;1 0 0]); 
title('False Pixel (FP)');
hold on
figure(3),imshow(false_negative, 'ColorMap',[1 1 1;0 0 1]); 
title('False Negative (FN)');
hold off

1 个答案:

答案 0 :(得分:4)

我在评论中建议的原始解决方案是:

merged_image = cat(3, false_pixel, true_pixel, false_negative);
imshow(merged_image)

这会导致图像的真负像素为黑色:

enter image description here

(请原谅顶部的图例,我使用了您的图片,而且懒得删除它。)

如果三个图像之间有重叠的可能性,这就是我要使用的方法。如果您希望TN为白色,则可以将像素从其他两个通道中减去,而不是将像素添加到所需的通道中:

% turn images into logical arrays to use in indexing
true_pixel = logical(true_pixel);
false_pixel = logical(false_pixel);
false_negative = logical(false_negative);

% create RGB channels for all-white image
r_channel = ones(size(true_pixel));
g_channel = ones(size(true_pixel));
b_channel = ones(size(true_pixel));

% leave pixels in true_pixel image green
r_channel(true_pixel) = 0;
b_channel(true_pixel) = 0;

% leave pixels in false_pixel image red 
g_channel(false_pixel) = 0;
b_channel(false_pixel) = 0;

% leave pixels in false_negative image blue
r_channel(false_negative) = 0;
g_channel(false_negative) = 0;

% merge into RGB image 
merged_image = cat(3, r_channel, g_channel, b_channel);
imshow(merged_image)

结果:

enter image description here

另一种可能性是像最初那样使用索引图像。最干净的方法是在循环中的合并图像中生成不同的索引{1,2,3},因此在您的最后一个循环中(与其他两个类似的代码)类似这样:

...
false_negative(i,j) = cc(i,j);
merged_image(i,j) = cc(i,j)*3; 
...

然后在最后,将所有3种颜色组合为一种颜色图:

imshow(uint8(merged_image), 'ColorMap', [1 1 1; 0 1 0; 1 0 0; 0 0 1])