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

2024-09-28 05:27:25 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这是我的密码

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


Tags: in图像密码newforrange像素cv
2条回答

首先使用最新的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)

从OpenCV 2和OpenCV 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

相关问题 更多 >

    热门问题