Python cv.Get2D颜色坐标
我想点击一个对象,得到像素坐标,这是我之前选择的颜色。 我在网上找到了这个代码Python cv.Get2D颜色坐标,python,opencv,Python,Opencv,我想点击一个对象,得到像素坐标,这是我之前选择的颜色。 我在网上找到了这个代码 import cv tolerancia = 30 def evento_mouse(event,x,y,flags,param): if event==cv.CV_EVENT_LBUTTONDOWN: pixel=cv.Get2D(imagen,y,x) print 'X =',x,' Y =',y print 'R =',pixel[2],'G =',p
import cv
tolerancia = 30
def evento_mouse(event,x,y,flags,param):
if event==cv.CV_EVENT_LBUTTONDOWN:
pixel=cv.Get2D(imagen,y,x)
print 'X =',x,' Y =',y
print 'R =',pixel[2],'G =',pixel[1],'B =',pixel[0]
cv.InRangeS(imagen,(pixel[0]-tolerancia,pixel[1]-tolerancia,pixel[2]- tolerancia),(pixel[0]+tolerancia,pixel[1]+tolerancia,pixel[2]+tolerancia),temporal)
cv.ShowImage('Color',temporal)
c = cv.Get2D(temporal,y,x)
print c[0],c[1],c[2] # I always get: 255, 0, 0
imagen=cv.LoadImage('prueba2.png')
cv.ShowImage('Prueba',imagen)
temporal=cv.CreateImage(cv.GetSize(imagen),cv.IPL_DEPTH_8U,1)
cv.SetMouseCallback('Prueba',evento_mouse)
cv.WaitKey(0)
我想看看像素是白色还是黑色。但是我总是得到相同的值:255,0,0(蓝色=255)首先,我想你需要检查你的下限和上限是否在0到255之间 其次,您的“时态”变量是一个“掩码”。如果位置(x,y)处的像素值在上下限范围内,则该值设置为255。它不包含给定范围内的所有像素 下面是我在测试图像上尝试的一些代码。注意,我使用了一些numpy,但使用cv2.cv.fromarray()将其转换为CvMat以匹配您的代码
#!/usr/bin/env python
import cv2
import numpy as np
def main():
image = cv2.imread("forest.jpg")
imageMat = cv2.cv.fromarray(image)
dst = cv2.cv.fromarray(np.zeros((imageMat.rows, imageMat.cols), np.uint8))
x = 250 # Manually set pixel location. You can get this from your mouse event handler.
y = 500
pixel = image[y, x] # Note y index "row" of matrix and x index "col".
tolerance = 10
# Ensure your bounds are within 0 and 255.
lower = tuple(map(lambda x: int(max(0, x - tolerance)), pixel))
upper = tuple(map(lambda x: int(min(255, x + tolerance)), pixel))
# Get mask of all pixels that satisfy range condition and store it in dst.
cv2.cv.InRangeS(imageMat, lower, upper, dst)
mask = np.asarray(dst) # Convert back to numpy array.
cv2.imshow("Mask", mask) # The mask indicating which pixels satisfy range conditions
cv2.imshow("Image", image)
extracted = np.zeros_like(image) # The pixels that satisfy range condition.
extracted[np.where(mask)] = image[np.where(mask)]
cv2.imshow("extracted", extracted)
cv2.waitKey()
if __name__ == "__main__":
main()
这是python2版本:
#!/usr/bin/env python
import cv2
import numpy as np
def main():
image = cv2.imread("forest.jpg")
x = 230 # Manually set pixel location. You can get this from your mouse event handler.
y = 300
pixel = image[y, x] # Note y index "row" of matrix and x index "col".
tolerance = 30
# Ensure your bounds are within 0 and 255.
lower = map(lambda x: max(0, x - tolerance), pixel)
upper = map(lambda x: min(255, x + tolerance), pixel)
lower = np.asarray(lower)
upper = np.asarray(upper)
mask = cv2.inRange(image, lower, upper) # Notice we can just get mask without having to allocate it beforehand.
cv2.imshow("Mask", mask) # The mask indicating which pixels satisfy range conditions
cv2.imshow("Image", image)
extracted = np.zeros_like(image) # The pixels that satisfy range condition.
extracted[np.where(mask)] = image[np.where(mask)]
cv2.imshow("extracted", extracted)
cv2.waitKey()
if __name__ == "__main__":
main()
有几件事你需要了解 您正在使用OpenCV的旧“cv”界面。在这种情况下,要获得像素值,可以使用函数“cv2.Get2D”,该函数返回相应BGR值的元组 1。为什么是蓝色,即二进制图像为(255,0,0)? 对于彩色图像和灰度/二值图像,返回3个元素的相同元组,但对于灰度图像,第一个元素是像素值,其余两个是无关的,因此它们是零。所以你会得到(255,0,0)等值,因为你正在读取二值图像的像素值(时间) 2。为什么总是(255,0,0)? 单击原始图像时,将创建相应的二值图像,其中与单击的颜色对应的区域将变为白色,而所有其他区域将变为黑色。若你们点击了原始图像中的红色,你们的二值图像将是这样的,所有的红色区域都是白色,剩下的是黑色。很明显,你点击的像素总是白色的。所以,若你们从二值图像中读取那个像素,你们总是只能得到(255,0,0) 我建议您迁移到OpenCV新的Python接口“cv2”模块。它有很多优点。主要优势是Numpy支持,这是一个大问题。您可以检查此软件以进行一些比较:
您还可以从这里获得一些关于cv2的启动教程:cv2的Get2D版本叫什么?在cv2中不是这样的。您可以通过索引简单地访问像素。例如,img[100100]是这样的。好吧,我可能误解了这个问题,但我认为Get2D能够获得鼠标点击的坐标。在cv2中无法获取鼠标点击坐标?有一些标志,如cv2.EVENT\u LBUTTONUP等。请检查opencv官方下载中opencv/samples/python2文件夹中的mouse\u和\u match.py。您可以看到如何在cv2中使用它。