我有下面的代码,我想在很多部分修改它:
如何使用 Raspbery Pi 相机而不是 USB 相机?
对于任何给我提示或编写正确代码的人,我将不胜感激。
代码是:
import cv2.cv as cv
import smbus
import cv2
bus = smbus.SMBus(1)
address = 0x04
def sendData(value):
bus.write_byte(address, value)
return -1
def readData():
state = bus.read_byte(address)
return state
def ColorProcess(img):
imgHSV = cv.CreateImage(cv.GetSize(img) ,8 ,3)
cv.CvtColor(img, imgHSV, cv.CV_BGR2HSV)
imgProcessed = cv.CreateImage(cv.GetSize(img) ,8 ,1)
cv.InRangeS(imgHSV, (100, 94, 84), (109, 171, 143), imgProcessed)
return imgProcessed
def main():
width = 320
height = 240
capture = cv.CreateCameraCapture(0)
cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, width)
cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, height)
cv.NamedWindow("output", 1)
cv.NamedWindow("processed", 1)
while True:
frame = cv.QueryFrame(capture)
cv.Smooth(frame, frame, cv.CV_BLUR, 3)
imgColorProcessed = ColorProcess(frame)
mat = cv.GetMat(imgColorProcessed)
moments = cv.Moments(mat, 0)
area = cv.GetCentralMoment(moments, 0, 0)
moment10 = cv.GetSpatialMoment(moments, 1, 0)
moment01 = cv.GetSpatialMoment(moments, 0, 1)
if (area > 60000):
posX = int(moment10/area)
posY = int(moment01/area)
ali = long(2000000)
state = readData()
if state == 1:
sendData(posX)
sendData(posY)
print 'x: ' + str(posX) + 'y: ' + str(posY)
cv.ShowImage("processed", imgColorProcessed)
cv.ShowImage("output", frame)
if cv.WaitKey(10) >= 0:
break
return;
if __name__ == "__main__":
main()
我将非常感谢任何帮助。
谢谢。