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), 但是它们都很慢.
假设有 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), 但是它们都很慢.
相关文章推荐
- opencv——访问图像元素(imagedata widthstep)
- OpenCV Python教程(2、图像元素的访问、通道分离与合并)
- OpenCV Python教程(2、图像元素的访问、通道分离与合并)
- OpenCV----访问图像元素
- OpenCV Python教程(2、图像元素的访问、通道分离与合并)
- OpenCV Python教程(2、图像元素的访问、通道分离与合并)
- OpenCV Python教程(2、图像元素的访问、通道分离与合并)
- OpenCV遍历访问图像的每一个元素
- OpenCV之Python学习笔记(1)(2): 图像的载入、显示和保存 图像元素的访问、通道分离与合并
- 【opencv】图像元素的访问
- OpenCV Python教程(2、图像元素的访问、通道分离与合并)
- OpenCV,多种Mat图像元素访问方式的速度对比
- OpenCV Python教程之图像元素的访问、通道分离与合并
- opencv笔记 3.访问图像元素的四种方法
- OpenCV Python教程(2、图像元素的访问、通道分离与合并)
- opencv学习笔记(十四)——图像像素的访问
- opencv中如何访问IplImage图像中每个像素的值
- OPENCV2 访问图像像素值的三种方法
- 【图像】软件使用--matlab访问向量中的元素
- OpenCV图像数据访问,查询表和时间消耗测试