[][1]

这是我需要对其进行轮廓检测的图像.我需要勾画矩形内部和外部的字符轮廓,但不需要矩形本身.我附上的第二个图像是所需的输出.

enter image description here

这是我迄今为止try 过的方法:

import cv2

# Read the image
image = cv2.imread('fig1.jpg')

# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply histogram equalization to enhance contrast
equalized = cv2.equalizeHist(gray)

# Apply Gaussian blur to the equalized image
blurred = cv2.GaussianBlur(equalized, (5, 5), 0)

# Apply Laplacian operator to detect edges
edges = cv2.Laplacian(blurred, cv2.CV_64F)

# Apply thresholding to create a binary image
_, edges_binary = cv2.threshold(edges, 20, 255, cv2.THRESH_BINARY)

# Convert edges_binary to the appropriate data type (CV_8UC1)
edges_binary = edges_binary.astype(np.uint8)

# Find contours of the edges
contours, _ = cv2.findContours(edges_binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Draw the sides of the rectangle (for visualization)
cv2.drawContours(image, contours, -1, (255, 255, 255), 2)

# Get the bounding box of the rectangle
x, y, w, h = cv2.boundingRect(contours[0])

# Omit the sides of the rectangle from the binary image
edges_binary[y:y+h, x:x+w] = 0
# Find contours in the modified binary image
contours, _ = cv2.findContours(edges_binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Draw contours on the original image
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)

# Display the result
cv2.imshow('Contour Detection', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

enter image description here

我觉得我已经很近了.我试过其他几种方法,但这一种方法最接近预期结果.我上周才学会了OpenCV,有谁能告诉我还有什么可以try 的吗?

推荐答案

您可以使用轮廓区域来避免大的外部矩形.try 下面的代码.最终结果将有字母和符号的边界框.希望这对你有帮助.

import cv2
import numpy as np

#READ IMAGE AND APPLY THRESHOLD
image = cv2.imread("letters.jpg")
grayscale_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, thresholded_image = cv2.threshold(grayscale_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

#FIND THE CONTOURS IN IMAGE
contours, hierarchy = cv2.findContours(thresholded_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contour_image = np.zeros_like(thresholded_image)

#SELECT CONTOURS WHICH ARE SMALL, LESS AREA COMPARED TO LARGE RECTANGLE
selected_ctr =[]
selected_bounding_rect = []
for ctr in contours:
    if cv2.contourArea(ctr) < 1000: #ADJUST THIS ACCORDING TO YOUR NEED.
        selected_ctr.append(ctr) #SELECT CONTOUR
        selected_bounding_rect.append(cv2.boundingRect(ctr)) #GET BOUNDING BOX

#DRAW CONTOURS OF THE FOUND LETTERS AND SYMBOLS
cv2.drawContours(contour_image, selected_ctr, -1, 255, thickness=1)
cv2.imwrite("letters_ctr.jpg",contour_image) # SAVE CONTOUR IMAGE

#DRAW THE SELECTED  RECTANGLES
for rect in selected_bounding_rect:
    image_rect = cv2.rectangle(image, rect,  (0, 255, 0) , 1)
cv2.imwrite("letters_rect.jpg",image_rect) #SAVE RECTANGLE DRAWN IMAGE

输出:

enter image description here

Python相关问答推荐

如何自动抓取以下CSV

如何计算两极打印机中 * 所有列 * 的出现次数?

我从带有langchain的mongoDB中的vector serch获得一个空数组

SQLGory-file包FilField不允许提供自定义文件名,自动将文件保存为未命名

运行终端命令时出现问题:pip start anonymous"

在Polars(Python库)中将二进制转换为具有非UTF-8字符的字符串变量

更改键盘按钮进入'

在Wayland上使用setCellWidget时,try 编辑QTable Widget中的单元格时,PyQt 6崩溃

NP.round解算数据后NP.unique

avxspan与pandas period_range

Stacked bar chart from billrame

如何在图中标记平均点?

不允许访问非IPM文件夹

matplotlib图中的复杂箭头形状

从列表中获取n个元素,其中list [i][0]== value''

ModuleNotFoundError:Python中没有名为google的模块''

pytest、xdist和共享生成的文件依赖项

EST格式的Azure数据库笔记本中的当前时间戳

使用Scikit的ValueError-了解

Python:在cmd中添加参数时的语法