如何通过在Python中识别单条或多条水平线来分割图像? [英] How to split an image by identifying single or multiple horizontal lines in Python?

查看:42
本文介绍了如何通过在Python中识别单条或多条水平线来分割图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Python根据问题之间的模糊灰线将图像分为多个部分(如下图所示).有办法吗?

I want to split an image into multiple parts using Python based on the faint grey line between questions.(as in image below). Is there a way to do so?

推荐答案

您可以创建水平线的蒙版,然后使用 cv2.reduce 使用 MAX 值将图像缩小.通过检测轮廓,您可以计算缩小蒙版中线条的开始垂直坐标,最后使用该信息裁剪图像.像这样:

You can create a mask of the horizontal lines, then use cv2.reduce to reduce the image to a column using the MAX value. By detecting contours you can calculate the starting vertical coordinate of the lines in the reduced mask and finally, crop the image using this info. Something like this:

# Set image path
imagePath = "D://opencvImages//"
imageName = "zlSGu.jpg"

# Read image:
inputImage = cv2.imread(imagePath + imageName)
# Store a copy for results:
inputCopy = inputImage.copy()

# Convert BGR to grayscale:
grayInput = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)

# Set a lower and upper range for the threshold:
lowerThresh = 230
upperThresh = 235

# Get the lines mask:
mask = cv2.inRange(grayInput, lowerThresh, upperThresh)

这为您提供了线罩:

有点吵,您的图像被压缩了.让我们应用一个最小面积为50的 areaFilter 来滤除这种噪声:

Which is a little bit noisy, your image is compressed. Let's apply an areaFilter with a minimum area of 50 to filter out this noise:

# Set a filter area on the mask:
minArea = 50
mask = areaFilter(minArea, mask)

这是过滤的面罩:

现在,使用 MAX(255)强度值将图像缩小为一列:

Now, reduce the image to a column using the MAX (255) intensity value:

# Reduce matrix to a n row x 1 columns matrix:
reducedImage = cv2.reduce(mask, 1, cv2.REDUCE_MAX)

这是缩小的图像,在这里很难看到,但是只显示了灰线(缩小为一列).现在,让我们检测这些线的起点和终点-实际上这只是一个垂直坐标.我们可以从线条的边界框中计算此坐标:

This is the reduced image, which is a little bit hard to see here, but only the gray lines (reduced to a column) are shown. Now, let's detect the starting and ending points of these lines - which are really just a vertical coordinate. We can calculate this coordinate from the line's bounding box:

# Find the big contours/blobs on the filtered image:
contours, hierarchy = cv2.findContours(mask, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

# Store the lines here:
separatingLines = []

# We need some dimensions of the original image:
imageHeight = inputCopy.shape[0]
imageWidth = inputCopy.shape[1]

# Look for the outer bounding boxes:
for _, c in enumerate(contours):

    # Approximate the contour to a polygon:
    contoursPoly = cv2.approxPolyDP(c, 3, True)

    # Convert the polygon to a bounding rectangle:
    boundRect = cv2.boundingRect(contoursPoly)

    # Get the bounding rect's data:
    [x, y, w, h] = boundRect

    # Start point and end point:
    lineCenter = y + (0.5 * h)
    startPoint = (0,int(lineCenter))
    endPoint = (int(imageWidth), int(lineCenter))

    # Store the end point in list:
    separatingLines.append( endPoint )

    # Draw the line using the start and end points:
    color = (0, 255, 0)
    cv2.line(inputCopy, startPoint, endPoint, color, 2)

    # Show the image:
    cv2.imshow("inputCopy", inputCopy)
    cv2.waitKey(0)

我还将该行的数据存储在 separatingLines 列表中.另外,仅出于显示目的,我在原始输入上绘制了线条.这是已识别的行的图像:

I've additionally stored the line's data in the separatingLines list. Also, just for displaying purposes, I've drawn the lines on the original input. This is the image of the identified lines:

现在,这些行未排序.让我们根据它们的垂直坐标对它们进行 sort 排序.在正确地对行进行排序之后,我们可以在遍历行列表时对每个部分进行 crop .像这样:

Now, these lines are unsorted. Let's sort them based on their vertical coordinate. After the lines are correctly sorted, we can crop each section as we loop through the lines list. Like this:

# Sort the list based on ascending Y values:
separatingLines = sorted(separatingLines, key=lambda x: x[1])

# The past processed vertical coordinate:
pastY = 0

# Crop the sections:
for i in range(len(separatingLines)):

    # Get the current line width and starting y:
    (sectionWidth, sectionHeight) = separatingLines[i]

    # Set the ROI:
    x = 0
    y = pastY
    cropWidth = sectionWidth
    cropHeight = sectionHeight - y

    # Crop the ROI:
    currentCrop = inputImage[y:y + cropHeight, x:x + cropWidth]
    cv2.imshow("Current Crop", currentCrop)
    cv2.waitKey(0)

    # Set the next starting vertical coordinate:
    pastY = sectionHeight

这些是图像的裁剪部分.请注意,这些是单独的图像:

And these are the cropped portions of the image. Note that these are individual images:

这是 areaFilter 函数的定义和实现:

This is the definition and implementation of the areaFilter function:

def areaFilter(minArea, inputImage):
    # Perform an area filter on the binary blobs:
    componentsNumber, labeledImage, componentStats, componentCentroids = \
    cv2.connectedComponentsWithStats(inputImage, connectivity=4)

    # Get the indices/labels of the remaining components based on the area stat
    # (skip the background component at index 0)
    remainingComponentLabels = [i for i in range(1, componentsNumber) if componentStats[i][4] >= minArea]

    # Filter the labeled pixels based on the remaining labels,
    # assign pixel intensity to 255 (uint8) for the remaining pixels
    filteredImage = np.where(np.isin(labeledImage, remainingComponentLabels) == True, 255, 0).astype('uint8')

    return filteredImage

这篇关于如何通过在Python中识别单条或多条水平线来分割图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆