1

I'm working on an image preprocessing project in my university and used an image magick script to clean image background.Now I want to get the same output through Magick++ (c++ api for imageMagick).

ImageMagick Command: "convert -respect-parenthesis ( INPUT_IMAGE.jpg -colorspace gray -contrast-stretch 0 ) ( -clone 0 -colorspace gray -negate -lat 25x25+30% -contrast-stretch 0 ) -compose copy_opacity -composite -fill white -opaque none -alpha off -background white OUTPUT_IMAGE.jpg"

I tried to convert this code to Magick++ code and failed in "-lat", "-contrast-stretch" and "-compose" positions.

This is my c++ code so far:

Image backgroungImage;
backgroungImage.read("INPUT_IMAGE.jpg");
backgroungImage.colorSpace(GRAYColorspace);
backgroungImage.type(GrayscaleType);
backgroungImage.contrastStretch(0, QuantumRange);
backgroungImage.write("Partial_output.jpg");

If anyone has an idea or a better solution please let me know. thanx in advance.

TJay
  • 63
  • 10

1 Answers1

3

You're on the right track with -contrast-stretch. For -lat, remember that's an abbreviation of "Local Adaptive Threshold". So the C++ code would look like...

Image backgroundImage;
// INPUT_IMAGE.jpg
backgroundImage.read("INPUT_IMAGE.jpg");
// -colorspace gray 
backgroundImage.colorSpace(GRAYColorspace);
// -contrast-stretch 0
backgroundImage.contrastStretch(0, QuantumRange);
// -clone 0
Image foregroundImage(backgroundImage);
// -negate
foregroundImage.negate();
// -lat 25x25+30%
foregroundImage.adaptiveThreshold(25, 25, QuantumRange * 0.30);
// -contrast-stretch 0
backgroundImage.contrastStretch(0, QuantumRange);
// -compose copy_opacity -composite
backgroundImage.composite(foregroundImage, 0, 0, CopyAlphaCompositeOp);
// -fill white -opaque none
backgroundImage.opaque(Color("NONE"), Color("WHITE"));
// -alpha off
backgroundImage.alpha(false);
// -background white
backgroundImage.backgroundColor(Color("WHITE"));
// OUTPUT_IMAGE.jpg
backgroundImage.write("OUTPUT_IMAGE.jpg");

Hope that helps!

emcconville
  • 23,800
  • 4
  • 50
  • 66
  • Thank you very much. With this help, I can proceed further with the conversion. But the output I get from the image magick command is different from the magick++ code. As per my knowledge that should cause due to the values I passed to adaptiveThreshold() function. I tried altering the values, but still, the Output image removes most of the features in the image and make it more white. BTW the above explanation resolved most of the doubts I had. Thank you. – TJay Apr 28 '17 at 04:29
  • Please post the expected image, the result, and which version of ImageMagick used. Perhaps I'll be able to identify the issue – emcconville Apr 28 '17 at 11:07
  • Thank to you and your guidance I have found the issue. I commented the two lines that perform "Contrast Stretching" for images and get the exact output similar to command line execution. My problem is solved leaving a question "then what's the purpose of contrast stretching and QuantumRange ", something that I should dig into. – TJay May 02 '17 at 09:34
  • Actually, Try replacing `QuantumRange` with `backgroundImage.columns() * backgroundImage.rows() / 100.0` for the white pixel value in `contrastStretch` method. See if that works, I might have misunderstood the method myself. – emcconville May 02 '17 at 13:01
  • I have tried by changing the QuantumRange value as you suggested, but the ImageMagick command line output and magick++ output images differ each other. I'm using "ImageMagick-7.0.4-Q16". – TJay May 03 '17 at 04:15
  • I'm feeding a scanned book page as input where magick++ code generates a more whitish image (barely identify the fonts). BTW as I explain earlier commenting the "contrastStretch(0, QuantumRange)" generates the identical image as command-line ImageMagick. – TJay May 03 '17 at 04:24