如何使用 OpenCV-Python 访问图像的像素? [英] How do I access the pixels of an image using OpenCV-Python?

查看:38
本文介绍了如何使用 OpenCV-Python 访问图像的像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何遍历图像的所有像素.我试过这个:

I want to know how to loop through all pixels of an image. I tried this:

import cv2
import numpy as np

x = np.random.randint(0,5,(500,500))
img = cv2.imread('D:ProjectCapture1.jpg',0)
p = img.shape
print p
rows,cols = img.shape

for i in range(rows):
    for j in range(cols):
        k = x[i,j]
        print k

它打印一组不是数组形式的垂直数字.我也得到一个数组越界异常.请提出一种方法.

It prints a vertical set of numbers which is not in the form of an array. I am also getting an array out of bounds exception. Please suggest a method.

推荐答案

我不明白你的 x 变量的目的是什么.你不需要它.

I don't see what's the purpose of your x variable. You don't need it.

简单使用:

img = cv2.imread('/path/to/Capture1.jpg',0)
rows,cols,_ = img.shape

for i in range(rows):
    for j in range(cols):
        k = img[i,j]
        print(k)

这确实会打印一组垂直的数字.如果您想修改像素的值,请使用 img.itemset().http://docs.opencv.org/3.0-测试版/doc/py_tutorials/py_core/py_basic_ops/py_basic_ops.html

which will print indeed a vertical set of numbers. If you want to modify the values of the pixels use img.itemset(). http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_core/py_basic_ops/py_basic_ops.html

如果要打印整个数组,请使用 print(img)

If you want to print the whole array then use print(img)

这篇关于如何使用 OpenCV-Python 访问图像的像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆