I have a laser pointer that projects red cross shape. My aim is to write a python code that detects those two projected lines from a video source. I use my mobile phone camera as a source and the video is streamed via web IP source.
First, I wanted to detect the lines from a single image before trying it out with video input. I have written python code below, as a result I came out with multiple lines instead of the main lines on the cross. You can see input and output images. I want to get rid of unwanted lines and detect only the main two lines on the cross.
# import necessary modules
import numpy as np
import urllib.request
import cv2 as cv
# read the image
with open("5.jpg", "rb") as image:
f = image.read()
# convert to byte array
bytef = bytearray(f)
# convert to numpy array
image = np.asarray(bytef)
# Convert image to grayscale
gray = cv.imdecode(image, 1)
# Use canny edge detection
edges = cv.Canny(gray, 50, 150, apertureSize=3) # default apertureSize: 3
# Apply HoughLinesP method to
# to directly obtain line end points
lines_list = []
lines = cv.HoughLinesP(
edges, # Input edge image
1, # Distance resolution in pixels
np.pi / 180, # Angle resolution in radians
threshold=100, # Min number of votes for valid line (default: 100)
minLineLength=50, # Min allowed length of line
maxLineGap=10 # Max allowed gap between line for joining them (default: 10)
)
if lines is not None:
# Iterate over points
for points in lines:
# Extracted points nested in the list
x1, y1, x2, y2 = points[0]
# Draw the lines joing the points
# On the original image
cv.line(gray, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Maintain a simples lookup list for points
lines_list.append([(x1, y1), (x2, y2)])
# display image
cv.imshow("Image", gray)
cv.waitKey()
input image
output image
When I tried it with video source, it did not detect any lines at all. Mobile app that I used to stream video is “IP Webcam”. You can see my code below.
import numpy as np
import cv2 as cv
# replace with your own IP provided in ip webcam mobile app "IPv4_address/video"
cap = cv.VideoCapture("http://192.168.1.33:8080/video")
while(True):
_, image = cap.read()
# Resize the image
image = cv.resize(image, (500, 500))
# Convert image to grayscale
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# Use canny edge detection
edges = cv.Canny(gray, 50, 150, apertureSize=3)
# Apply HoughLinesP method to
# to directly obtain line end points
lines_list = []
lines = cv.HoughLinesP(
edges, # Input edge image
1, # Distance resolution in pixels
np.pi / 180, # Angle resolution in radians
threshold=10, # Min number of votes for valid line (default: 100)
minLineLength=5, # Min allowed length of line
maxLineGap=200 # Max allowed gap between line for joining them (default: 10)
)
if lines is not None:
# Iterate over points
for points in lines:
# Extracted points nested in the list
x1, y1, x2, y2 = points[0]
# Draw the lines joing the points
# On the original image
cv.line(gray, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Maintain a simples lookup list for points
lines_list.append([(x1, y1), (x2, y2)])
cv.imshow('Livestream', image)
if cv.waitKey(1) == ord('q'):
break
cap.release()
cv.destroyAllWindows()
burak kirmaci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.