
Во-первых, я хотел обнаружить линии на одном изображении, прежде чем пробовать это с видеовходом. Ниже я написал код на Python, в результате вместо основных строк на кресте у меня получилось несколько строк. Вы можете видеть входные и выходные изображения. Я хочу избавиться от ненужных линий и обнаружить только две основные линии на пересечении.
# import necessary modules
import numpy as np
import urllib.request
import cv2 as cv
# read the image
with open("input.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(image, (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", image)
cv.waitKey()

Когда я попробовал с видеоисточником, вообще никаких линий не обнаружил. Мобильное приложение, которое я использовал для потоковой передачи видео, — «IP-веб-камера». Вы можете увидеть мой код ниже.
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()
Подробнее здесь: https://stackoverflow.com/questions/785 ... ing-opencv