opencv : 基础数据结构

IplImage的基本数据结构:

IplImage |-- int nChannels; // 颜色通道数目 (1,2,3,4) |-- int depth; // 像素的位深: | // IPL_DEPTH_8U, IPL_DEPTH_8S, | // IPL_DEPTH_16U,IPL_DEPTH_16S, | // IPL_DEPTH_32S,IPL_DEPTH_32F, | // IPL_DEPTH_64F |-- int width; // 图像宽度(像素为单位) |-- int height; // 图像高度 |-- char* imageData; // 图像数据指针 | // 注意彩色图像按BGR顺序存储数据 |-- int dataOrder; // 0 - 将像素点不同通道的值交错排在一起,形成单一像素平面 | // 1 - 把所有像素同通道值排在一起,形成若干个通道平面,再把平面排列起来 | // cvCreateImage 只能创建像素交错排列式的图像 |-- int origin; // 0 – 像素原点为左上角, | // 1 – 像素原点为左下角 (Windows bitmaps style) |-- int widthStep; // 相邻行的同列点之间的字节数 |-- int imageSize; // 图像的大小(字节为单位) = height*widthStep |-- struct _IplROI *roi;// 图像的感兴趣区域(ROI). ROI非空时对图像的 | // 处理仅限于ROI区域. |-- char *imageDataOrigin; // 图像数据未对齐时的数据原点指针 | // (需要正确地重新分配图像内存 ) | // (needed for correct image deallocation) |-- int align; // 图像数据的行对齐: 4 or 8 byte alignment | // OpenCV 中无此项,采用widthStep代替 |-- char colorModel[4]; // 颜色模型 – OpenCV中忽略此项


#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h> 
 
int main()
{
  IplImage* img = 0; 
  int height,width,step,channels;
  uchar *data;
  int i,j,k; 
 // load an image  
  img=cvLoadImage("D:\\Demo.bmp"); //图片位置
  if(!img){
    printf("Could not load image file: \n");
    exit(0);
  } 
 
  // get the image data
  height    = img->height;
  width     = img->width;
  step      = img->widthStep;
  channels  = img->nChannels;
  data      = (uchar *)img->imageData;
  printf("Processing a %dx%d image with %d channels\n",height,width,channels); 
 
  // create a window
  cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); 
  cvMoveWindow("mainWin", 100, 100); 
 
  // invert the image
  // 相当于 cvNot(img);
  // IplImage *pDstImg = cvCreateImage(cvGetSize(img),img->depth,img->nChannels);
  // cvNot(img, pDstImg);
  for(i=0;i<height;i++) for(j=0;j<width;j++) for(k=0;k<channels;k++)
    data[i*step+j*channels+k]=255-data[i*step+j*channels+k]; 
 
  // show the image
  cvShowImage("mainWin", img ); 
 
  // wait for a key
  cvWaitKey(0); 
 
  // release the image
  cvReleaseImage(&img );
  return 0;
}

 





posted @ 2012-07-04 15:20  chchche  阅读(115)  评论(0)    收藏  举报