I've been facing this problem for days!
I have to implement an Interface with this structure for image storing:
typedef struct Image
{
uint16_t image_width;
uint16_t image_height;
uint16_t image_depth;
uint8_t data;
Label description;
} Image;
In my c++ function, I need the image in cv::Mat type. So I have to convert the uint8_t type in uchar type (since cv::Mat stores the data with uchar type) and viceversa. I tried in so many ways, but everytime I try to access in any way the Mat image after the convertion, I get a segmentation fault.
Look at my code:
Image face;
Mat input;
Mat output;
input = imread( argv[i], 1 );
/*data = static_cast<uint8_t>(reinterpret_cast<uchar>(*input.data));
this is an alternative way found online,
but it gives the same result.
So I replaced it with the following line*/
uint8_t data = *input.data;
image_width = input.cols;
image_height = input.rows;
image_depth = input.channels();
face.data = data;
face.image_depth = image_depth;
face.image_height = image_height;
face.image_width = image_width;
output = Mat(face.image_height, face.image_width, CV_8UC3);
output.data = &face.data;
//both the following gives segmentation fault
imshow("Face", output);
cout << output << endl; //it starts printing the matrix, but it stops after a while with the seg fault
//but the following, the Mat before the convertion, does not
imshow("Face", input);
EDIT. What I need to do is implement the Inteface
using Multiface = std::vector<Image>;
class Interface {
public:
Interface();
virtual ReturnStatus createTemplate(
const Multiface &faces,
TemplateRole role,
std::vector<uint8_t> &templ,
std::vector<EyePair> &eyeCoordinates,
std::vector<double> &quality)
};
So, after reading the image via imread, I need to pass it to createTemplate in a vector of Image type, and then inside createTemplate create a Mat object from it. I wrote the previous code to check if the conversion was possible.
The issue is to have the same picture as Image struct and ad as Mat, making a sort of conversion beetween them.