JMoore
JMoore

Reputation: 3

Have Multiple lines connecting blobs to center of screen using webcam OpenCV

I am using OpenCV BlobDetection to detect blobs that appear on my webcam. I am trying to use cv2.arrowedLine function to draw lines from the center of each blob to the middle of the screen.

I have the coordinates of each blob using

pts = [k.pt for k in keypoints]

When I try and use

cv2.arrowedLine(img=frame, pt1=(centre), pt2=(int(pts)), color=(0,0,255), thickness=2)

Nothing shows up.

Is there a solution for this? Thank you very much for any help.

Upvotes: 0

Views: 321

Answers (1)

Victor Sonck
Victor Sonck

Reputation: 406

Maybe some code first

# Standard imports
import cv2
import numpy as np

# Read image
im = cv2.imread("blob.jpg", cv2.IMREAD_GRAYSCALE)

# Set up the detector with default parameters.
detector = cv2.SimpleBlobDetector_create()

# Detect blobs.
keypoints = detector.detect(im)

I assume you got this far. Now when you run the following line

pts = [k.pt for k in keypoints]

pts is a list of coordinate tuples in the form [(x, y), (x, y), ... ]. Opencv can't draw an arrow between a single point center and a list of points. So we'll have to go over it in a for loop as such

pts = [k.pt for k in keypoints]
centre = (200, 320) # This should be changed to the center of your image

for pt in pts:
    pt = tuple(map(int, pt))
    cv2.arrowedLine(img=im, pt1=(centre), pt2=(pt), color=(0, 0, 255), thickness = 2)

The line pt = tuple(map(int, pt)) is used to map all numbers in the list to integers. Just calling int(list) isn't going to work.

If you then look at the result it's as I think you wanted.

# Show keypoints
cv2.imshow("Keypoints", im)
cv2.waitKey(0)

blobs before arrows blobs with arrows

Upvotes: 1

Related Questions