How can I solve error: Arrays have incompatible sizes for this operation?

I run the matlab code and had an error that Arrays have incompatible sizes for this operation. I want a solution to it which again can apply to different images...
The operation that I used is :
A = logical(imread('7001-236.png'));
BW_groundTruth =logical(imread('Label7001-236.png'));
similarity = jaccard(A, BW_groundTruth)
Arrays have incompatible sizes for this operation.
Error in jaccard (line 126)
if any(size(A) ~= size(B))
Error in GT_jac_index (line 9)
similarity = jaccard(A, BW_groundTruth)
I tried to apply the following code to fix it but not workiking
image2=imread('Label7001-236.png');
newIm = imresize(image2, [309, 310]);
imwrite(newIm,"Label7001-236.png")
still have same error, how to fix it?

8 Comments

The reason is very simple, once you look at the array sizes:
A = logical(imread('7001-236.png'));
B = logical(imread('Label7001-236.png'));
size(A)
ans = 1×3
309 310 3
size(B)
ans = 1×2
309 310
Note that the JACCARD documentation clearly states that its 2nd input BW2 must be "...specified as a logical array of the same size as BW1."
Note that TMW could have written more robust code with ISEQUAL(SIZE(..),SIZE(..))
@Stephen23 yes the answer is:
ans =
309 310 3
ans =
309 310
there is different bewteen their sizes , so how to fix it , convert the label image to gray image althought its binary image already or what?
Given that all three pages/channels are exactly equal:
A = logical(imread('7001-236.png'));
isequal(A(:,:,1),A(:,:,2),A(:,:,3))
ans = logical
1
you could simply use indexing to extract one channel.
@Stephen23 sorry I am new biggener in coding and matalab I didnt understand what is the benifit from using
logical(imread('7001-236.png'));
isequal(A(:,:,1),A(:,:,2),A(:,:,3))
I understood that image 7001-236.png is 3 channels which is different than label image , so I have to convert it to gray image to have same channel size of label image , then I can apply jaccard index, is it the right way?
Use
similarity = jaccard(squeeze(A(:,:,1)), BW_groundTruth)
instead of
similarity = jaccard(A, BW_groundTruth)
Note that SQUEEZE does absolutely nothing there, you can safely get rid of it:
similarity = jaccard(A(:,:,1), BW_groundTruth)

Sign in to comment.

Answers (0)

Categories

Asked:

on 9 Oct 2023

Commented:

on 9 Oct 2023

Community Treasure Hunt

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

Start Hunting!