【问题标题】:How can I consistently read and change each individual pixel using OpenCV with Python when reading pixel value?在读取像素值时,如何使用 OpenCV 和 Python 一致地读取和更改每个单独的像素?
【发布时间】:2018-07-13 08:04:38
【问题描述】:

您好,感谢您阅读我的问题!我注意到,当我尝试在使用“(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)

【问题讨论】:

  • 你的迭代变量r和红色通道变量r冲突,重命名一个。
  • 谢谢!哇,不敢相信我错过了……

标签: python opencv computer-vision opencv3.0


【解决方案1】:

正确用法是使用numpy的ndarray函数item和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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 2017-01-31
    相关资源
    最近更新 更多