OpenCV:
Numpy:
Matplotlib:
The main steps involved in this project are:
Initialize the camera capture
Apply image processing techniques to extract the points of interest
Store the detected points in a data structure
Plot the detected points in a graph with their respective directions
process for detect point and plot in graph
First, you will need to import the necessary libraries:
import cv2
import numpy as np
import matplotlib.pyplot as plt
Next, you can use OpenCV to capture the live camera feed:
cap = cv2.VideoCapture(0)
Then, you can create an infinite loop that reads the frames from the camera feed, converts them to grayscale, and detects the points:
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10)
we are using the cv2.goodFeaturesToTrack() function to detect up to 100 corners in the grayscale image. The second parameter specifies the maximum number of corners to detect, the third parameter specifies the quality level of the corners (0.01 in this case), and the fourth parameter specifies the minimum distance between corners.
Once the points are detected, you can store them in a NumPy array:
if corners is not None:
corners = np.int0(corners)
for corner in corners:
x, y = corner.ravel()
cv2.circle(frame, (x, y), 3, (0, 255, 0), -1)
Here, we are converting the corners to integers and looping over them to draw circles on the frame around each corner.
Finally, you can use Matplotlib to display the resulting image and a line through the detected points:
plt.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
if corners is not None and len(corners) > 2:
line = cv2.fitLine(corners, cv2.DIST_L2, 0, 0.01, 0.01)
x0, y0 = line[2:]
x1, y1 = x0 + 100 * line[0], y0 + 100 * line[1]
plt.plot((x0, x1), (y0, y1), 'r-', linewidth=2)
plt.show()
we are first converting the BGR image to RGB using cv2.cvtColor(), then displaying it using plt.imshow(). If there are at least three corners detected, we are using cv2.fitLine() to fit a line through them, and plotting the resulting line on the image using plt.plot().
process for detect point and plot in graph
import cv2
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10)
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10)
if corners is not None:
corners = np.int0(corners)
for corner in corners:
x, y = corner.ravel()
cv2.circle(frame, (x, y), 3, (0, 255, 0), -1)
corners = np.int0(corners)
for corner in corners:
x, y = corner.ravel()
cv2.circle(frame, (x, y), 3, (0, 255, 0), -1)
plt.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
if corners is not None and len(corners) > 2:
line = cv2.fitLine(corners, cv2.DIST_L2, 0, 0.01, 0.01)
x0, y0 = line[2:]
x1, y1 = x0 + 100 * line[0], y0 + 100 * line[1]
plt.plot((x0, x1), (y0, y1), 'r-', linewidth=2)
plt.show()
if corners is not None and len(corners) > 2:
line = cv2.fitLine(corners, cv2.DIST_L2, 0, 0.01, 0.01)
x0, y0 = line[2:]
x1, y1 = x0 + 100 * line[0], y0 + 100 * line[1]
plt.plot((x0, x1), (y0, y1), 'r-', linewidth=2)
plt.show()
No comments:
Post a Comment
Tell us how you like it.