opencv学习笔记2--存取图像元素
生活随笔
收集整理的這篇文章主要介紹了
opencv学习笔记2--存取图像元素
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
最近了解了下如何存取圖像元素,在這里歸納總結下,也是記錄下,以后需要的時候可以復習下。
假設需要讀取在i行j列像點的第k通道. 其中, 行數(shù)i的范圍為[0, height-1], 列數(shù)j的范圍為[0, width-1], 通道k的范圍為[0, nchannels-1].
?
1.間接存取: (比較通用, 但效率低, 可讀取任一類型圖像數(shù)據(jù))
對單通道字節(jié)圖像:
IplImage*img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); CvScalar s; s=cvGet2D(img,i,j); // get the (i,j) pixel value printf("intensity=%f\n",s.val[0]); s.val[0]=111; cvSet2D(img,i,j,s); // set the (i,j) pixel value對多通道浮點或字節(jié)圖像:
IplImage*img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3); CvScalar s; s=cvGet2D(img,i,j); // get the (i,j) pixel value printf("B=%f, G=%f, R=%f\n",s.val[0],s.val[1],s.val[2]); s.val[0]=111; s.val[1]=111; s.val[2]=111; cvSet2D(img,i,j,s); // set the (i,j) pixel value2.直接存取: (效率高, 但容易出錯)
對單通道字節(jié)圖像:
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); ((uchar *)(img->imageData + i*img->widthStep))[j]=111;對多通道字節(jié)圖像:
IplImage*img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3); ((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels +0]=111; // B ((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels +1]=112; // G ((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels +2]=113; // R對多通道浮點圖像:
IplImage*img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3); ((float *)(img->imageData + i*img->widthStep))[j*img->nChannels +0]=111; // B ((float *)(img->imageData + i*img->widthStep))[j*img->nChannels +1]=112; // G ((float *)(img->imageData + i*img->widthStep))[j*img->nChannels +2]=113; // R3.用指針直接存取 : (在某些情況下簡單高效)
對單通道字節(jié)圖像:
IplImage* img =cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); int height = img->height; int width = img->width; int step = img->widthStep/sizeof(uchar); uchar* data =(uchar *)img->imageData; data[i*step+j] = 111;對多通道字節(jié)圖像:
IplImage* img =cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3); int height = img->height; int width = img->width; int step = img->widthStep/sizeof(uchar); int channels =img->nChannels; uchar* data =(uchar *)img->imageData; data[i*step+j*channels+k] = 111;對單通道浮點圖像(假設用4字節(jié)調整):
IplImage* img =cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3); int height = img->height; int width = img->width; int step = img->widthStep/sizeof(float); int channels =img->nChannels; float * data =(float *)img->imageData; data[i*step+j*channels+k] = 111;4.使用 c++ wrapper 進行直接存取: (簡單高效)
對單/多通道字節(jié)圖像,多通道浮點圖像定義一個 c++ wrapper:
template<class T> classImage {private:IplImage* imgp;public:Image(IplImage* img=0){imgp=img;}~Image(){imgp=0;}void operator=(IplImage*img) {imgp=img;}inline T*operator[](const int rowIndx) {return ((T*)(imgp->imageData + rowIndx*imgp->widthStep));} }; typedef struct{unsigned char b,g,r; } RgbPixel; typedef struct{float b,g,r; } RgbPixelFloat; typedef Image<RgbPixel> RgbImage; typedef Image<RgbPixelFloat> RgbImageFloat; typedef Image<unsigned char> BwImage; typedefImage<float> BwImageFloat;單通道字節(jié)圖像:
IplImage*img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); BwImage imgA(img); imgA[i][j] = 111;多通道字節(jié)圖像:
IplImage*img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3); RgbImage imgA(img); imgA[i][j].b = 111; imgA[i][j].g = 111; imgA[i][j].r = 111;多通道浮點圖像:
IplImage*img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3); RgbImageFloat imgA(img); imgA[i][j].b = 111; imgA[i][j].g = 111; imgA[i][j].r = 111;總結
以上是生活随笔為你收集整理的opencv学习笔记2--存取图像元素的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 收集Redis16个最常见面试问题
- 下一篇: 最幸福的码农在用什么编程语言?