1

Im using this command thats is OpenCV image capturing and using PIL for processing. It take almost 2 second to capture and completing the processing. I need more simple command and fastest processing to work for my robots. Anyone have suggestion please be advice.

import cv2
from cv2 import *
from PIL import Image 
import RPi.GPIO as GPIO
import time
im = 0
.
.
.
.
.
def imgCrop(im):
        box = (0, 190, 640, 200)
        region = im.crop(box)
        region.save('crop.jpg')
    return region

def imgThres(im):
        gray = im.convert('L')
        bw = gray.point(lambda x: 0 if x<150 else 255, '1')
        bw.save("bw.jpg")
    return bw

def find_centroid(im, rez):
        width, height = im.size
        XX, YY, count = 0, 0, 0
        for x in xrange(0, width, rez):
            for y in xrange(0, height, rez):
                    if im.getpixel((x, y)) == 255:
                        XX += x
                        YY += y
                        count += 1
        return XX/count, YY/count

def camera ():
    cam = VideoCapture(0)   # 0 -> index of camera
    s, img = cam.read()
    if s:    
            imwrite("img.jpg",img) #save image
    imageFile = "img.jpg"
    Img = Image.open(imageFile)
    cImg = imgCrop(Img)
    tImg = imgThres(cImg)
    print find_centroid(tImg, 1)

        cen = find_centroid(tImg, 1)
        diff = cen[0] - 320
        if diff > 10:
        right()
            print 'right'
        if diff < -10:
        left()
            print 'left'
        else:
        forward()
            print 'straight'


while True:
    camera ()

Im using raspberry pi B as my micro-controller and using webcam to capture image. For info this command only for image capture and processing. There also have robot motion command but I not included here.

timekeeper
  • 43
  • 6
  • have you profiled your code to find out where the time is spent? – root Mar 06 '14 at 07:20
  • how to do that? im just waiting. it more in processing section – timekeeper Mar 06 '14 at 07:31
  • http://stackoverflow.com/questions/582336/how-can-you-profile-a-python-script – root Mar 06 '14 at 07:47
  • I'm not familiar with python syntax, but: do you call `find_centroid` twice (once only for displaying?) and do you fetch the image from webcam, save it to disk (including jpg compression) and load it afterwards from disk instead of using the image directly? – Micka Mar 06 '14 at 10:14
  • and is there an image writing in each function? that might be quite expensive... – Micka Mar 06 '14 at 10:18

0 Answers0