Ask Your Question

Revision history [back]

One thing you can do is you can sun a separate thread for grabbing the images from the video capture object, and put that captured images in a queue, and from the main thread get a frame from the queue. So while its processing the image for 25ms the grab thread will be able to grab (25/9)=2 frames in the queue ready for the main thread to process and the main thread wont have to wit for image to be captured.

this will completely remove that 9ms time that was being added to your total time

So right now its like : grab frame(9ms)+ process(25ms)=total time (34ms)

with a separate thread running:

grabber thread:->grab(9ms)--->grab(9ms)------->grab(9ms)----->grab(9ms)->-

main thread: ---> wait(9ms)->getFrame()->--Process------(25ms)---getFrame()....

so this way the main thread will only wait 9ms in the beginning for the first frame and wont have to wait again anymore

One thing you can do is you can sun run a separate thread for grabbing the images from the video capture object, and put that captured images in a queue, and from the main thread get a frame from the queue. queue and do your processing.

So while its the main thread is processing the image for 25ms 25ms, the grab grabber thread will be able to grab (25/9)=2 frames in the queue ready for the main thread to process and the main thread wont have to wit wait 9ms for image to be captured.

this will completely remove that 9ms time that was being added to your total time

So right now its like : grab frame(9ms)+ process(25ms)=total time (34ms)

with a separate thread running:

grabber thread:->grab(9ms)--->grab(9ms)------->grab(9ms)----->grab(9ms)->-

main thread: ---> wait(9ms)->getFrame()->--Process------(25ms)---getFrame()....wait(9ms)->getFrame()->--Process------(25ms)---getFrame().... total time for one process cycle is

so this way the main thread will only wait 9ms in the beginning for the first frame and wont have to wait again anymore

One thing you can do is you can run a separate thread for grabbing the images from the video capture object, and put that captured images in a queue, and from the main thread get a frame from the queue and do your processing.

So while the main thread is processing the image for 25ms, the grabber thread will be able to grab (25/9)=2 frames in the queue ready for the main thread to process and the main thread wont have to wait 9ms for image to be captured.

this will completely remove that 9ms time that was being added to your total time

So right now its like : grab frame(9ms)+ process(25ms)=total time (34ms)

with a separate thread running:

grabber thread:->grab(9ms)--->grab(9ms)------->grab(9ms)----->grab(9ms)->-

main thread: ---> wait(9ms)->getFrame()->--Process------(25ms)---getFrame().... total time for one process cycle is 25ms

so this way the main thread will only wait 9ms in the beginning for the first frame and wont have to wait again anymore

One thing you can do is you can run a separate thread for grabbing the images from the video capture object, and put that captured images in a queue, and from the main thread get a frame from the queue and do your processing.

So while the main thread is processing the image for 25ms, the grabber thread will be able to grab (25/9)=2 frames in the queue ready for the main thread to process and the main thread wont have to wait 9ms for image to be captured.

this will completely remove that 9ms time that was being added to your total time

So right now its like : grab frame(9ms)+ process(25ms)=total time (34ms)

with a separate thread running:

grabber thread:->grab(9ms)--->grab(9ms)------->grab(9ms)----->grab(9ms)->-

main thread: ---> wait(9ms)->getFrame()->--Process------(25ms)---getFrame().... total time for one process cycle is 25ms

so this way the main thread will only wait 9ms in the beginning for the first frame and wont have to wait again anymore

Update: Did some testing in python by myself to see how much it improves the processing in practical, and this is the result

import threading
import time
import Queue
import cv2
frames = Queue.Queue(5)

class ImageGrabber(threading.Thread):
    def __init__(self, ID):
        threading.Thread.__init__(self)
        self.ID=ID
        self.cam=cv2.VideoCapture(ID)
        self.cam.set(3,1280) # just to increase capture time
        self.cam.set(4,1024) # just to increase capture time
        self.runFlag=True

    def run(self):
        print "sub started"
        global frames
        while self.runFlag:
            ret,frame=self.cam.read()
            frames.put(frame)
            time.sleep(0.01)
        self.cam.release()
    def stop(self):
        self.runFlag=False



##------------------------##
## Some random processing ##
## -----------------------##

def Process(img):
    #img1=cv2.Canny(img,100,200)
    img2=cv2.blur(img,(5,5))

##------------------------##
##    without threading   ##
##------------------------##

cam=cv2.VideoCapture(0)
cam.set(3,1280) # just to increase capture time
cam.set(4,1024) # just to increase capture time
ret,img=cam.read()# to ignore the lag of first capture
sumTime=0
i=0
while i<30:
    i=i+1
    start=time.time()
    ret,img=cam.read()
    capTime=time.time()
    Process(img)
    end=time.time()
    sumTime=sumTime+end-start
    print end-start, "capture time=",capTime-start

print "avarage time without multithread=",sumTime/30    
cam.release()


##------------------------##
##     with threading     ##
##------------------------##
grabber = ImageGrabber(0)
grabber.start()
i=0
sumTime=0
while i<30:
    if(not frames.empty()):
        i=i+1
        start=time.time()
        Currframe=frames.get()
        Process(Currframe)
        end=time.time()
        sumTime=sumTime+end-start
        print end-start
print "avarage time with multithread=",sumTime/30    
grabber.stop()
grabber.join()

And this is the output

0.108999967575 capture time= 0.0929999351501
0.0569999217987 capture time= 0.0420000553131
0.0190000534058 capture time= 0.00499987602234
0.055999994278 capture time= 0.0389997959137
0.0340001583099 capture time= 0.0210001468658
0.0820000171661 capture time= 0.069000005722
0.0350000858307 capture time= 0.0199999809265
0.0660002231598 capture time= 0.0530002117157
0.0350000858307 capture time= 0.0209999084473
0.0629999637604 capture time= 0.0479998588562
0.0239999294281 capture time= 0.00499987602234
0.0440001487732 capture time= 0.0310001373291
0.104000091553 capture time= 0.0859999656677
0.0439999103546 capture time= 0.0289998054504
0.0650000572205 capture time= 0.050999879837
0.0299999713898 capture time= 0.0139999389648
0.0780000686646 capture time= 0.0599999427795
0.0369999408722 capture time= 0.0209999084473
0.069000005722 capture time= 0.0520000457764
0.0329999923706 capture time= 0.018000125885
0.0759999752045 capture time= 0.0620000362396
0.039999961853 capture time= 0.0239999294281
0.0599999427795 capture time= 0.0480000972748
0.0230000019073 capture time= 0.00600004196167
0.074000120163 capture time= 0.0590000152588
0.0250000953674 capture time= 0.00499987602234
0.0590000152588 capture time= 0.0439999103546
0.0209999084473 capture time= 0.00600004196167
0.0810000896454 capture time= 0.0640001296997
0.0420000553131 capture time= 0.0239999294281
avarage time without multithread= 0.0528333584468
sub started
0.018000125885
0.0179998874664
0.0199999809265
0.0190000534058
0.0199999809265
0.0179998874664
0.0169999599457
0.0179998874664
0.0160000324249
0.0190000534058
0.0169999599457
0.018000125885
0.018000125885
0.0160000324249
0.0169999599457
0.018000125885
0.0199999809265
0.0190000534058
0.0190000534058
0.0210001468658
0.0200002193451
0.0190000534058
0.0190000534058
0.0199999809265
0.0169999599457
0.0190000534058
0.0160000324249
0.0240001678467
0.0179998874664
0.0190000534058
avarage time with multithread= 0.0185666958491