Why I do I get different results when I used jaccard Index with png format than with jpg format?

When I applied the following:
A = logical(imread('R7001-21.png'));
BW_groundTruth =logical(imread('gT7001_21.png'));
similarity = jaccard(A, BW_groundTruth)
I got:
>> GT_jac_index
similarity =
0.9952
While when I applied
A = logical(imread('R7001-21.jpg'));
BW_groundTruth =logical(imread('gT7001_21.jpg'));
similarity = jaccard(A, BW_groundTruth)
I got:
>> GT_jac_index
similarity =
0.8843
Why? And is it better to use png format images?

 Accepted Answer

Both sets of code use the PNG versions.
In general you should not use JPG files for image analysis, unless you're specifically doing research on compression and how lousy JPG is compared to other methods.
It is better to use PNG because it is a loasless compression and does not change values or introduce artifacts (such as contouring/posterization, edge blurring, etc.) like JPG does.

7 Comments

@Image Analyst yes I correct it , the first one used png and got .99 and second code used jpg and I got .88, and strange thing if I compare between png and jpg I got jaccrad index=1
I don't understand why you say you get 1 and at the same time get .99 and .88. Which is it?
@Image Analyst i got different values of jaccard index based on the image format
1- if 2 comaprison images png similarity = 0.9952
2-if 2 comparison images jpg similarity = 0.8843
3-if one of them jpg and other png similarity = 1
as shown below:
case1:
A = logical(imread('R7001-21.png'));
BW_groundTruth =logical(imread('gT7001_21.png'));
similarity = jaccard(A, BW_groundTruth)
>> GT_jac_index
similarity =
0.9952
case 2:
while when I applied
A = logical(imread('R7001-21.jpg'));
BW_groundTruth =logical(imread('gT7001_21.jpg'));
similarity = jaccard(A, BW_groundTruth)
>> GT_jac_index
similarity =
0.8843
case 3:
while when I applied
A = logical(imread('R7001-21.jpg'));
BW_groundTruth =logical(imread('gT7001_21.png'));
similarity = jaccard(A, BW_groundTruth)
>> GT_jac_index
similarity =
1
Doesn't make sense. If the two images are perfectly aligned and all pixels match, like they should be if both images were PNG, then you should get 1. Please attach the JPG image you are using.
A = logical(imread('R7001-21.jpg'));
BW_groundTruth =logical(imread('gT7001_21.jpg'));
similarity = jaccard(A, BW_groundTruth)
>> GT_jac_index
similarity =
0.8843
The images are different. Not all the pixels are the same so the jaccard index will not be 1.
fileName1 = 'R7001-21.png';
fileName2 = 'gT7001_21.png';
fileName3 = 'R7001-21.jpg';
fileName4 = 'gT7001_21.jpg';
% Read in all files:
image1 = imread(fileName1);
image2 = imread(fileName2);
image3 = imread(fileName3);
image4 = imread(fileName4);
% Take green channel of all of them.
image1 = image1(:, :, 2);
image2 = image2(:, :, 2);
image3 = image3(:, :, 2);
image4 = image4(:, :, 2);
% See how many pixels are different between the two.
diffImage = imabsdiff(image1, image2);
fprintf('Image 1 differs from image2 by %d pixels.\n', nnz(diffImage));
Image 1 differs from image2 by 133 pixels.
diffImage = imabsdiff(image1, image3);
fprintf('Image 1 differs from image3 by %d pixels.\n', nnz(diffImage));
Image 1 differs from image3 by 133 pixels.
diffImage = imabsdiff(image1, image4);
fprintf('Image 1 differs from image4 by %d pixels.\n', nnz(diffImage));
Image 1 differs from image4 by 3238 pixels.
diffImage = imabsdiff(image2, image3);
fprintf('Image2 differs from image3 by %d pixels.\n', nnz(diffImage));
Image2 differs from image3 by 0 pixels.
diffImage = imabsdiff(image2, image4);
fprintf('Image2 differs from image4 by %d pixels.\n', nnz(diffImage));
Image2 differs from image4 by 3263 pixels.
diffImage = imabsdiff(image3, image4);
fprintf('Image3 differs from image4 by %d pixels.\n', nnz(diffImage));
Image3 differs from image4 by 3263 pixels.
% Compare filenames 1 and 2
testImage = logical(image1);
BW_groundTruth = logical(image2);
similarity = jaccard(testImage, BW_groundTruth)
similarity = 0.9952
% Compare filenames 3 and 4
testImage = logical(image3);
BW_groundTruth = logical(image4);
similarity = jaccard(testImage, BW_groundTruth)
similarity = 0.8843
% Compare filenames 1 and 3
testImage = logical(image1);
BW_groundTruth = logical(image3);
similarity = jaccard(testImage, BW_groundTruth)
similarity = 0.9952
% Compare filenames 2 and 4
testImage = logical(image2);
BW_groundTruth = logical(image4);
similarity = jaccard(testImage, BW_groundTruth)
similarity = 0.8843

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!