gpt4 book ai didi

python - 如何用OpenCV分割图像的灰色区域

转载 作者:行者123 更新时间:2023-12-02 18:50:24 27 4
gpt4 key购买 nike

我想在模糊后获得图像的灰色部分:

import numpy as np
import cv2


img = cv2.imread('image1.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

img_w_treshold = cv2.threshold(img, 150, 255, cv2.ADAPTIVE_THRESH_MEAN_C)[1]
cv2.imshow('img_w_treshold', img_w_treshold)
cv2.waitKey()

blured_image = cv2.GaussianBlur(img_w_treshold, (99, 99), 0)
cv2.imshow('blured_image', blured_image)
cv2.waitKey()

这是开始图像和模糊图像: enter image description here

enter image description here我想分割这些部分: enter image description here

最佳答案

您正在尝试裁剪每个信息框,对吗?您可以应用具有大结构元素的非常激进的形态链来创建大文本 block 。然后,提取轮廓并将其近似为边界矩形。然后,只需从原始图像中裁剪每个边界矩形即可。这是代码:

import cv2
import numpy as np

# image path
path = "D://opencvImages//"
fileName = "sBREr.jpg"

# Reading an image in default mode:
inputImage = cv2.imread(path + fileName)
# Deep copy for results:
inputCopy = inputImage.copy()

# Convert the image to grayscale:
grayImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)

# Threshold via Otsu:
_, binaryImage = cv2.threshold(grayImage, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

这一步将为您提供一个漂亮的二值图像,请注意目标像素为白色:

现在,应用几个扩张操作。首先,使用一个大的垂直内核,然后使用与第一个大小相同的水平内核。这些是大内核,第一种情况的大小为 1 x 15,最后一种情况的大小为 15 x 1。还设置了几次迭代。对于此输入,我尝试了 4 次迭代。这是按如下方式完成的:

# Set morph operation iterations:
opIterations = 4

# Get the structuring element:
verticalKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 15))
# Perform Dilate (vertical):
dilateImage = cv2.morphologyEx(binaryImage, cv2.MORPH_DILATE, verticalKernel, None, None, opIterations, cv2.BORDER_REFLECT101)

horizontalKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 1))
# Perform Dilate (horizontal):
dilateImage = cv2.morphologyEx(dilateImage, cv2.MORPH_DILATE, horizontalKernel, None, None, opIterations, cv2.BORDER_REFLECT101)

现在,如果您处理这些 Blob ,您的边界矩形可能会太大。这是因为扩张操作有效地增加了 blob 大小。让我们尝试通过应用erode将 Blob 恢复到原始大小,迭代次数相同,但这次使用更传统的内核 - 我使用3 x 3 矩形一:

# Perform Erode:

# Set kernel (structuring element) size:
kernelSize = 3
# Set morph operation iterations:
opIterations = 8

# Get the structuring element:
maxKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernelSize, kernelSize))

# Perform closing:
erodeImage = cv2.morphologyEx(dilateImage, cv2.MORPH_ERODE, maxKernel, None, None, opIterations, cv2.BORDER_REFLECT101)

这是形态链的最终结果:

我们现在有了很好的区 block ,我曾经使用过原始信息。此外,这些 block 很厚且相连。现在让我们检测外部轮廓 - 这里要小心并使用外部模式,因为某些 block 可能有嵌套孔。我们只寻找没有 child 的 parent 轮廓。此外,我还添加了一个最小面积过滤器来过滤掉可能的噪音:

# Get each bounding box
# Find the big contours/blobs on the filtered image:
contours, hierarchy = cv2.findContours(erodeImage, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)


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

# Approximate the contour to a polygon:
contoursPoly = cv2.approxPolyDP(c, 3, True)
# Get the polygon's bounding rectangle:
boundRect = cv2.boundingRect(contoursPoly)

# Get the dimensions of the bounding rect:
rectX = boundRect[0]
rectY = boundRect[1]
rectWidth = boundRect[2]
rectHeight = boundRect[3]

# Compute the rect's area:
rectArea = rectWidth * rectHeight
# Set a min area threshold:
minArea = 100
# Process rectangle only if its above the min threshold:
if rectArea > minArea:
# Draw rectangle:
color = (0, 255, 0)
cv2.rectangle(inputCopy, (int(rectX), int(rectY)), (int(rectX + rectWidth), int(rectY + rectHeight)), color, 3)

cv2.imshow("Bounding Rectangle", inputCopy)
cv2.waitKey(0)

# Crop the current ROI:
croppedImage = inputImage[rectY:rectY + rectHeight, rectX:rectX + rectWidth]

cv2.imshow("croppedImage", croppedImage)
cv2.waitKey(0)

这些是原始图像的边界矩形:

这些是裁剪区域。请注意,这些是单独的图像:

关于python - 如何用OpenCV分割图像的灰色区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66877799/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com