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.

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

6 Comments

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.
Please post the expected image, the result, and which version of ImageMagick used. Perhaps I'll be able to identify the issue
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.
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.
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".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.