Question

I have two binary images that refer as ground truth image A and test image B. I want to calculate Dice Coefficient Similarity defined here. To calculate it is very easy. This is one same code

function dist = dist_Dice(A,B)
    % Calculation of the Dice Coefficient

    idx_img = find(B== 1);
    idx_ref = find(A== 1);
    idx_inter = find((B== 1) & (A== 1));

    dist = 2*length(idx_inter)/(length(idx_ref)+length(idx_img));

The result is a number. But my work is how to show this result visually by an image. The range of the image from 0 to 1. I have no idea to resolve it? I think it similar the overlap of two images that regions overlap have pixel equal 0 and otherwise equal 1.Could you help me implement in matlab?

Was it helpful?

Solution

I don't know if something like that is close to what you have in mind in terms of visualising the differences. As you pointed out, the quantity in which you are interested is a scalar, so there aren't too many options.

RandStream.setDefaultStream(RandStream('mt19937ar','seed',0)); % For reproducibility of results

a = rand(10);
b = rand(10);

A = im2bw(a, graythresh(a));
subplot(2,2,1)
imshow(A, 'InitialMagnification', 'fit')
title('A (ground truth image)')

B = im2bw(b, graythresh(b));
subplot(2,2,2)
imshow(B, 'InitialMagnification', 'fit')
title('B (test image)')

idx_img = find(B);
idx_ref = find(A);
idx_inter = find(A&B);

common_white = zeros(size(A));
common_white(idx_inter) = 1;

subplot(2,2,3)
imshow(common_white, 'InitialMagnification', 'fit')
title('White in both pictures')

dist = 2*length(idx_inter)/(length(idx_ref)+length(idx_img))

idx_img = find(~B);
idx_ref = find(~A);
idx_inter = find(~A&~B);

common_black = ones(size(A));
common_black(idx_inter) = 0;

subplot(2,2,4)
imshow(common_black, 'InitialMagnification', 'fit')
title('Black in both pictures')

dist = 2*length(idx_inter)/(length(idx_ref)+length(idx_img))

enter image description here

OTHER TIPS

Think you are looking for this -

AB = false(size(A));
AB(idx_inter) = true;
figure, imshow(AB)

Generally, with binary images, note that you don't have to do the ==1 part. Also, if you just need to know how many ones there are in an image, you don't need to use find and then length, you can just sum over a binary image:

AB = A&B
imshow(AB);

dist = 2*sum(AB(:))/(sum(A(:))+sum(B(:)));

I find that (sorry, couldn't resist) m = find(A) is for a 256 x 256 image, about twice as quick as the ==1 equivalent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top