您的位置:首页 > 运维架构

OpenCV 之图像篇 怎么访问图像元素

2010-11-04 21:48 399 查看
(坐标起点相对于图像原点 image origin 从 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))[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 )

也有针对各种图像(包括 4-通道)和矩阵的函数(cvGet2D, cvSet2D), 但是它们都很慢.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: