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

Rag*_*san 5 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    
Run Code Online (Sandbox Code Playgroud)

我正在获取 RGB 每个像素值的输出,请参阅输出的快照

在此输入图像描述

Cra*_*akC 6

从 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]
Run Code Online (Sandbox Code Playgroud)

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