如何仅检测图像中的垂直线?

时间:2019-12-03 11:58:35

标签: python-3.x opencv image-processing

我有这张图片:

original image

从那开始,我只想检测垂直线,而不想要水平线。

我编写了以下代码,它也用水平线给出了此结果:

second image

那是我的代码:

import sys
import math
import cv2 as cv
import numpy as np


def main(argv):
    default_file = 'C:/Users/Rizwan/Desktop/amy_images/image2_43WqE0i.png'
    filename = argv[0] if len(argv) > 0 else default_file
    # Loads an image
    src = cv.imread(cv.samples.findFile(filename), cv.IMREAD_GRAYSCALE)
    img = cv.resize(src, (100, 40))
    src = cv.medianBlur(img, 5)
    # Check if image is loaded fine
    if src is None:
        print('Error opening image!')
        print('Usage: hough_lines.py [image_name -- default ' + default_file + '] \n')
        return -1

    dst = cv.Canny(src, 10, 40, None, 3)

    # Copy edges to the images that will display the results in BGR
    cdst = cv.cvtColor(dst, cv.COLOR_GRAY2BGR)
    cdstP = np.copy(cdst)

    lines = cv.HoughLines(dst, 1, np.pi / 180, 150, None, 0, 0)

    if lines is not None:
        for i in range(0, len(lines)):
            rho = lines[i][0][0]
            theta = lines[i][0][1]
            a = math.cos(theta)
            b = math.sin(theta)
            x0 = a * rho
            y0 = b * rho
            pt1 = (int(x0 + 1000 * (-b)), int(y0 + 1000 * (a)))
            pt2 = (int(x0 - 1000 * (-b)), int(y0 - 1000 * (a)))
            cv.line(cdst, pt1, pt2, (0, 0, 255), 3, cv.LINE_AA)

    linesP = cv.HoughLinesP(dst, 1, np.pi / 180, 50, None, 50, 10)

    if linesP is not None:
        for i in range(0, len(linesP)):
            l = linesP[i][0]
            cv.line(cdstP, (l[0], l[1]), (l[2], l[3]), (0, 0, 255), 3, cv.LINE_AA)

    cv.imshow("Source", src)
    cv.imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst)
    cv.imwrite("Source.png", cdst)
    cv.imwrite("Source1.png", src)
    # cv.imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP)

    cv.waitKey()
    return 0


if __name__ == "__main__":
    main(sys.argv[1:])

还有一点,这使我在第一行中有两行,在第二行中有两行,因为原始图像中只有两条粗线,但是在第二张图像中它却给了我4条垂直线。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

较亮的条纹和背景之间的(强度)差大约为10。最好的想法是,之后使用cv2.adaptiveThreshold和形态学开口以及一些垂直线内核,请参见。 cv2.morphologyEx

以下是一些代码:

import cv2
import numpy as np
from skimage import io      # Only needed for web grabbing images, use cv2.imread for local images

# Read image from web (is already grayscale)
image = io.imread('https://i.stack.imgur.com/FVrCN.png')

# Apply adaptive threshold
image_thr = cv2.adaptiveThreshold(image, 255, cv2.THRESH_BINARY_INV, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, 51, 0)

# Apply morphological opening with vertical line kernel
kernel = np.ones((image.shape[0], 1), dtype=np.uint8) * 255
image_mop = cv2.morphologyEx(image_thr, cv2.MORPH_OPEN, kernel)

# Canny edge detection
image_canny = cv2.Canny(image_mop, 1, 3)

# Get pixel values from the input image (force RGB/BGR on given input) within stripes
image_bgr = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
pixels = image_bgr[image_mop > 0, :]
print(pixels)

# (Visualization) Output
cv2.imshow('image', image)
cv2.imshow('image_thr', image_thr)
cv2.imshow('image_mop', image_mop)
cv2.imshow('image_canny', image_canny)
cv2.waitKey(0)
cv2.destroyAllWindows()

自适应阈值化的结果

After thresholding

打开形态后:

After opening

Canny边缘检测后:

After Canny

您可以使用参数。也许可以改善线条的“形状”。

希望有帮助!