查找图像中目标的位置

时间:2017-03-18 05:03:33

标签: python opencv numpy image-processing

所以我正在研究覆盆子pi 3.我正在传达图像中目标的位置,如果它在它内部存在的话。我目前正在使用python和numpy的组合来做到这一点。代码将在问题下面。我已经读过opencv可以比numpy更快地处理图像,但是目前我的numpy代码运行速度比我试过的代码要快。我想知道如何编写一个opencv代码来运行比我的更快,或者至少我应该研究什么opencv函数。我正在使用的opencv代码将显示在numpy代码之后以及此链接中:http://www.pyimagesearch.com/2015/05/04/target-acquired-finding-targets-in-drone-and-quadcopter-video-streams-using-python-and-opencv/

numpy code:

<div>
  Hello World!
</div>

opencv代码:

 import time
 import picamera
 import numpy as np
 from PIL import Image
 import io    


stream=io.BytesIO()
with picamera.PiCamera() as camera:
camera.capture(stream, format='jpeg')
start_time =time.time()
#construct a numpy array from the stream
#Rewind the stream to the beginning so we can read its content
stream.seek(0)
image= Image.open(stream)
pic_array=np.array(image)

#black=pic_array[::8,::8,0]
print (time.time()-start_time)

r=np.argwhere(pic_array[::8,::8,0]>210)         #collects the location of the ones
ones= len(r)                      #how many ones
if(ones>=1):
    size= np.shape(pic_array)           #gets the size of the array
    array= [0,0]                      #location array
    total=np.sum(r,axis=0)            #sums the location of the ones
    x=total[0]/ones                   #averages the x cordinate location
    y=total[1]/ones                   #averages the y cordinage location
    xboundary=size[0]/2               #finds half the length of the array for x
    yboundary=size[1]/2               #finds half the length of the array for y
    x=x-xboundary               #adjusts the vector for the center of the array for x
    y=-y+yboundary                   #adjusts that vector for the center of the array for y 
    array[0]=x                       #sets x value in vector
    array[1]=y                       #sets y value in vector
print array
print (time.time()-start_time)`

0 个答案:

没有答案