cv.get2d颜色坐标



我想单击一个对象并获取我之前选择的颜色的像素坐标。我在互联网上找到了此代码

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)

您需要了解几件事。

您正在使用OpenCV的旧" CV"接口。在这种情况下,要获得像素值,您可以使用函数'cv2.get2d',该函数返回了相应的bgr值的元组。

1。为什么要蓝色,即二进制图像的蓝色(255,0,0)?

对于颜色图像和灰度/二进制图像,都会返回3个元素的元组,但是对于灰度图像,第一个元素是像素值,其余两个是无关紧要的,因此它们是零。因此,由于您正在读取二进制图像的像素值(时间)。

,因此获得了(255,0,0)等的值。

2。为什么总是(255,0,0)?

单击原始图像时,会创建相应的二进制图像,其中与您单击的颜色相对应的区域变为白色,所有其他区域都变成黑色。如果您单击原始图像中的红色,则您的二进制图像将如此,所有红色区域都将是白色的,剩余的黑色。因此,显然,您点击的像素总是是白色的。因此,如果您从二进制图像中读取该像素,您只会得到(255,0,0)。

我想建议您迁移到OpenCV新的Python接口'CV2'模块。它具有很多优势。主要优势是Numpy支持,这很重要。您可以检查此SOF以进行一些比较:所有这些OpenCV Python接口之间有什么不同?

您还可以从这里获得CV2上的一些启动教程:www.opencvpython.blogspot.com

首先,我认为您需要检查您的下层和上限是否在0到255的范围内。

其次,您的"时间"变量是"掩码"。如果该位置的像素值在您的下限和上限内,则将位置设置为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()

最新更新