Page 1 of 1

Compare & Compression (C++)

Posted: 2009-04-14T05:53:07-07:00
by Hans
Hello everybody! I'm new to ImageMagick so I apologize ahead of time if this question is already answered in the documentation, but I was unable to find it. So here it goes...

Right now I'm writing a C++ tool that tries various methods of compression/quality/etc in order to get a small filesize without losing too much quality of the image. Basically I was planning of having a loop that cycles the quality if an Image down one point at a time until the difference between it and the original Image reaches a threshold. ( Just to add, I'm initially loading up a PNG32, but this current compression loop converts it to JPEG ) Here is the problem I'm having. Right now I have a loop that looks something like this...

Code: Select all

while( /*Check to see if difference threshold is met && imageJPG.quality() > 1*/ )
{
    imageJPG.quality( imageJPG.quality() - 1 );
    imageBase.compare( imageJPG );
}
The problem is that compare seems to only update meanErrorPerPixel, normalizedMaxError, and normalizedMeanError, and even then only if the Image has been quantized, never taking into account the current quality of the Image. I ran a few loops with some outputs and it does appear that the filesize of the image is going down, but the compare values stay consistently at 0 unless I call quantize(), where they always stay at the same value depending on how I specify the quantize values.

In the end, I'd like to download the quality of the JPG until the number of pixels that change reaches a certain threshold. This is purely a brute-force approach, nothing fancy. Thanks ahead of time for your help.

Re: Compare & Compression (C++)

Posted: 2009-04-14T06:17:59-07:00
by magick
You most likely want MagickCore's CompareImages() method which permits you to choose a metric (e.g. PSNR). CompareImages() is not native to Magick++ right now but you can access MagickCore methods like this: MagickCore::CompareImages().

Re: Compare & Compression (C++)

Posted: 2009-04-14T06:23:35-07:00
by Hans
Thanks for the quick reply! I'll give it a shot and see how it turns out.

Re: Compare & Compression (C++)

Posted: 2009-04-14T18:45:11-07:00
by stuartwhelan
I use the following code for motion detection on webcams..

The onyl time it fails is when the webcam is capturing a image in a black room, it seems to get white spots.

The amount of white spots and trigger the motion detection.

Code: Select all

double diff = 0.2;
Magick::Image imagea;
Magick::Image imageb;

imagea.read(bloba);
imageb.read(blobb);

imagea.compare(imageb);
if ( imagea.normalizedMaxError() > diff )
{
	// Do stuff
}