簡體   English   中英

檢測圖像邊緣是否幾乎是一條直線

[英]Detecting if an image edge is almost a straight line or not

如何確定圖像的邊緣是否幾乎是一條直線?

說下面的輸入圖像 1 和圖像 2,程序將識別圖像 1 幾乎是一條直線,而圖像 2 不是。

圖 1

在此處輸入圖像描述

圖 2

在此處輸入圖像描述

您可以使用cv2.HoughLinesP()來檢測線條。 查看底部的結果。 在其他圖片上,它沒有返回任何線條。 您需要在圖片之間保持一些一致性,因為您需要調整min_line_length等。

import cv2
import matplotlib.pyplot as plt
import numpy as np
import math
img = cv2.imread('straight.jpg').astype(np.uint8)
m_list = list()
x_list = list()
y_list = list()


edges = cv2.Canny(img,50,150,apertureSize = 3)
lines = cv2.HoughLinesP(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 1, np.pi/360, 10,
                        minLineLength=30, maxLineGap=1)

if lines is not None:
    for line in lines:
        x1, y1, x2, y2 = np.maximum(1, line[0])
        if x1 == x2:
            x2 += 1
        if y1 == y2:
            y2 += 1
        m = (y2 - y1) / (x2 - x1)
        if not math.isinf(m):
            m_list.append(m)
        x_list.append([x1, x2])
        y_list.append([y1, y2])
        cv2.line(img, (x1, y1), (x2, y2), [122, 122, 255], 2)
    print('{} lines identified.'.format(len(lines)))
else:
    print('No lines identified.')
    m_list = 0
    x_list = 0
    y_list = 0
plt.imshow(img)
plt.show()
1 lines identified.

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM