Álvaro Casas
Álvaro Casas

Reputation: 21

How to convert cv::Mat to void*

I am making a DLL in C++ to use it in LabView, I get an RGB image and I convert it to grayscale. My code is the following:

DLLIMPORT void function (void* ImageSrc, void* ImageF)
{
    cv::Mat greyMat, colorMat;
    colorMat = cv::Mat(488, 648, CV_8U, (uchar*)ImageSrc);
    greyMat = (colorMat, CV_LOAD_IMAGE_GRAYSCALE);


}

ImageF would be the gray image and I do not know how to copy grayMat in ImageF.

Upvotes: 1

Views: 1105

Answers (1)

Rotem
Rotem

Reputation: 32144

According to the following post it supposed to be very simple:

DLLIMPORT void function(void* ImageSrc, void* ImageF)
{
    cv::Mat colorMat = cv::Mat(488, 648, CV_8UC3, ImageSrc);
    cv::Mat greyMat = cv::Mat(488, 648, CV_8UC1, ImageF);
    cv::cvtColor(colorMat, greyMat, cv::COLOR_RGB2GRAY); //You may also try cv::COLOR_BGR2GRAY
}
  • The ImageSrc is RGB, so it is has 3 color channels, and the type should be CV_8UC3 and not CV_8U.
  • Create the gray cv::Mat with ImageF as data argument (and type CV_8UC1).
    There are multiple options for creating cv:Mat objects (constructor overloading).
    The above option sets ImageF to point the data of the image.
  • Execute cv::cvtColor with cv::COLOR_RGB2GRAY conversion type, for converting from RGB to gray.

Note:
The resolution of the output image (684x488) and the type (gray type with one byte per pixel) must be defined in LabView before executing the function.
The size and type information are not passed from function to LabView.
Only the "raw" image data is passed to LabView.

Please let me know if it works, because I have no way to test it.

Upvotes: 1

Related Questions