简体   繁体   English

使用 OpenCV 检测垂直曲线

[英]Vertical curved line detection with OpenCV

I want to detect the vertical curved line which is in the middle of the image.我想检测图像中间的垂直曲线。 Do you have any idea how to do it?你知道怎么做吗? I want to separate the left and right side by the line.我想用线把左右两边分开。 What feature or filter should I use?我应该使用什么功能或过滤器? Thank you very much for your ideas how to detect the line.非常感谢您提供如何检测线路的想法。

Here is link to the image:这是图片的链接:

在此处输入图片说明

BTW: I use OpenCV library in Python.顺便说一句:我在 Python 中使用 OpenCV 库。

Sorry, I do not know OpenCV that well.抱歉,我不太了解 OpenCV。 So here is an outline of how to do it.所以这里是如何做到这一点的概述。 I also present code using Imagemagick and also Python Wand code.我还使用 Imagemagick 和 Python Wand 代码呈现代码。 Note, that I save the intermediate images to show the steps.请注意,我保存了中间图像以显示步骤。

Read the image

Blur it some

Threshold is to binary

Do connected components processing to remove all small regions (see contours or blobs in OpenCV)

Use morphology open and close to smooth the edges

Extract an edge outline of the transition between white and black (there are many edge operators: laplacian, gradients, canny, etc)


Input:输入:

在此处输入图片说明

convert img.jpg -blur 0x1 -threshold 9% -type bilevel +write threshold.png \
-define connected-components:mean-color=true \
-define connected-components:area-threshold=10000 \
-connected-components 4 +write ccl.png \
-morphology open disk:5 \
-morphology close disk:5 +write smooth.png \
-morphology edge diamond:1 \
result.png


Threshold:临界点:

在此处输入图片说明

Connected Components:连接组件:

在此处输入图片说明

Smooth:光滑的:

在此处输入图片说明

Result:结果:

在此处输入图片说明

Here is the equivalent code using Python Wand 0.5.6 (currently under development) with Imagemagick 7.0.8.56这是使用 Python Wand 0.5.6(目前正在开发)和 Imagemagick 7.0.8.56 的等效代码

#!/bin/python3.7

from wand.image import Image
from wand.display import display

with Image(filename='curve.jpg') as img:
    img.blur(radius=0, sigma=1)
    img.threshold(threshold=0.09)
    img.connected_components(connectivity=4, area_threshold=1000, mean_color=True)
    img.morphology(method='open', kernel='disk:5')
    img.morphology(method='close', kernel='disk:5')
    img.morphology(method='edge', kernel='diamond:1')
    img.save(filename='0_wand_trim.png')
    display(img)


Here's a simple approach这是一个简单的方法

  • Convert image to grayscale and median blur将图像转换为灰度和中值模糊
  • Threshold to get binary image获取二值图像的阈值
  • Perform morphological transformations to smooth image执行形态变换以平滑图像
  • Perform canny edge detection执行精明的边缘检测

Median blur → Threshold → Opening → Close → Canny中值模糊 → 阈值 → 打开 → 关闭 → Canny

import cv2

image = cv2.imread('1.jpg', 0)
blur = cv2.medianBlur(image, 9)
thresh = cv2.threshold(blur, 25, 255, cv2.THRESH_BINARY)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=3)
close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=3)
canny = cv2.Canny(close, 120, 255, 1)

cv2.imshow('canny', canny)
cv2.waitKey()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM