如何获得[0-255]范围python内的灰度图像,像素矩阵值

时间:2015-10-28 10:26:17

标签: python opencv

我是新手,我正在尝试访问灰度图像的像素值

这里是我的代码

im = cv.LoadImage("new.bmp")
# displaying the matrix form of image
for i in range(im.height):
 for j in range(im.width):
    print im[i,j],

print "\n",i    

我在输出RGB的每个像素值时都会看到输出的快照

enter image description here

2 个答案:

答案 0 :(得分:2)

自OpenCV 2和3起,LoadImage()已停止使用。请使用最新版本的OpenCV和imread()

<强>使用 -

import cv2
img = cv2.imread("new.bmp", 0) #since the image is grayscale, we need only one channel and the value '0' indicates just that
for i in range (img.shape[0]): #traverses through height of the image
    for j in range (img.shape[1]): #traverses through width of the image
        print img[i][j]

文档: http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#imread

答案 1 :(得分:0)

首先使用最新的Opencv 3.将图像转换为灰度并按照此代码打印像素值

import cv2

im = cv2.imread("\Test.jpg")

gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
#You can directly use gray = cv2.imread("\Test.jpg", 0) for input grayscale image
print(gray)