python写元旦快乐_python转C的提议,新年快乐

python转C的建议,新年快乐

2014新年快乐!

我这有份别人写的Python代码,做OpenCV颜色识别的。我要把它换成C,实在不太了解Python,不知道从何下手。希望朋友们给点意见,谢谢啦,新年愉快,恭喜发财!

下面的 COLOR_RANGE={,,,} self 那些 在C里都是如何处理的

#!/usr/bin/python

import sys

from math import sqrt

from threading import Thread

from opencv.cv import *

from opencv.highgui import *

import Xlib

# Global Variables

#storage = cvCreateMemStorage(0)

#from Xlib import X,display,Xutil

storage=cvCreateMemStorage(0)

capture = cvCreateCameraCapture( 0 )

COLOR_RANGE={

'yellow': (cvScalar(10, 100, 100, 0), cvScalar(40, 255, 255, 0)),\

'red': (cvScalar(0, 0, 0, 0), cvScalar(190, 255, 255, 0)),\

'blue': (cvScalar( 90 , 84 , 69 , 0 ), cvScalar( 120 , 255 , 255 , 0)),\

'green': (cvScalar( 40 , 80 , 32 , 0), cvScalar( 70 , 255 , 255 , 0)),\

'orange': (cvScalar( 160 , 100 , 47 , 0 ), cvScalar( 179 , 255 , 255 , 0 ))\

}

DISPLAY_COLOR={

'yellow':CV_RGB(255,255,0)

,'red':CV_RGB(255,0,0)

,'blue':CV_RGB(0,0,255)

,'green':CV_RGB(0,110,0)

}

class Tracker(Thread):

def __init__(self,color,flag):

Thread.__init__(self)

self.color=color

self.display=DISPLAY_COLOR[color]

self.path=cvCreateImage(cvSize(640,480),8,3)

self.lastx=0

self.lasty=0

self.h_min=COLOR_RANGE[color][0]

self.h_max=COLOR_RANGE[color][1]

self.flag=flag

if self.flag:

cvNamedWindow(self.color,1)

def poll(self,img):

if 1:

thresh = cvCreateImage( cvSize(img.width,img.height), 8, 1 )

new_img=cvCreateImage( cvSize(img.width,img.height),8 ,3 )

cvCopy(img,new_img)

cvCvtColor(img, new_img, CV_BGR2HSV )

cvInRangeS(new_img,self.h_min,self.h_max,thresh)

cvSmooth(thresh,thresh,CV_GAUSSIAN,9,9)

circles=cvHoughCircles(thresh,storage,CV_HOUGH_GRADIENT,2,thresh.height/4,200,100,25,0)

maxRadius=0

x=0

y=0

found=False

for i in range(circles.total):

circle=circles[i]

if circle[2]>maxRadius:

found=True

radius=int(circle[2])

maxRadius=int(radius)

x=int(circle[0])

y=int(circle[1])

if found:

cvCircle( img, cvPoint(x,y),3, CV_RGB(0,255,0), -1, 8, 0 )

cvCircle( img, cvPoint(x,y),maxRadius, CV_RGB(255,0,0), 3, 8, 0 )

print self.color+ " Ball found at",x,y

if self.lastx > 0 and self.lasty > 0:

cvLine(self.path,cvPoint(self.lastx,self.lasty),cvPoint(x,y),self.display,5)

self.lastx=x

self.lasty=y

cvAdd(img,self.path,img)

if self.flag:

cvShowImage(self.color,thresh)

cvShowImage("result",img)

if( cvWaitKey( 10 ) >= 0 ):

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值