首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

python转C的提议,新年快乐

2014-01-05 
python转C的建议,新年快乐2014新年快乐!我这有份别人写的Python代码,做OpenCV颜色识别的。我要把它换成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 ):


            return


if __name__ == '__main__':
   cvNamedWindow( "result", CV_WINDOW_AUTOSIZE )
   if capture:
      frame_copy = None
   yellow=Tracker("yellow",1)
   green=Tracker("green",1)
   blue=Tracker("blue",1)
   yellow.start()
   green.start()
   blue.start()
   while True:
      img=cvQueryFrame(capture)
      yellow.poll(img)
      green.poll(img)
      blue.poll(img)
      yellow.join()
      green.join()
      blue.join()
      if cvWaitKey(10) >=0:
         sys.exit(1)
   cvDestroyWindow("result")


[解决办法]
Track是个类,self相当于this指针,你可以用C++封装,不想用C++就用C函数实现,COLOR_RANGE是字典。
[解决办法]
支持下了,不懂易
[解决办法]
用STL中map容器。

引用:
Quote: 引用:

Track是个类,self相当于this指针,你可以用C++封装,不想用C++就用C函数实现,COLOR_RANGE是字典。

在C里面有什么好方法  来表达 字典吗

[解决办法]
貌似只是常量,用字典只是方便书写代码,增加可读性而已,c里用数组结合枚举替代即可...
[解决办法]
OpenCV没有C的API?
[解决办法]
不要做A语言代码修改为B语言代码的无用功。
也不要做用A语言代码直接调用B语言代码库这样复杂、这样容易出错的傻事。
只需让A、B语言代码的输入输出重定向到文本文件,或修改A、B语言代码让其通过文本文件输入输出。
即可很方便地让A、B两种语言之间协调工作。

热点排行