IplImag、HImage相互转换
来源:互联网 发布:北京宇信科技集团知乎 编辑:程序博客网 时间:2024/06/05 17:13
IplImage* HImageToIplImage(Hobject &Hobj){ IplImage *pImage; HTuple htChannels; char cType[MAX_STRING]; Hlong width,height; width = height =0; //转换图像格式 convert_image_type(Hobj,&Hobj,"byte"); count_channels(Hobj,&htChannels); if (htChannels[0].I() == 1) { unsigned char *ptr; get_image_pointer1(Hobj,(Hlong *)&ptr,cType,&width,&height); pImage = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1); for(int i = 0; i<height; i++) { memcpy(pImage->imageData+ pImage->widthStep*i, ptr + width*i, width); } } if (htChannels[0].I() == 3) { unsigned char *ptrRed,*ptrGreen,*ptrBlue; ptrRed = ptrGreen =ptrBlue = NULL; get_image_pointer3(Hobj,(Hlong *)&ptrRed,(Hlong *)&ptrGreen,(Hlong *)&ptrBlue,cType,&width,&height); IplImage *pImageRed,*pImageGreen,*pImageBlue; pImage = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3); pImageRed = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1); pImageGreen = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1); pImageBlue = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1); for(int i = 0; i<height; i++) { memcpy(pImageRed->imageData+ pImageRed->widthStep*i, ptrRed + width*i, width); memcpy(pImageGreen->imageData+ pImageGreen->widthStep*i, ptrGreen + width*i, width); memcpy(pImageBlue->imageData+ pImageBlue->widthStep*i, ptrBlue + width*i, width); } cvMerge(pImageBlue,pImageGreen,pImageRed,NULL,pImage); cvReleaseImage(&pImageRed); cvReleaseImage(&pImageGreen); cvReleaseImage(&pImageBlue); } return pImage;}
Hobject IplImageToHImage(IplImage *pImage){ Hobject Hobj; if (3 == pImage->nChannels) { IplImage *pImageRed,*pImageGreen,*pImageBlue; pImageRed = cvCreateImage(cvGetSize(pImage),IPL_DEPTH_8U,1); pImageGreen = cvCreateImage(cvGetSize(pImage),IPL_DEPTH_8U,1); pImageBlue = cvCreateImage(cvGetSize(pImage),IPL_DEPTH_8U,1); cvSplit(pImage,pImageBlue,pImageGreen,pImageRed,NULL); uchar *dataRed = new uchar[pImage->width*pImage->height]; uchar *dataGreen = new uchar[pImage->width*pImage->height];; uchar *dataBlue = new uchar[pImage->width*pImage->height];; int height = pImage->height; int width =pImage->width; for(int i = 0; i<height; i++) { memcpy(dataRed + width*i,pImageRed->imageData + pImageRed->widthStep*i, width); memcpy(dataGreen + width*i,pImageGreen->imageData + pImageGreen->widthStep*i, width); memcpy(dataBlue + width*i,pImageBlue->imageData + pImageBlue->widthStep*i, width); } gen_image3(&Hobj,"byte",pImage->width,pImage->height,(Hlong)(dataRed),(Hlong)(dataGreen),(Hlong)(dataBlue)); cvReleaseImage(&pImageRed); cvReleaseImage(&pImageGreen); cvReleaseImage(&pImageBlue); delete[] dataRed; delete[] dataGreen; delete[] dataBlue; } if (1 == pImage->nChannels) { int height = pImage->height; int width =pImage->width; uchar *dataGray = new uchar[width*height]; for(int i = 0; i<height; i++) { memcpy(dataGray + width*i,pImage->imageData + pImage->widthStep*i, width); } gen_image1(&Hobj,"byte",pImage->width,pImage->height,(Hlong)(dataGray)); delete[] dataGray; } return Hobj;}
(1)、从Hobject到IplImageIplImage* HImageToIplImage(Hobject &Hobj){IplImage* pImage;HTuple htChannels;char cType[MAX_STRING];Hlong width,height;width=height=0;//转换图像格式convert_image_type(Hobj,&Hobj,"byte");count_channels(Hobj,&htChannels);if(htChannel[0].I()==1){unsinged char* ptr;get_image_pointer1(Hobj,(Hlong*)&ptr,cType,&width,&height);pImage=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);for(int i=0;i<height;i++){memcpy(pImage->imageData+pImage->widthStep*i,ptr+width*i,width);}}if(htChannels[0].I()==3){unsinged char *ptrRed , *ptrGreen , *ptrBlue;ptrRed=ptrGreen=ptrBlue=NULL;get_image_pointer3(Hobj,(Hlong*)&ptrRed,(Hlong*)&ptrGreen,(Hlong*)&ptrBlue,cType,&width,&height)IplImage *pImageRed , *pImageGreen , *pImageBlue ;pImage=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3);pImageRed=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);pImageGreen=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);pImageBlue=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);for(int i=0;i<height;i++){memcpy(pImageRed->imageData+pImageRed->widthStep*i , ptrRed+width*i , width);memcpy(pImageGreen->imageData+pImageGreen->widthStep*i , ptrGreen+width*i , width);memcpy(pImageBlue->imageData+pImageBlue->widthStep*i , ptrBlue+width*i , width);}cvMerge(pImageBlue,pImageGreen,pImageRed,NULL,pImage);cvReleaseImage(&pImageRed);cvReleaseImage(&pImageGreen);cvReleaseImage(&pImageBlue);}return pImage;}(2)、从IplImage到HobjectHobject IplImageToHImage(IplImage* pImage){Hobject Hobj;if(pImage->nChannels==1){int height=pImage->height;int width=pImage->width;uchar *dataGray=new uchar[width*height];for(int i=0; i<height; i++){memcpy(dataGray+width*i, pImage->imageData+pImage->widthStep*i,width);}gen_image1(&Hobj,"byte",pImage->width,pImage->height,(Hlong)(dataGray));delete[ ] dataGray;}if(pImage->nChannels==3){IplImage *IpImageRed, *IplImageGreen, *IplImageBlue;IplImageRed=cvCreateImage(cvSize(pImage),IPL_DEPTH_8U,1);IplImageGreen=cvCreateImage(cvSize(pImage),IPL_DEPTH_8U,1);IplImageBlue=cvCreateImage(cvSize(pImage),IPL_DEPTH_8U,1);cvSplit(pImage, pImageBlue, pImageGreen, pImageRed,NULL);uchar* dataRed=new uchar[pImage->width*pImage->height];uchar* dataGreen=new uchar[pImage->width*pImage->height];uchar* dataBlue=new uchar[pImage->width*pImage->height];int height=pImage->height;int width=pImage->width;for(int i=0; i<height; i++){memcpy(dataRed+width*i, pImageRed->imageData+pImageRed->widthStep*i,width);memcpy(dataGreen+width*i, pImageGreen->imageData+pImageGreen->widthStep*i,width);memcpy(dataBlue+width*i, pImageBlue->imageData+pImageBlue->widthStep*i,width);}gen_image3(&Hobj,"byte",pImage->width,pImage->height,(Hlong)(dataRed),(Hlong)(dataGreen),(Hlong)(dataBlue));cvReleaseImage(&pImageRed);cvReleaseImage(&pImageGreen);cvReleaseImage(&pImageBlue);delete[ ] dataRed;delete[ ] dataGreen;delete[ ] dataBlue;}return Hobj;}(3)、封装一个画图函数void DrawPicToHDC(IplImage* img , UINT ID){CDC* pdc=GetDlgItem(ID)->GetDC();HDC hdc=pdc->GetSafeHdc();CRect rect;GetDlgItem(ID)->GetClientRect(&rect);CvvImage vvimg;vvimg.CopyOf(img);vvimg.DrawToHDC(hdc , &rect);ReleaseDC(pdc);}(4)、测试新建一个MFC对话框项目,添加一个pic控件,ID为IDC_IMG,添加一个图片显示按钮ShowImg,双击按钮添加消息处理函数,如下建立一个全局变量IplImage* opencv_image;void OnBnClickedShowimg(){Hobject Image;read_image(&Image,"E:/.../1.jpg");opencv_image=HImageToIplImage(Image);DrawPicToHDC(opencv_image,IDC_IMG);cvReleaseImage(&opencv_image);}
参考:http://blog.csdn.net/taily_duan/article/details/51073335
0 0
- IplImag、HImage相互转换
- 【OpenCV】IplImag、HImage相互转换
- Mat转换为IplImag*类型
- halcon HImage 与opencv 里的 IplImage互相转换
- C# 使用Sentech相机sdk 获取图像,转换为halcon HImage的两种方法
- Halcon 里Hobject 转HImage
- 变量中的相互转换
- 宽窄字符相互转换
- 整数小数相互转换
- RGB YUV420 相互转换
- String Date 相互转换
- LPTSTR CString 相互转换
- native2ascii相互转换
- latex word相互转换
- unicode-汉字相互转换
- 字符串的相互转换
- 字符串相互转换
- 数组、泛型 相互转换
- 如何在android studio建立module,然后实现不同module间的工程依赖
- Centos 安装redis
- 磁盘加密与其自动挂载及加密清除
- Density Peak改进(Hierarchical)
- Java -> 内部类与匿名内部类
- IplImag、HImage相互转换
- 深度学习笔记三-卷积神经网络中的部分问题
- 字典树的题目 找了个时间刷了一点字典树的题目
- Toolbar基本使用
- 系统调用sys_write的过程
- 求助这个netflow数据都代表什么。
- iOS的崩溃率高于Android?来自听云的数据告诉你真相
- 类与对象(附加)---day3
- 【AngularJs】使用angular-cli实现多国语言i18n