图像分割工作总结(五)opencv遍历图像

时间:2022-06-03 16:05:59

(1)间接访问(调用函数访问)效率不高

IplImage*src=cvLoadImage(filename,0);
CvScalar s=cvGet2D(src,i,j);
printf("%f",s.val[0]);
cvSet2D(src,i,j,s);

(2)直接访问(单通道单字节)

IplImage* src=cvLoadImage(filename,0);
(uchar*)(src->imagedata+h*src->widthstep)[w]=111;


(3)指针访问(单通道)

uchar* data=(uchar*)src->imagedata;
int step=src->widthstep/sizeof(uchar);
data[h*step+w]=111;


指针访问(多通道)

float*data=(float*)src->imagedata;
int step=src->widthstep/sizeof(float);
data[h*step+w*channels+k]=111;


(4)利用C++模板

template<class T> class Image
{
    public:
          IplImage* pimg;
          Image(IplImage* img=0){pimg   =  img;}
          ~Image(){pimg=0;}

           void operater=(pimg=img;)
           inline T* operater[](const int row)
         {return ((T*)(pimg->imagedata+row*pimg->widthstep));}
}
struct RgbPixel
{
  unsigned char r,g,b;
};

struct RgbPixelFloat
{
   float r,g,b;
}

Image<unsigned char> imageA(src);
imageA[i][j]=111;