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

发布于 2025-01-28 09:36:17 字数 363 浏览 2 评论 0原文

我想检测甘蔗棒中的关节,不希望检测到边界线 这是我的原始原始图像,在应用形态梯度后,我得到了这个输出图像。我需要的所需输出是仅检测关节的垂直边缘,所需的输出仅是此图像

因此,如果有人能帮助我,那就太好了!

I want to detect joints in a sugarcane stick and do not want the border lines to be detected
this is my original Original Image, and after applying Morphological Gradient I got this output image. The desired output that I need is to detect only vertical edges that is the joints, the desired output are only the red lines in this image.

So, it would be great if anyone could help me out!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尹雨沫 2025-02-04 09:36:17

您可以尝试增强以下代码以找到垂直边缘,因为您需要

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)

”在此处输入图像说明”

You can try to enhance the following code to find vertical edges as you need

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)

enter image description here

梦在深巷 2025-02-04 09:36:17

Sugarcan Bud检测&切割机制

最终得到了正确的问题,并为我的问题做了回答,它使用猪圈转换来检测芽,并在其周围显示红色圆圈。

它还将序列信息传递给计算机的COM4端口,以便可以在正确的时间操作切割器。

该程序用于识别,检测和切割无人干扰的甘蔗芽,

这是我的研究论文的链接 - 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.")

SUGARCAN BUD DETECTION & CUTTING MECHANISM

Finally I got the right and working answer for my question, It detects the buds using HOG Circle Transform and shows red circle around it.

It also passes a serial info to the COM4 port of the computer so that the cutter can be operated at the right time.

The program is used to Identify, Detect And Cut a sugarcane bud without human interference

Here's a link to my research paper - 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.")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文