如何在python中使用opencv检测图像中的垂直边缘

nhaq1z21  于 12个月前  发布在  Python
关注(0)|答案(2)|浏览(96)

我想检测甘蔗棒中的关节,不想检测到边界线,这是我原来的Original Image,应用形态梯度后,我得到了这个output image。我需要的输出是只检测垂直边缘,即关节,所需的输出只有this image中的红线。
如果有人能帮我就太好了!

kognpnkq

kognpnkq1#

您可以尝试增强以下代码,以便根据需要查找垂直边

img = cv2.imread("edges.png")
mask = img[:,:,0]

height, width = mask.shape

mask = cv2.threshold(mask, 100, 255, cv2.THRESH_BINARY)[1]
#cv2.imshow("mask", mask)

vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, height//30))
vertical_lines = cv2.erode(mask, vertical_kernel, iterations=1)
vertical_lines = cv2.dilate(vertical_lines, vertical_kernel, iterations=1)

#cv2.imshow("vertical_lines", vertical_lines)

img[vertical_lines > 0, 2] = 255
cv2.imshow("img", img)
cv2.waitKey(0)

zsbz8rwp

zsbz8rwp2#

甘蔗芽检测和切割机构
最后,我得到了正确的和工作的答案,我的问题,它检测芽使用Pig圆变换,并显示红色圆圈周围。
它还将串行信息传递到计算机的COM 4端口,以便切割机可以在正确的时间操作。
该程序是用来识别,检测和切割甘蔗芽没有人为干扰
这是我的研究论文的链接-https://www.irjet.net/archives/V10/i6/IRJET-V10I627.pdf

import cv2
import numpy as np
import serial
import time
import threading

ser = serial.Serial('COM4',bytesize=8, baudrate=9600, timeout=1)  # Replace 'COM3' with the appropriate serial port
time.sleep(3)

cap = cv2.VideoCapture(0)
circle_detected = False

flag = False

def callback():
    global flag
    flag = True

while True:
    # Capture frame-by-frame
    ret, frame = cap.read()

# Convert frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Apply Gaussian blur to remove noise
blur = cv2.GaussianBlur(gray, (5, 5), 0)

# Detect circles using Hough Circle Transform
circles = cv2.HoughCircles(blur, cv2.HOUGH_GRADIENT, 1, 20,
                           param1=60, param2=34, minRadius=10, maxRadius=50)

# Draw circles around detected centers and set flag if circle is detected
if circles is not None:
    circle_detected = True
    circles = np.round(circles[0, :]).astype("int")
    for (x, y, r) in circles:
        cv2.circle(frame, (x, y), r, (0, 0, 255), 2)

if circle_detected:
    if threading.active_count() <= 1:
        timer = threading.Timer(7.0, callback)
        timer.start()
    if circle_detected and flag:
        flag = False
        ser.write(b'00000001')
        print("Sent data to serial port")
    circle_detected = False

# Display the resulting frame
cv2.imshow('Sugarcane Buds Detection', frame)

# Exit program when 'q' is pressed
if cv2.waitKey(1) == ord('q'):
    break

# Release the capture
cap.release()
cv2.destroyAllWindows()

# Flag signal if circle is detected
if circle_detected:
    print("Circle detected!")
else:
    print("No circle detected.")

相关问题