Opencv7:Mat与unsigned char[ ]类型的相互转换
(1)前言:
参考其他博客编写的利用积分图优化的均值滤波函数中,输入参数和输出参数均为unsigned char *类型,无法直接传入Mat类型参数,因此需要进行Mat与unsigned char[ ]类型的相互转换。
(2)Mat转化为unsigned char[ ]类型:
//【1】原图像格式转换[mat转unsigned char]
Mat image = imread("6.png",0);
int image_size = image.cols * image.rows;
unsigned char* imageData = new unsigned char[image_size];
int a = 0;
for (int i = 0;i<image.rows;i++)
{
for (int j = 0;j<image.cols;j++)
{
imageData[a]= image.at<uchar>(i, j);
a++;
}
}
(3)unsigned char[ ]转化为Mat类型:
①前文:
BoxBlur()为自定义函数,函数原型为void BoxBlur(unsigned char *Src, unsigned char *Dest, int Width, int Height, int Stride, int Radius),不在此多做赘述。
//【2】执行均值滤波
unsigned char* imageBoxBlurData = new unsigned char[image_size];
BoxBlur(imageData, imageBoxBlurData, image.cols, image.rows, 3, 4);
② unsigned char[ ]转化为Mat类型:
//【3】均值滤波输出unsigned char格式转换[unsigned char转mat]
cv::Mat ImageBoxBlur(image.rows, image.cols, CV_8UC1);
int b = 0;
for (int i = 0;i<image.rows;i++)
{
for (int j = 0;j<image.cols;j++)
{
ImageBoxBlur.at<uchar>(i, j) = (uchar)imageBoxBlurData[b];
b++;
}
}