Reputation: 3955
Consider the following image:
This is a frame from a video footage of the traffic.
What I want to do is, to crop out only the oncoming traffic, and analyze it. I want a fast and efficient method with which I can extract, say, a polygon, by providing certain coordinates.
I'm working on OpenCV and Python.
EDIT:
One option I see is treating image as Numpy array and using for loop to extract certain elements, but that won't be efficient and I don't know if its a proper thing to do.
Upvotes: 5
Views: 3742
Reputation: 900
I would suggest Extracting your region of interest (Any shape you'd want), by using Contours. Refer to this documentation: Drawing Contours
Your approach should be as follows:
Sample Code:
#Function
def on_mouse(event, x, y, flags,(cPts,overlayImage,resetImage)):
if event==cv.CV_EVENT_LBUTTONUP:
cPts[0].append([x,y])
cv2.circle(overlayImage,(x,y),5,(255),-1)
elif event==cv.CV_EVENT_RBUTTONUP:
cPts[0]=[]
print cPts
overlayImage[:]=resetImage[:]
#Main Program
cvImage=cv2.imread(inputImageFilePath)
grayscaleImage=cv2.cvtColor(cvImage,cv.CV_BGR2GRAY)
overlayImage=np.copy(grayscaleImage)
cv2.namedWindow('preview')
cPts=[[]]
cv2.setMouseCallback('preview',on_mouse,(cPts,overlayImage,grayscaleImage))
opacity=0.4
while True:
displayImage=cv2.addWeighted(overlayImage,opacity,grayscaleImage,1-opacity,0)
cv2.imshow('preview',displayImage)
keyPressed=cv2.waitKey(5)
if keyPressed==27:
break
elif keyPressed==32:
print cPts
cv2.drawContours(overlayImage,np.array(cPts),0,255)
maskImage=np.zeros_like(grayscaleImage)
cv2.drawContours(maskImage,np.array(cPts),0,255,-1)
extractedImage=np.bitwise_and(grayscaleImage,maskImage)
cv2.imshow('extractedImage',extractedImage)
cv2.destroyAllWindows()
Upvotes: 5
Reputation: 19416
Well, I propose you do something like:
cv2.threshold
)cv2.findContours
and more)If you have a stream of video or something like it, you can use something like Motion Detection too.
Some links you might find useful:
Upvotes: 1
Reputation: 1348
I can propose a version of the algorithm:
Hope this will be helpful.
Upvotes: 1