Follow

Detect point using opencv and plot in graph

OpenCV:

OpenCV is an open-source computer vision library that provides a comprehensive set of algorithms for image and video processing. It supports various platforms and programming languages, making it a popular choice for computer vision applications in fields such as robotics, healthcare, and security.

Numpy:

NumPy is a Python library for scientific computing and data analysis. It provides fast and efficient data structures for multi-dimensional arrays, as well as a range of mathematical functions for data manipulation, including linear algebra, statistical analysis, and Fourier analysis.

Matplotlib:

Matplotlib is a data visualization library for Python. It provides a wide range of graphs, charts, and other visualizations for representing data in a clear and effective manner. It can be used for creating static, interactive, and animated visualizations.

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().

No comments:

Post a Comment

Tell us how you like it.