我怎么知道在Python中使用OpenCV检测到的白色区域的位置? [英] How do I know the position of white areas detected using OpenCV in python?

查看:1099
本文介绍了我怎么知道在Python中使用OpenCV检测到的白色区域的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在此代码中,我已过滤了视频供稿,以显示白色区域.我怎么知道他们的位置/坐标?(x,y)

For example in this code i have filtered the video feed to show the white areas. How do i know their position/coordinates?(x,y)

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while(1):
    _, frame = cap.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # define range of white color in HSV
    # change it according to your need !
    lower_white = np.array([0,0,0], dtype=np.uint8)
    upper_white = np.array([0,0,255], dtype=np.uint8)

    # Threshold the HSV image to get only white colors
    mask = cv2.inRange(hsv, lower_white, upper_white)
    # Bitwise-AND mask and original image
    res = cv2.bitwise_and(frame,frame, mask= mask)
    cv2.imshow('frame',frame)
    cv2.imshow('mask',mask)
    cv2.imshow('res',res)

    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()

推荐答案

您可以使用numpy方法获取矩阵中所有不为零的值.

You can just use numpy way of getting all the values in a matrix which are not zero.

indcies = numpy.nonzero(res)

这篇关于我怎么知道在Python中使用OpenCV检测到的白色区域的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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