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

查看:116
本文介绍了如何使用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:\Project\Capture1.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('D:\Project\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- beta/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天全站免登陆