Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is the pseudo code to compare two image by ignoring the rectangular area which contains dynamic data
Objective-C
#include <stdio.h>

#define WID 200
#define DEP 600

typedef struct {
int left;
int top;
int right;
int bot;
} rect;

char image1[WID*DEP];
char image2[WID*DEP];

int inrect (int x, int y, rect r) {
if (x<r.left || x>r.right || y<r.top || y>r.bot)
    return 0;
return 1;
}

  int compareimages (char *im1, char* im2, int wid, int dep, rect *rarr,   int rects) 
{
   int x, y, n, ignore;
   for (y=0; y<dep; y++)
    for (x=0; x<wid; x++) {
    int read_bytes = fread(&k, 1, 1, image1); // 
        ignore = 0;
       for (n=0; n<rects; n++)
            ignore |= inrect (x, y, rarr[n]);
        if (!ignore)
            if (k != im2[y*wid+x])   // compare pixels
                return 0;
    }
return 1;
}

int main(void) {
rect rectarr[2] = { {10, 10, 50, 50}, { 40, 40, 90, 90 }};
// ... load images ...

// put pixel in one of the rectangles
image1[20*WID+20] = 1;
if (compareimages (image1, image2, WID, DEP, rectarr, 2))
    printf ("Same\n");
else
    printf ("Different\n");

// put pixel outside any rectangles
image1[0] = 1;
if (compareimages (image1, image2, WID, DEP, rectarr, 2))
    printf ("Same\n");
else
    printf ("Different\n");
return 0;
}


Above code works fine if each pixel has 1 byte but if i want to compare bmp image where each pixel has 4 byte each byte for red,green,blue and alpha channel how to do that please help
Posted

1 solution

Read the four bytes together as DWORD, or unsigned int as you prefer, dimension your image arrays as same var type and you're done ;)
C++
#include <stdio.h>

#define WID 200
#define DEP 600
 
typedef struct {
int left;
int top;
int right;
int bot;
} rect;
 
DWORD image1[WID*DEP];
DWORD image2[WID*DEP];
 
int inrect (int x, int y, rect r) {
if (x<r.left x="">r.right || y<r.top y="">r.bot)
    return 0;
return 1;
}
 
  int compareimages (DWORD *im1, DWORD *im2, int wid, int dep, rect *rarr,   int rects) 
{
   int x, y, n, ignore;
   DWORD k; //You have not defined it in your code! 
   for (y=0; y<dep; y++)
    for (x=0; x<wid; x++) {
    int read_bytes = fread(&k, 4, 1, image1); // read four bytes
        ignore = 0;
       for (n=0; n<rects; n++)
            ignore |= inrect (x, y, rarr[n]);
        if (!ignore)
            if (k != im2[y*wid+x])   // compare pixels
                return 0;
    }
return 1;
}
 
int main(void) {
rect rectarr[2] = { {10, 10, 50, 50}, { 40, 40, 90, 90 }};
// ... load images ...

// put pixel in one of the rectangles
image1[20*WID+20] = 1;
if (compareimages (image1, image2, WID, DEP, rectarr, 2))
    printf ("Same\n");
else
    printf ("Different\n");
 
// put pixel outside any rectangles
image1[0] = 1;
if (compareimages (image1, image2, WID, DEP, rectarr, 2))
    printf ("Same\n");
else
    printf ("Different\n");
return 0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900