python-opencv怎样找到要跟踪对象的HSV

来源:互联网 发布:手机淘宝店如何推广 编辑:程序博客网 时间:2024/06/10 13:47

其实这真的很简单,函数 cv2.cvtColor() 也可以用到这里。但是现在你要传入的参数是(你想要
的)BGR 值而不是一副图。例如,我们要找到绿色的 HSV 值,我们只需在终端输入以下命令:

**import cv2import numpy as npgreen=np.uint8([0,255,0])hsv_green=cv2.cvtColor(green,cv2.COLOR_BGR2HSV)error: /builddir/build/BUILD/opencv-2.4.6.1/modules/imgproc/src/color.cpp:3541:error: (-215) (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F)in function cvtColor#scn (the number of channels of the source),#i.e. self.img.channels(), is neither 3 nor 4.##depth (of the source),#i.e. self.img.depth(), is neither CV_8U nor CV_32F.# 所以不能用 [0,255,0] ,而要用 [[[0,255,0]]]# 这里的三层括号应该分别对应于 cvArray , cvMat , IplImagegreen=np.uint8([[[0,255,0]]])hsv_green=cv2.cvtColor(green,cv2.COLOR_BGR2HSV)print hsv_green[[[60 255 255]]]**
**扩展缩放只是改变图像的尺寸大小。OpenCV 提供的函数 cv2.resize()可以实现这个功能。图像的尺寸可以自己手动设置,你也可以指定缩放因子。我们可以选择使用不同的插值方法。在缩放时我们推荐使用 cv2.INTER_AREA,在扩展时我们推荐使用 v2.INTER_CUBIC(慢) 和 v2.INTER_LINEAR。默认情况下所有改变图像尺寸大小的操作使用的插值方法都是 cv2.INTER_LINEAR。你可以使用下面任意一种方法改变图像的尺寸:**
**# -*- coding: utf-8 -*-"""@author: Andrew"""import cv2import numpy as npimg=cv2.imread('tu.jpg')res=cv2.resize(img,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC)height,width=img.shape[:2]res=cv2.resize(img,(2*width,2*height),interpolation=cv2.INTER_CUBIC)while(1):    cv2.imshow('res',res)    cv2.imshow('img',img)    if cv2.waitKey(1)&0xFF==27:        breakcv2.destroyAllWindows()**
0 0