I want to use ImageMagick in order to write a multiframe tiff. I followed this in order to write a "TiffWriter" : Tiffwriter topic
I tried to convert a cv::Mat to a Magick::Image using this topic : cv::Mat => Magick::Image
I face a problem on the second part :
I changed the TiffWritter in order to write RGBImage from OpenCV Matrix like that :
TiffWriter::TiffWriter(std::string filename) : filename(filename) {}
// for example for a 8 bit gray image buffer
void TiffWriter::writeGray(const unsigned char* buffer, int width, int height)
{
Magick::Blob gray8Blob(buffer, width * height);
Magick::Geometry size(width, height);
Magick::Image gray8Image(gray8Blob, size, 8, "GRAY");
imageList.push_back(gray8Image);
}
void TiffWriter::writeRGB(Mat& mat)
{
auto depth = mat.depth();
auto channels = mat.channels();
auto type = mat.type();
Magick::Image Mg_RGB(mat.cols,mat.rows,"BGR",Magick::CharPixel,(char *)mat.data);
imageList.push_back(Mg_RGB);
}
TiffWriter::~TiffWriter()
{
Magick::writeImages(imageList.begin(), imageList.end(), filename);
}
During execution, i face an error like that :
I tried to check Depth, channels, and type as you can see in spy, and all seems good then i'am a little out of ideas about what can be the problem.
Just before i succeed to display exactly the same image in a window.
for (int i = 16; i < shg.count(); i += 2)
{
cout << "\n Image " << i << "\n";
start = chrono::system_clock::now();
Mat im = shg.GetSubImage(i);
MaskRCNN.ProcessImage(im);
// This work !
cv::imshow("Display window", im);
cv::waitKey(500);
end = std::chrono::system_clock::now();
int elapsed_seconds = std::chrono::duration_cast<std::chrono::seconds>
(end - start).count();
cout << "elapsed time: " << elapsed_seconds << "s\n";
}
cvvDestroyWindow("Display window");
// Issue here
shg.Save(Dest_File);
The method in the object encapsuling the images is like that :
bool ImageContainer::Save(cv::String Filepath)
{
try
{
TiffWriter MyTiffWriter(Filepath);
for (int i = 0; i < count(); i++)
{
Mat Im = GetSubImage(i);
MyTiffWriter.writeRGB(Im);
}
cout << Filepath + " Enregistré...\n";
return true;
}
catch (Exception e)
{
cout << e.msg;
return false;
}
}
Do anybody have an idea ?
Thanks !
The error message detail :
Exception non gérée à 0x00007FFAE241A388 dans Program.exe : exception
Microsoft C++ : Magick::ErrorOption à l'emplacement de mémoire
0x0000003DED95E150.
If i click "continue", i have this message :
Exception levée à 0x0000000000000000 dans Program.exe : 0xC0000005 :
Violation d'accès lors de l'exécution à l'emplacement
0x0000000000000000.Exception levée à 0x0000000000000000 dans Program.exe :
0xC0000005 : Violation d'accès lors de l'exécution à l'emplacement
0x0000000000000000.
EDIT : I handled the exception like that :
void TiffWriter::writeRGB(Mat& mat)
{
try
{
auto depth = mat.depth();
auto channels = mat.channels();
auto type = mat.type();
Magick::Image Mg_RGB(mat.cols, mat.rows, "BGR", Magick::CharPixel, (char *)mat.data);
imageList.push_back(Mg_RGB);
}
catch (Magick::Error &error)
{
// Process Magick++ file open error
cerr << "Error: " << error.what() << endl;
// Try next image.
}
}
The message is this one :
Error: Magick: unrecognized pixel map ``QSõ▒' @ error/pixel.c/ImportImagePixels/4243
If i replace the line causing problem by :
Magick::Image Mg_RGB(2, 2, "RGB", Magick::CharPixel, (char*)"2222");
I get exactly the same issue.
I tried inspect "ImageMagick/pixel.c" file : When i watch the code of "pixel.c" line 4243, there is a switch case :
switch (map[i])
{
case 'a':
case 'A':
{
quantum_map[i]=AlphaQuantum;
image->alpha_trait=BlendPixelTrait;
break;
}
case 'B':
case 'b':
{
quantum_map[i]=BlueQuantum;
break;
}
...
default:
{
quantum_map=(QuantumType *) RelinquishMagickMemory(quantum_map);
(void) ThrowMagickException(exception,GetMagickModule(),OptionError,
"UnrecognizedPixelMap","`%s'",map);
return(MagickFalse);
}
}
Then it is as if i went in the "default" part of this "switch/case" maybe a problem with "RGB" char ?