Thread: ignore a rectangular area in a bmp image during image comparison

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    1

    ignore a rectangular area in a bmp image during image comparison

    Below is the pseudo code to compare two image by ignoring the rectangular area which contains dynamic data
    Code:
    #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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Why would that make a difference to your algorithm?

    If you had a proper pixel_t, a proper function to read pixels_t, and a comparison function to compare two pixels_t's, the rest of the code shouldn't care at all.

    Why is your compare function even calling fread()?
    I though all of that was done in main, and it was just comparing two images in memory.

    Getting rid of the global variables would be a good start as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-13-2010, 05:10 AM
  2. Replies: 13
    Last Post: 11-20-2009, 04:43 PM
  3. Problems with Image Magick [Unable to Quantisize Image]
    By Maragato in forum C Programming
    Replies: 1
    Last Post: 09-18-2006, 10:41 PM
  4. Image on the menu bar unused area ?
    By hemanth.balaji in forum Windows Programming
    Replies: 2
    Last Post: 06-05-2005, 11:40 AM
  5. Image class - loading image file
    By GaPe in forum Windows Programming
    Replies: 2
    Last Post: 07-11-2004, 01:35 PM

Tags for this Thread