簡體   English   中英

使用 Python 檢查 OpenCV 中的像素顏色

[英]Checking pixel color in OpenCV with Python

我目前正在使用 python 和 OpenCV 開發一個項目。 對於項目的一部分,我想檢查一個特定的像素(特別是坐標為 100、100 的像素)是否不等於黑色。 我的代碼如下。

import cv2

img = cv2.imread('/Documents/2016.jpg')

if img[100, 100] != [0, 0, 0]:
    print("the pixel is not black")

當我去終端玩樂時,我收到此錯誤。

File "/Documents/imCam.py", line 5, in <module>
if img[100, 100] != [0, 0, 0]:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我究竟做錯了什么?

正如它所說,您正在將列表與多個條目進行比較,這太不精確了。

你必須使用numpy.any like

import cv2
import numpy as np

img = cv2.imread('/Documents/2016.jpg')

if np.any(img[100, 100] != 0):
    print("the pixel is not black")
import cv2

image = cv2.imread('abc.jpg')

if image[50, 50, 0] != 0:
    print("the pixel is not black")

嘗試這個 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM