how to compare 2 binary images pixel by pixel

hello i need help please.
how to compare 2 binary images pixel by pixel? by using "for" in function "compare"?
it show me error (under text) and i do not know how to solve it:
??? Cell contents reference from a non-cell array object.
Error in ==> compare at 4
if BW1{a,b}==BW2{a,b}
Error in ==> com at 9
if compare(BW1,BW2)>100
i have this code:
if true
n=1;
p=2;
i=1;
max=100;
while p <= max
BW1=im2bw(o{n},0.4); % in "o" are colour pictures
BW2=im2bw(o{p},0.4);
if compare(BW1,BW2)>100
c{i}=o{p};
n=p;
p=p+1;
i=i+1;
else
p=p+1;
end
end
end
and function for compare:
if true
function [same] = compare( BW1, BW2 )
for a=1:500
for b=1:500
if BW1{a,b}==BW2{a,b}
same=same+1;
end
end
end
end
end

2 Comments

Deos cpoy and pstae no lenogr wrok on yuor cpmouetr?

Sign in to comment.

 Accepted Answer

Change to
if BW1(a,b)==BW2(a,b)

2 Comments

Lukas
Lukas on 20 Mar 2014
Edited: Lukas on 20 Mar 2014
so sipmle thank you :) and "{}" are using only if i want to save or copy components of matrix variable?
{} is used only to extract something that is in cell array, removing the cell layer. () is used for all other kinds of arrays, such as struct arrays, object arrays, numeric arrays, and char arrays. () is also used for cell arrays when the result is to be left as a cell layer.

Sign in to comment.

More Answers (3)

Lukas, don't loop. It looks like you want to count the number of pixels that are true(1) in both images. If so, just do
bothTrue = BW1 & BW2;
NumPixels = sum(bothTrue(:));
I don't know what you're doing with the "c" and "o" cell arrays and what you're doing with p. It looks like p may likely end up being 100 ("max"). But DO NOT USE max AS THE NAME OF YOUR VARIABLE! max() is a built in function and you'll destroy it if you name your own variable that.

7 Comments

thank you i edited it. and something i add there. now i have other problem. function compare do not want to return value of "NumPixels". i need to compare value of "NumPixels" against number in if . and if "NumPixels" is larger then number in if (2 in code), copy c{i}=o{p}; what is wrong please?
here is code:
if true
n=2;
p=3;
i=1;
c{1}=o{1};
all=136;
while p <= all
G=im2bw(o{n},0.4);
BW1 = imresize(G, 0.1);
D=im2bw(o{p},0.4);
BW2= imresize(D, 0.1);
if (compare(BW1, BW2))>2 % here i need "NumPixels" compare with 2
n=p;
p=p+1;
else
while true
n=p;
p=p+1;
G=im2bw(o{n},0.4);
BW1 = imresize(G, 0.1);
D=im2bw(o{p},0.4);
BW2= imresize(D, 0.1);
if (compare(BW1, BW2))>2; break; end
end
i=i+1;
c{i}=o{p};
n=p;
p=p+1;
end
end
end
and function compare:
if true
function [NumPixels] = compare( BW1 , BW2 )
bothTrue = BW1 & BW2;
NumPixels = sum(bothTrue(:));
end
end
Looks like you accepted an answer so I guess the above comment no longer applies, so I didn't really examine it.
yes. i accepted it. but now i know that it is not good. i want to count pixels that are true & false in both images (not only true). how to do it in funcion "compare"?
if true
function NumPixels = compare( BW1 , BW2 )
for a=1:359
for b=1:479
if (BW1(a,b)==BW2(a,b))
NumPixels=NumPixels+1;
end
end
end
end
end
Here it is calculating practically every thing I can think of:
numTrue1 = sum(BW1(:));
numTrue2 = sum(BW2(:));
bothTrue = BW1 & BW2;
numTrueBoth = sum(bothTrue(:));
numFalse1 = sum(~BW1(:));
numFalse2 = sum(~BW2(:));
bothFalse = ~BW1 & ~BW2;
numTrueBoth = sum(bothFalse(:));
false1true2 = ~BW1 & BW2;
numF1T2 = sum(false1true2(:));
true1False2 = BW1 & ~BW2;
numT1F2 = sum(true1False2(:));
Hopefully you can get whatever you need from that.
yes it is good. thank you. and is it any way how to compare only one part(second half) of matrix (binary image)?
middleRow = ceil(size(BW1, 1)/2);
BW1BottomHalf = BW1(middleRow:end, :);
Then do whatever you want with this half sub-image.

Sign in to comment.

I have this problem. i should to make pdf from video (turning pages of book) . So i try choose to every single page from video (without OCR). My plan is. in "o{}" are saved all frames from video. "n"," p" are variables for choosing frames for camparing. First frame is automatically saved to "c{1}". later i compare 2. and 3. frame. if they are same i compare 3. and 4. etc. while there will be 2 different frames(during turning pages every frame is different). ) Now i am finding two same frames (it is time while i am moving hand for turn next page) and save it to "c{2}". changing of reslution and converting to BW are for saving hardware (processor). I hope that this code is not so far from succes but a don not know what is wrong still
You can check the difference using Euclidian distance.
test_img = imread('image1.bmp');
input_im = imread('image2.bmp');
minimum =1000;
[row, col] = size(test_img(:,:,1));
distance = zeros(row, col);
for xAxis = 1 : row
for yAxis = 1 : col
distance(xAxis, yAxis) = sqrt((input_im(xAxis, 1) - test_img(yAxis, 1)).^ 2 + (input_im(xAxis, 2) - test_img(yAxis, 2)).^ 2);
end
end
if distance < minimum
minimum = distance;
end

1 Comment

First of all, you made the common beginner mistake of swapping x and y with row and column. The first index of the image arrays is not the x coordinate. It's the y coordinate and it's deceptive to call the y coordinate of input_img "xAxis". If the image is not square and the number of rows is more than the number of columns, your code will throw an error. Like I said, it's a common mistake, so be careful about that.
Secondly the computation doesn't make sense. You're taking the "distance" between the delta of the first columns and the delta of the second columns. Not only doesn't that make sense, but it ignores all columns from 3 onwards.
Third, using minimum, which goes from a scalar to a 2-D array, is unneeded and never even used.
And finally the whole loop thing could be vectorized into a single line of code.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects 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!