0

我正在编写一个程序来检测无人机图像中的裁剪行,并且我已经成功地使用opencv和python在图像的裁剪部分中检测到裁剪行,但是我现在遇到的问题是如何去检测一个结束作物行。

该图像是一个字段(不能公开共享),我正在尝试检测该字段中的所有行,我可以检测到这些行,但是目前我只是从图像的顶部到底部沿着该行绘制线。相反,我想从作物行的开始到结束画这条线。

即像这样 的东西https://i.stack.imgur.com/WUkg6.jpg(还没有足够的代表来发布图像)但基本上我需要检测行的末尾,以便我可以将线画到最后行的。

关于如何做到这一点的任何想法?

4

1 回答 1

0

使用概率霍夫线变换

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

frame = cv2.imread("crop.jpg")
# It converts the BGR color space of image to HSV color space
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

# Threshold of blue in HSV space
lower_green = np.array([21, 115, 0])
upper_green = np.array([179, 255, 255])

# preparing the mask to overlay
mask = cv2.inRange(hsv, lower_green, upper_green)

mask = cv2.blur(mask, (5,5))
ret, mask = cv2.threshold(mask, 128, 255, cv2.THRESH_BINARY)

edges = cv2.Canny(mask,100,200)

# The black region in the mask has the value of 0,
# so when multiplied with original image removes all non-blue regions
result = cv2.bitwise_and(frame, frame, mask = mask)

cdst = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
linesP = cv2.HoughLinesP(edges, 1, np.pi / 180, 50, None, 100, 30)

if linesP is not None:
    for i in range(0, len(linesP)):
        l = linesP[i][0]
        cv2.line(cdst, (l[0], l[1]), (l[2], l[3]), (0,0,255), 3, cv2.LINE_AA)

cv2.imshow("Detected Lines (in red) - Probabilistic Line Transform", cdst)
cv2.imshow('frame', frame)
cv2.imshow('mask', mask)
cv2.imshow('result', result)

cv2.waitKey(-1)
于 2021-01-20T18:36:17.197 回答