怎么访问图像像素

来源:互联网 发布:mac 音频剪辑合成软件 编辑:程序博客网 时间:2024/05/01 15:20

怎么访问图像像素

(坐标是从0开始的,并且是相对图像原点的位置。图像原点或者是左上角 (img->origin=IPL_ORIGIN_TL) 或者是左下角 (img->origin=IPL_ORIGIN_BL) )

  • 假设有 8-bit 1-通道的图像 I (IplImage* img):
I(x,y) ~ ((uchar*)(img->imageData + img->widthStep*y))[x]
  • 假设有 8-bit 3-通道的图像 I (IplImage* img):
I(x,y)blue ~ ((uchar*)(img->imageData + img->widthStep*y))[x*3]I(x,y)green ~ ((uchar*)(img->imageData + img->widthStep*y))[x*3+1]I(x,y)red ~ ((uchar*)(img->imageData + img->widthStep*y))[x*3+2]
例如,给点 (100,100) 的亮度增加 30 ,那么可以这样做:
CvPoint pt = {100,100};((uchar*)(img->imageData + img->widthStep*pt.y))[pt.x*3] += 30;((uchar*)(img->imageData + img->widthStep*pt.y))[pt.x*3+1] += 30;((uchar*)(img->imageData + img->widthStep*pt.y))[pt.x*3+2] += 30;
或者更高效地:
CvPoint pt = {100,100};uchar* temp_ptr = &((uchar*)(img->imageData + img->widthStep*pt.y))[pt.x*3];temp_ptr[0] += 30;temp_ptr[1] += 30;temp_ptr[2] += 30;
  • 假设有 32-bit 浮点数, 1-通道 图像 I (IplImage* img):
I(x,y) ~ ((float*)(img->imageData + img->widthStep*y))[x]
  • 现在,一般的情况下,假设有 N-通道,类型为 T 的图像:
I(x,y)c ~ ((T*)(img->imageData + img->widthStep*y))[x*N + c]
你可以使用宏 CV_IMAGE_ELEM( image_header, elemtype, y, x_Nc )
I(x,y)c ~ CV_IMAGE_ELEM( img, T, y, x*N + c )