我正在用 python 中的 opencv 做一些简单的程序。我想自己编写一些算法,因此需要获取图像中的“原始”图像数据。例如,我不能只做 image[i,j],我怎样才能得到数字?
谢谢
最佳答案
使用 LoadImageM
将图像文件直接加载到 cvmat
中的快速示例:
import cv
path = 'stack.png'
mat = cv.LoadImageM(path, cv.CV_LOAD_IMAGE_UNCHANGED)
x, y = 42, 6
print type(mat)
print mat[y, x]
输出:
<type 'cv.cvmat'>
(21.0, 122.0, 254.0)
显示如何将一个或多个颜色 channel 乘以 0.5
的快速示例:
for x in xrange(mat.cols):
for y in xrange(mat.rows):
# multiply all 3 components by 0.5
mat[y, x] = tuple(c*0.5 for c in mat[y, x])
# or multiply only the red component by 0.5
b, g, r = mat[y, x]
mat[y, x] = (b, g, r * 0.5)
关于python - Opencv ...获取 IPLImage 或 CvMat 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6127314/