Python Open CV2颜色检测遮罩到像素坐标 [英] Python Open CV2 Color Detection Mask to Pixel Coordinates

查看:459
本文介绍了Python Open CV2颜色检测遮罩到像素坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Python进行单个图像的颜色检测.加载图像并建立RGB(或CV2中的BGR)后,我使用以下两行代码生成蒙版和输出图像.

I am currently working in Python to do color detection on a single image. After loading my image and establishing my RGB (or BGR in CV2), I use the following 2 lines to produce a mask and a output image.

mask = cv2.inRange(image, lower, upper)
output = cv2.bitwise_and(image, image, mask = mask)

然后代码显示以下图像.

Then the code displays the following image.

但是现在,我想拍摄经过处理的图像并提取绿线的像素坐标点.

But now, I would like to take the processed image and extract pixel coordinate points for the green line.

谢谢.任何帮助将不胜感激.

Thanks. Any help would be appreciated.

推荐答案

那么,对图像的二值化版本上的findNonZeros()怎么样? 从黑色背景上带有绿线的图像开始:

So, how about findNonZeros() on a binarised version of your image ? Starting with the image with the green line on black background :

import cv2
import numpy as np

img = cv2.imread(output.png)
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #converting to grayscale
img = img.astype(np.uint8)

#get all non zero values
coord = cv2.findNonZero(img)

在另一个问题上已经指出,您也可以使用numpy的函数nonzeros.它给出了相同的结果,但是我发现它要慢一些

EDIT : It has been pointed out on another question that you can also use numpy's function nonzeros. It gives the same results, but I find it to be slower

import cv2
import numpy as np
import time 

so=cv2.imread(your_image,0)

start1=time.clock()
coord=cv2.findNonZero(so)
end1=time.clock()

start2=time.clock()
coord2=np.nonzero(so)
end2=time.clock()

print("cv2.findNonZeros() takes "+str(end1-start1)+" seconds.")
print("np.nonzero() takes       "+str(end2-start2)+" seconds.")

>>> cv2.findNonZeros() takes 0.003266 seconds.
>>> np.nonzero() takes       0.021132 seconds.

这篇关于Python Open CV2颜色检测遮罩到像素坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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