在读取像素值时,如何使用OpenCV和Python一致地读取和更改每个像素?

时间:2018-02-02 18:53:16

标签: python opencv computer-vision opencv3.0

您好,感谢您阅读我的问题!我注意到当我尝试使用“(b,g,r)= image [c,r]”更改它之前读取像素值时,python会跳过大部分像素吗?有没有任何理由,是否有任何解决方法?我正在努力的是尝试增强图像中的任何红色,但如果python正在跳过像素,它将无法工作......

import argparse
import cv2
import numpy as np

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
           help="Path to the image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])

for r in range(image.shape[1]):
    for c in range(image.shape[0]):
        count = count + 1

    #without this line the code works fine but when it is included it 
    #skips most of the pixels...
    (b,g,r) = image[c,r]

    canvas[c, r] = (255, 255, 255)

    cv2.imshow("changed canvas", canvas)
    cv2.waitKey(0)

1 个答案:

答案 0 :(得分:0)

正确的用法是使用numpy的ndarray函数项和itemset

读取像素(0,0)处的红色值:

#note instead of rgb opencv writes the order as bgr so red is last
image.item(0,0,2)

设置:

image.itemset((0,0,2), 250)