如何通过open cv改进图像中线条的检测? [已关闭]

问题描述 投票:0回答:2

嗨,亲爱的,我正在从事一个通过图像处理查找和跟踪高度和方位太阳角度的项目。相机每 10 分钟就会在阳光下拍摄一条 10 厘米尾巴的阴影照片,作为我的拍摄对象。但是,当白天是第一个早晨或最后一个晚上时,您会收到一个挑战,那就是在纸上找到物体的尾巴阴影。这条线(阴影),当它像这样微弱的颜色或苍白时

enter image description here

它不检测线路。但是,当线条颜色丰富或强烈时,我的算法会找到线条(阴影)。我需要线的顶部和底部的坐标来计算线的长度。

我的代码是:

import time
import numpy as np
import cv2
import serial
from math import atan, sqrt, degrees

# Initialize the serial port
ser = serial.Serial('COM3', baudrate=9600, timeout=1)

def captureImage():
    print('Capturing image')
    videoCaptureObject = cv2.VideoCapture(1)

    result = True
    while(result):
        ret, frame = videoCaptureObject.read()
        cv2.imwrite("Newpicture.jpg", frame)
        result = False
    videoCaptureObject.release()
    return frame

def processImage(im):
    print('Processing image')
    image = im

    # Convert image to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    # Apply Gaussian blur to reduce noise
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    # Convert grayscale image to binary using Otsu thresholding
    # Apply edge detection
    edges = cv2.Canny(blurred, 50, 150)
    
    # Find contours in the edge-detected image
    contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    
    if contours:
        # Get the largest contour (assuming it's the line)
        c = max(contours, key=cv2.contourArea)
        
        # Get the extreme points of the contour (line)
        x1, y1 = c[c[:, :, 0].argmin()][0]
        x2, y2 = c[c[:, :, 0].argmax()][0]
        
        # Calculate the length of the line
        length = sqrt((x2 - x1)**2 + (y2 - y1)**2)

我更改了高斯模糊和边缘值的值,但没有解决我的问题,也没有检测到线条。我使用另一种型号的相机,质量更高,但无法检测线条。最后我在代码中找到了问题。

python opencv image-processing computer-vision coordinates
2个回答
3
投票

您可以尝试使用自适应阈值来处理照明的变化,沿着这些思路:

import cv2 as cv

# Load image as greyscale
img = cv.imread('line.jpg', cv.IMREAD_GRAYSCALE)

# Threshold relative to brightness of local 49x49 area
th = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY_INV,49,10)

# Save result
cv.imwrite('result.png', th)

enter image description here


0
投票

使用

cv::HoughLinesP()
功能正确检测线条:

您可以在这里找到相关教程

根据照片的曝光,您需要微调所使用的阈值。为此,使用自适应阈值可以帮助您。

© www.soinside.com 2019 - 2024. All rights reserved.