[magick-users] Help on difference btw images using ImageMagick and C++

Pranjal D. Bonde pranjal.bonde at gmail.com
Thu Sep 21 07:39:04 CDT 2006


Hello all,

 I'm trying to separate an object from a background image. Both plain
bagkground and object+background images need to be in grayscale. I need
to subtract these two images and get pixel values of the difference
image. 

Using ImageMagick I've managed to read the images, find their width,
height and convert to grayscale. I've even used 'PixelPacket' to get the
pixels of the images. However after subracting these image pixels and
saving with view.sync(), image.write() doesn't bring out the right
difference.  

Could someone please help me with this one?

Thanks a billion,
Pranjal

Code - 

// Reading a file into image object
      image.read(fileName);

      image.modifyImage();

// Changing to Grayscale
      image.type(GrayscaleType);

// Displaying Converted Image
      //image.display();

      Pixels view(image);
      PixelPacket *pixels;
 
// Reading pixel values into an array
      int rows = width;
      int columns = height;
      int asize = width*height;
      int pixval[asize];
      int differ[asize];
      int pval[asize];
      int k = 0;
      for (int r=0;r<rows;r++)
	{
	  for (int c=0;c<columns;c++)
	    {
	      pixels = view.get(r,c,1,1);
	      Quantum gray = (pixels->red);
	      pixval[k] = pixels->red;
	      k++;
	    }
	}

// Function performing subtraction (I need to add more to this function
// later, hence the need for a separate function)

      temp = isolate(pixval,val,differ,asize); // Val has pixel values
					       // for image with object and 					       // background
      
      for (int r=0;r<rows;r++)
	{
	  for (int c=0;c<columns;c++)
	    {
	      pixels = view.get(r,c,1,1); // Moving this line out of the 	     
	      Quantum gray = *temp;	  // loop, gives a different set
	      (pixels->red) = gray;	  // of pixel values (??)
	      (pixels->blue) = gray;
	      (pixels->green) = gray;
	      temp++;
	      view.sync();
	    }
	}

// Writing image to a new file
      image.write("subimg.jpg");



More information about the Magick-users mailing list