How to save a Magick::Image in a grayscale format? I'm using ImageMagick to decode images and write the result to OpenCV matrix. What I'm doing now is reading the color image and then converting it to grayscale by OpenCV:

Magick::Image image("test.png");
cv::Mat mat(image.rows(), image.columns(), CV_8UC3);
image.write(0, 0, image.columns(), image.rows(), "BGR", Magick::CharPixel, mat.data);
cv::cvtColor(mat, mat, CV_BGR2GRAY);

I'd like to write image to the cv::Mat already in grayscale, without the intermidiate color image. This should be very simple, but wasn't able to find it out from the docs and would appreciate any help.

Also I'd like to know how to detect if an image contains an alpha channel?

有帮助吗?

解决方案

To set an image to grayscale, simple call Magick::Image.type( Magick::ImageType ) before writing the image blob to cv.

Magick::Image image("test.png");
image.type( Magick::GrayscaleType );
image.write(0, 0, image.columns(), image.rows(), "BGR", Magick::CharPixel, mat.data);

For detecting if an image has transparent, simply check if Magick::Image.matte() returns true.

Magick::Image image("test.png");
std::cout << "transparent = " << ( image.matte() ? "true" : "false") << std::endl;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top