Type conversion in open cv - c++

I am trying to convert an uint32_t* into a format that could be accepted by a Mat object (from open cv). I do not need to display the data from the Mat, I am simply using the mat to transport and pack information.I know that opencv does not use uint32_t and as a workaround I used int. What I did is the following:
Mat package = Mat(width,height, CV_32SC4);
int *dst1 = package.data;
//code that works with the dst
//finally put it back in the package
package.data = dst1;
The above code gives me an error at the second line. (Cannot convert from 'int *' to 'uchar *')

Related

How to convert Halide::Buffer<unsigned char> to c++ array

In lesson_02_input_image.cpp, I am trying to transpose Buffer<uint8_t> type data to a c++ array so that compare the time consumption between halide and c++. The code is like :
Halide::Buffer<uint8_t> input = load_image("images/rgb.png");
float *img = input.copy_to_device(halide_opencl_device_interface());
And the code got error:cannot convert ‘Halide::Buffer’ to ‘float*’ in initialization.It seems that input is Halide::Buffer type, how could I fix that?

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.
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.

Writing uchar* data to file

I am using the OpenCV library, which has a class called Mat, representing a matrix, with data stored in an array at uchar* Mat::data. I want to write this data to a binary file using C++.
Here is my code:
cv::Mat m(3, 3, CV_8UC1); // This basically creates a 3-by-3 matrix
std::fstream fileOut("file.bin", std::ios::out | std::ios::binary);
fileOut.write((char*)m.data, 9); // This should write 9 bytes of data
And I am getting the error message:
invalid conversion from ‘uchar* {aka unsigned char*}’ to ‘std::basic_istream<char>::char_type* {aka char*}’
What should I do differently?
it's an illegal conversion in C++
use reinterpret_cast<const char*>(m.data)

Opencv : Conversion Mat to IplImage for cvLabel

I have:
Mat *depthImage = new Mat(480, 640, CV_8UC1, Scalar::all(0));
And further in my code I do:
Mat image = *depthImage;
I do some OpenCV stuff with it and then I want to use cvBlob (so blob analysis). Though this function still uses IplImage and not Mat. So I wanted to convert them. I've read that I could just do this:
IplImage *blobimg = image;
But it doesn't work, I get this error:
Semantic Issue: No viable conversion from 'cv::Mat' to 'IplImage *' (aka '_IplImage *')
Eventually I want to be able to use this function on the newley created IplImage
cvLabel(<#const IplImage *img#>, <#IplImage *imgOut#>, <#CvBlobs &blobs#>)
As you can see the conversion from Mat to IplImage is required. But it is not working. My question is how do I fix this?
Thanks in advance
As Martin Beckett says in the comments, the cheatsheet shows this solution. There is no conversion from cv::Mat to IplImage *, but there is a conversion from cv::Mat to IplImage.
Change the line
IplImage *blobimg = image;
to
IplImage blobimg = image;
and it should compile.
When calling cvLabel, pass the parameter like
cvLabel(&blobimg, ...);

cvShowImage error

Ok. With a help of SO I have managed to put together this code. There is one more error though:
// ...
std::vector<char> jpegBuffer(lSize);
// copy the file into the buffer.
fread(&jpegBuffer[0], 1, jpegBuffer.size(), pFile);
// terminate
fclose (pFile);
Mat matrixJpeg = cv::imdecode(Mat(jpegBuffer), 1);
IplImage fIplImageHeader = matrixJpeg;
cvShowImage("Video", fIplImageHeader);
char key = cvWaitKey();
There is one more error with this code though. It is at this line:
cvShowImage("Video", fIplImageHeader);
And the error is:
/home/richard/Desktop/richard/client/src/main.cc:106: error: cannot convert ‘IplImage’ to ‘const CvArr*’ for argument ‘2’ to ‘void cvShowImage(const char*, const CvArr*)’
According to the docs Mat and IplImage should be interchangable and this:
IplImage fIplImageHeader = matrixJpeg;
Should be all that is needed to convert Mat to IplImage.
I think it should be:
IplImage* fIplImageHeader = matrixJpeg;
IplImage* and Mat* should be interchangeable.
CvArr* is a typedef for void*
You must specify the forced type conversion:
csShowImage("Whatever", static_cast<CvArr*>(somePointer));
Also note that somePointer is a pointer.
You might say something like
IplImage object = matrix;
IplImage* somePointer = &object;
Also, I kind of doubt it that arbitrary image type is substitutable with a matrix type. What are you willing to achieve when you tell your program to show the matrix?