unsigned char陣列與IplImage、Mat之間互相轉換
阿新 • • 發佈:2018-12-05
Mat input_image; unsigned char* input = new unsigned char[input_image.rows * input_image.cols]; //這裡假設是單通道的,如果不是,需要乘以input_image.channels() //將unsigned char陣列轉換為IplImage*,並顯示 IplImage* image = cvCreateImageHeader(cvSize(input_image.cols,input_image.rows), IPL_DEPTH_8U, input_image.channels()); image->origin = ~image->origin; cvSetData(image, input, input_image.cols * input_image.channels()); cvNamedWindow("window-1"); cvShowImage("window-1", image); //這裡顯示出來的影象是倒著的 //將unsigned char陣列轉換為Mat,並顯示 Mat output_image(input_image.rows, input_image.cols, input_image.type(),input); imshow("output_image",output_image); //這裡顯示出來的影象是正的 //將IplImage*轉換為unsigned char陣列 IplImage* image; int image_size = image->width * image->height; unsigned char* output = new unsigned char[image_size]; //或unsigned char* output = (unsigned char*)malloc(sizeof(unsigned char) * image_size); memcpy(output, image->imageData, image_size); //將Mat轉換為unsigned char陣列 int image_size = input_image.cols * input_image.rows; unsigned char* output = new unsigned char[image_size]; //或unsigned char* output = (unsigned char*)malloc(sizeof(unsigned char) * image_size); memcpy(output, input_image.data, image_size);