Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

how to compare 2 binary images pixel by pixel

Asked by Lukas

Lukas (view profile)

on 20 Mar 2014
Latest activity Commented on by Lukas

Lukas (view profile)

on 26 Mar 2014

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

0 Comments

Lukas

Lukas (view profile)

3 Answers

Answer by Walter Roberson

Walter Roberson (view profile)

on 20 Mar 2014
Accepted answer

Change to

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

2 Comments

Lukas

Lukas (view profile)

on 20 Mar 2014

so sipmle thank you :) and "{}" are using only if i want to save or copy components of matrix variable?

Walter Roberson

Walter Roberson (view profile)

on 23 Mar 2014

{} 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.

Walter Roberson

Walter Roberson (view profile)

Answer by Image Analyst

Image Analyst (view profile)

on 20 Mar 2014

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

Lukas

Lukas (view profile)

on 24 Mar 2014

yes it is good. thank you. and is it any way how to compare only one part(second half) of matrix (binary image)?

Image Analyst

Image Analyst (view profile)

on 24 Mar 2014
middleRow = ceil(size(BW1, 1)/2);
BW1BottomHalf = BW1(middleRow:end, :);

Then do whatever you want with this half sub-image.

Lukas

Lukas (view profile)

on 26 Mar 2014

thank you

Image Analyst

Image Analyst (view profile)

Answer by Lukas

Lukas (view profile)

on 21 Mar 2014

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

0 Comments

Lukas

Lukas (view profile)

Contact us