IplImage 与 Mat 转换成uchar

IplImage 与 Mat 转换成uchar

【废话不多说,直接上代码。】

关于IplImage 见:OpenCV 中的IplImage 结构

IplImage 转成 uchar,方法1:

/// 
/// OK
/// IplImage类型转换成uchar
/// 输入:单通道灰度图像指针
/// 输出:单通道灰度图像指针
/// 
int IplImage2Byte(IplImage* src, uchar* dst)
{
	if (NULL == src)
	{
		return -1;
	}

	int i = 0, j = 0;
	int nWidth = src->width;    //图像的宽 像素数.
	int nHeight = src->height;  //图像的高 像素数.
	int nWidthStep = src->widthStep;  //排列的图像行大小,以字节为单位.
	int nbitCount = src->depth;		  //像素的位深度: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16U, 
									  //  IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F, IPL_DEPTH_64F 可支持.
	int nChannel = src->nChannels;    //大多数OPENCV函数支持1,2,3 或 4 个通道.
	char *imgData = src->imageData;   //char *imageData; 指向排列的图像数据.

	uchar* pSrc = NULL;
	uchar* pDst = NULL;


	//FILE *INI,*src_INI;
	//char filename[256], filename2[256];
	//sprintf(filename, "..//输出//pixl_byte.txt");
	//INI = fopen(filename, "a+");

	//sprintf(filename2, "..//输出//pixl_Ipl.txt");
	//src_INI = fopen(filename2, "a+");


	for (i = 0; i < nHeight; i++)
	{
		pSrc = (uchar*)(src->imageData + i * (size_t)nWidthStep);
		pDst = (uchar*)(dst + i * (size_t)nWidth);
		for (j = 0; j < nWidth; j++)
		{
			pDst[j] = pSrc[j];

			//fprintf(src_INI, "%d,   ", pSrc[j]);
			//fprintf(INI, "%d,   ", pDst[j]);
			//if (6==j)
			//{
			//	fprintf(INI, "%d\n", pDst[j]);
			//	fprintf(src_INI, "%d\n", pSrc[j]);
			//}
		}
	}
	//fclose(INI);
	//fclose(src_INI);

	return 1;
}

IplImage 转成 uchar,方法2:

/// 
/// OK
/// IplImage类型转换成uchar
/// 
/// 输入:图像指针
/// 输出:图像指针
/// 
int f_IplImage2Byte2(IplImage* img, uchar* dst)
{
	if (img == NULL)
	{
		return -1;
	}

	size_t imgWidth = img->width;
	size_t imgHeight = img->height;

	memcpy(dst, img->imageData, (imgWidth * imgHeight));


	return 1;
}

关于Mat 见:OpenCV 中的Mat 类

Mat 转成 uchar,方法1:

/// 
/// OK
/// Mat类型转换成uchar
/// 输入:图像指针
/// 输出:图像指针
/// 
int f_Mat2Byte2(cv::Mat* img, uchar* dst)
{
	if (img == NULL)
	{
		return -1;
	}

	size_t imgWidth = img->cols;
	size_t imgHeight = img->rows;

	memcpy(dst, img->data, (imgWidth * imgHeight));


	return 1;
}

 

 

【32、不经历风雨,长不成大树,不受百炼,难以成钢。 】

 

 

你可能感兴趣的:(OpenCV,C_C++,opencv,计算机视觉,图像处理)