OpenCV中文网站

 找回密码
 立即注册
搜索
热搜: 安装 配置
查看: 4464|回复: 7

Opencv的困惑!!

[复制链接]
发表于 2008-12-24 11:49:06 | 显示全部楼层 |阅读模式
学OpenCV有了一段时间,感觉不知道怎么把OpenCV融入到其他图像处理的程序里? 以前做bmp图像处理,图像结构有文件头,信息头,调色板和数据区,而OpenCV直接一个IpImage数据就全部搞定了,结构好像和bmp不一样。那IpImage*到底是相当与哪一块的指针呢???我如果用其他得到BMP图像数据区的指针,然后再想用OPenCV做一些处理应该怎么调用函数呢?举个列子,我用视频采集的函数得到了当前帧图像的数据区的指针pData*,想做个腐蚀运算,调用CVEeode()函数,前两个参数的数据类型是CvArr*类型,
          IplImage* dst;
               dst=cvCreateImage(cvSize(w,h),IPL_DEPTH_8U,1);
               cvErode(pdata,Dst,NULL,1);   //pdata图像数据区指针
可是运行时直接非法错误,郁闷,是不是数据类型不匹配,不能把BMP数据区指针赋给CvArr*指针啊,哪位能指点一下,谢谢!!!!
回复

使用道具 举报

发表于 2008-12-24 14:26:42 | 显示全部楼层

Opencv的困惑!!

请看这里
<!-- m --><a class=\"postlink\" href=\"http://blog.csdn.net/hunnish/archive/2008/09/18/2947595.aspx#981474\">http://blog.csdn.net/hunnish/archive/20 ... spx#981474</a><!-- m -->
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-12-24 22:38:00 | 显示全部楼层

Opencv的困惑!!

呵呵 很有价值  谢谢!!
回复 支持 反对

使用道具 举报

发表于 2008-12-24 23:22:35 | 显示全部楼层

Opencv的困惑!!

不客气,这个方面的知识,我也欠缺。你看我还在那个网页里面回复过呢,云里雾里,一来MFC不是很精通,而来Opencv也不是很精通
你要是看懂了,写个例程教导我一下吧。
回复 支持 反对

使用道具 举报

发表于 2008-12-26 11:26:23 | 显示全部楼层

Opencv的困惑!!

这段时间太忙了,没过来看看。今天碰上了就给个礼物吧。Merry Christmas!
这是我自己写的一个BMP和iplImage转换的类。凑活着用吧,不好用的地方请提出来。
这是头文件
  1. class BMP {
  2. public:
  3.         BMP():bmpData(NULL) {
  4.                 memset(&biHeader, 0, sizeof(biHeader));
  5.         }
  6.         BMP(const BMP & img);
  7.         BMP(const IplImage &img);
  8.         BMP(int width, int height, int bitCount);
  9.         ~BMP(){ delete &#91;&#93; bmpData; }
  10.         bool CreateImage(const BITMAPINFOHEADER &biHeader);
  11.         //Export
  12.         IplImage * BMP2Ipl();
  13.         //void Show(HWND hWnd, int nID);
  14.         //void Show(CDC *pDC,  CRect & rect);
  15.         //void Show(HWND hWnd);
  16.         void ReSize(int newW, int newH);
  17. private:
  18.         void CopyData(char *dest, const char *src, int dataByteSize,
  19.                 bool isConvert, int height);
  20.         // isConvert=true 进行顶左和底左之间的转换(顶左到底左或底左到顶左),否则不转换
  21.         // biSizeImage may be set to zero for BI_RGB bitmaps, so calculate it.
  22.         int ImgSize() {
  23.                 return ( biHeader.biHeight * ((biHeader.biWidth * biHeader.biBitCount / 8 + 3) & (-4)));
  24.         }
  25. public:
  26.         void Clear();
  27.         BITMAPINFOHEADER biHeader;
  28.         unsigned char * bmpData;
  29. };
复制代码

这是实现
  1. #include &quot;Base.h&quot;
  2. /**************************************************************************************************
  3. *                                                                                                 *
  4. *  NOTE:                                                                                          *
  5. *    Only use the &quot;8 bit, 1 or 3 channels&quot; image, the BitMap use LowerLeft (底左),              *
  6. *    the IplImage use TopLeft (顶左)                                                            *
  7. *  IplImage:                                                                                      *
  8. *    nChannels = 1 or 3       number of channels                                                  *
  9. *         depth = IPL_DEPTH_8U     pixel depth (8 bit), IPL_DEPTH_8U=8                                 *
  10. *         dataOrder = 0            交叉存取颜色通道                                                    *
  11. *         origin = IPL_ORIGIN_TL   图像数据保存形式,IPL_ORIGIN_BL底左结构, IPL_ORIGIN_TL顶左结构      *
  12. *         align = 4                行数据对齐方式,保证下一行数据从整4字节位置开始                     *
  13. *         width                                                                                        *
  14. *         height                                                                                       *
  15. *         widthStep                图像数据行大小,单位字节                                            *
  16. *         imageSize                图像数据大小(=height*widthStep),单位字节                           *
  17. *         imageData                指向图像数据区,char *                                              *
  18. *  BITMAPINFOHEADER:                                                                              *  
  19. *    biSize                   Specifies the number of bytes required by the structure             *
  20. *         biWidth                                                                                      *
  21. *         biHeight(>0)             biHeight>0底左结构;biHeight<0顶左结构                              *
  22. *         biPlanes = 1             Specifies the number of planes for the target device. This value    *
  23. *                             must be set to 1.                                                   *
  24. *         biBitCount =8 or 24      bits-per-pixel,8-(pixelDepth=8,channels=1),                         *
  25. *                             24-(pixelDepth=8,channels=3)                                        *
  26. *         biCompression = BI_RGB   An uncompressed format                                              *
  27. *         biSizeImage              Specifies the size, in bytes, of the image.This may be set to zero  *
  28. *                             for BI_RGB bitmaps.                                                 *
  29. *         biXPelsPerMeter = 0                                                                          *
  30. *         biYPelsPerMeter = 0                                                                          *
  31. *         biClrUsed = 0                                                                                *
  32. *         biClrImportant = 0                                                                           *
  33. *                                                                                                 *
  34. ***************************************************************************************************/
  35. BMP::BMP(const BMP & img)
  36. {       
  37.         if(!IsSupport(img)) {
  38.                 BMP();
  39.                 return;
  40.         }
  41.        
  42.         //biHeader = img.biHeader;
  43.         PBITMAPINFOHEADER  pBmpH = (PBITMAPINFOHEADER)&img.biHeader;
  44.         memcpy(&biHeader, pBmpH, sizeof(BITMAPINFOHEADER));
  45.         biHeader.biSizeImage = ImgSize();
  46.         bool isLowerLeft = biHeader.biHeight>0;
  47.         //int rowSize=0;
  48.         if(!isLowerLeft)  biHeader.biHeight=-biHeader.biHeight;
  49.         if(bmpData!=NULL) delete&#91;&#93; bmpData;
  50.         bmpData = new unsigned char &#91;biHeader.biSizeImage&#93;;
  51.         //memcpy(bmpData, img.bmpData, img.biHeader.biSizeImage);
  52.         CopyData((char *)bmpData, (char*)img.bmpData, biHeader.biSizeImage,
  53.                 !isLowerLeft, biHeader.biHeight);
  54. }
  55. BMP::BMP(const IplImage &img) {
  56.         if(!IsSupport(img)) {
  57.                 BMP();
  58.                 return;
  59.         }
  60.         bool isTopLeft = (img.origin == IPL_ORIGIN_TL);
  61.         biHeader.biSize = sizeof(BITMAPINFOHEADER);
  62.         biHeader.biWidth = img.width;
  63.         biHeader.biHeight = img.height;
  64.         biHeader.biPlanes = 1;
  65.         biHeader.biBitCount = img.depth * img.nChannels;
  66.         biHeader.biCompression = BI_RGB;
  67.         biHeader.biSizeImage = img.imageSize;
  68.         biHeader.biXPelsPerMeter = 0;
  69.         biHeader.biYPelsPerMeter = 0;
  70.         biHeader.biClrUsed = 0;
  71.         biHeader.biClrImportant = 0;
  72.         if(bmpData!=NULL) delete&#91;&#93; bmpData;
  73.         bmpData = new unsigned char &#91;img.imageSize&#93;;
  74.         //memcpy(bmpData, img.ImageData, img.imageSize);
  75.         CopyData((char*)bmpData, (char*)img.imageData, img.imageSize,
  76.                 isTopLeft, img.height);
  77.         /*int i,j;
  78.         CvScalar s;
  79.         for(i=0;i<img.width;i++)
  80.                 for(j=0;j<img.height;j++){
  81.                         s=cvGet2D(&img,i,j);
  82.                 }
  83.         */
  84. }
  85. BMP::BMP(int width, int height, int bitCount) {
  86.         if(bitCount!=8 && bitCount!=24) return;
  87.         biHeader.biSize = sizeof(BITMAPINFOHEADER);
  88.         biHeader.biWidth = width;
  89.         biHeader.biHeight = height;
  90.         biHeader.biPlanes = 1;
  91.         biHeader.biBitCount = bitCount;
  92.         biHeader.biCompression = BI_RGB;
  93.         biHeader.biSizeImage = ImgSize();
  94.         biHeader.biXPelsPerMeter = 0;
  95.         biHeader.biYPelsPerMeter = 0;
  96.         biHeader.biClrUsed = 0;
  97.         biHeader.biClrImportant = 0;
  98.         if(bmpData!=NULL) delete&#91;&#93; bmpData;
  99.         bmpData = new unsigned char &#91;biHeader.biSizeImage&#93;;
  100.         Clear();
  101. }
  102. // dest:         the destination image
  103. // dataByteSize: the Source image
  104. // height:       source image height
  105. void BMP::CopyData(char *dest, const char *src, int dataByteSize,
  106.                                    bool isConvert, int height) {
  107.         char * p = dest;
  108.         if(!isConvert) {   
  109.                 memcpy(dest, src, dataByteSize);
  110.                 return;
  111.         }
  112.         if(height<=0) return;
  113.         //int height = dataByteSize/rowByteSize;
  114.         int rowByteSize = dataByteSize / height;
  115.         src = src + dataByteSize - rowByteSize ;
  116.         for(int i=0; i<height; i++) {
  117.                 memcpy(dest, src, rowByteSize);
  118.                 dest += rowByteSize;
  119.                 src  -= rowByteSize;
  120.         }       
  121. }
  122. IplImage * BMP::BMP2Ipl() {
  123.         if(!IsSupport(*this)) return NULL;
  124.         IplImage *iplImg;
  125.         int height;
  126.         bool isLowerLeft = biHeader.biHeight>0;
  127.         height = (biHeader.biHeight>0) ? biHeader.biHeight : -biHeader.biHeight;
  128.         iplImg = cvCreateImage( cvSize(biHeader.biWidth, height), IPL_DEPTH_8U, biHeader.biBitCount / 8);
  129.         //iplImg = cvCreateImageHeader( cvSize(biHeader.biWidth, height), IPL_DEPTH_8U, biHeader.biBitCount / 8);
  130.         //cvSetData(iplImg,(char*)bmpData,biHeader.biSizeImage/height);
  131.         CopyData( iplImg->imageData, (char*)bmpData, biHeader.biSizeImage,
  132.                 isLowerLeft, height);
  133.         /*int i,j;
  134.         CvScalar s;
  135.         int channels=biHeader.biBitCount / 8;
  136.         int step=(biHeader.biWidth*channels+3) & -4;
  137.         int loc=0;
  138.         for(i=0;i<iplImg->height;i++){
  139.                 for(j=0;j<iplImg->width;j++){
  140.                         loc=i*step + j*channels;
  141.                         s.val&#91;0&#93;=bmpData&#91;loc&#93;;
  142.                         if(channels==3){
  143.                                 s.val&#91;1&#93;=bmpData&#91;loc+1&#93;;
  144.                                 s.val&#91;2&#93;=bmpData&#91;loc+2&#93;;
  145.                         }
  146.                         cvSet2D(iplImg,i,j,s);
  147.                 }
  148.         }*/
  149.         return iplImg;
  150. }
  151. void BMP::Clear() {
  152.         if(bmpData == NULL) return;
  153.         memset(bmpData, 0, ImgSize());
  154. }
  155. void BMP::ReSize(int newW, int newH) {
  156.         biHeader.biWidth = newW;
  157.         biHeader.biHeight = newH;
  158.         biHeader.biSizeImage = ImgSize();
  159.         if(bmpData!=NULL) delete&#91;&#93; bmpData;
  160.         bmpData = new unsigned char &#91;biHeader.biSizeImage&#93;;
  161.         Clear();
  162. }
  163. bool BMP::CreateImage(const BITMAPINFOHEADER &bih) {
  164.         memset(&biHeader,0,sizeof(BITMAPINFOHEADER));
  165.         delete&#91;&#93; bmpData;
  166.         bmpData = NULL;
  167.         memcpy(&biHeader, &bih, sizeof(BITMAPINFOHEADER));
  168.         biHeader.biSizeImage = ImgSize();
  169.         bmpData = new unsigned char &#91; biHeader.biSizeImage &#93;;
  170.         if(bmpData == NULL) return false;
  171.         else{
  172.                 Clear();
  173.                 return true;
  174.         }
  175. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2008-12-26 11:31:55 | 显示全部楼层

Opencv的困惑!!

不错不错,学习了!我目前还没有用到这一块,需要的人的确是很有用的。
回复 支持 反对

使用道具 举报

发表于 2008-12-26 13:42:54 | 显示全部楼层

Opencv的困惑!!

我把这段代码贴到首页了
[url=http://www.opencv.org.cn/index.php?title=BMP%E4%B8%8EIplImage%E7%9B%B8%E4%BA%92%E8%BD%AC%E6%8D%A2:e4rt2guq]BMP与IplImage相互转换[/url:e4rt2guq]
这段时间太忙了,没过来看看。今天碰上了就给个礼物吧。Merry Christmas!
这是我自己写的一个BMP和iplImage转换的类。凑活着用吧,不好用的地方请提出来。
...
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-12-26 22:06:20 | 显示全部楼层

Opencv的困惑!!

呵呵 谢谢  !!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|OpenCV中文网站

GMT+8, 2024-5-2 12:57 , Processed in 0.009646 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表