用Python实现图像的边框检测算法?

当您应用光学字符辨认(OCR)或任何数据或对象辨认结果时,起首要做的是预措置。这里的预措置意味着提取我们信息地址的位置。提取位置后,将对该图象实施任何机械算法。

当您必须检测位于任何表/框或行列格局的对象时,会呈现结果。假定图象是多么的,那么你必须检测边框并一一提取它们。此刻该当精确地完成一切图象。作为示例,请拜见以下图象:

用Python完成图象的边框检测算法

用于提取信息的图象的示例

这里,关于该图象,我想要对一切等式遏制光学字符辨认。我想一一提取每个单位格(不是任何空白)来检测这些数字。提取每个单位后,我将对一切的数字遏制瓜分,并应用我的机械进修模型遏制辨认。关于这个算法,我们将应用python措辞应用opencv和numpy,一个一个末尾提取每个单位格:

起首导入一些库:

import cv2

import numpy as np

此刻读取图象,将其转换为灰度,遏制阈值措置并反转图象

# Read the image

img = cv2.imread(img_for_box_extraction_path, 0)

# Thresholding the image

(thresh, img_bin) = cv2.threshold(img, 128, 255,cv2.THRESH_BINARY| cv2.THRESH_OTSU)

# Invert the image

img_bin = 255-img_bin

cv2.imwrite("Image_bin.jpg",img_bin)

所以我们的图象看起来像多么:

用Python完成图象的边框检测算法

 

此刻我们需要检测边框。为此我们将应用外形学操作。为此,我们将根据图象的长度和宽度定义矩形内核(kernel )。我们将定义两个内核。1)内核检测程度线。2)内核检测垂直线。

# Defining a kernel length

kernel_length = np.array(img).shape[1]//80

# A verticle kernel of (1 X kernel_length), which will detect all the verticle lines from the image.

verticle_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, kernel_length))

# A horizontal kernel of (kernel_length X 1), which will help to detect all the horizontal line from the image.

hori_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_length, 1))

# A kernel of (3 X 3) ones.

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))

此刻在定义内核以后,我们将遏制外形学操作来检测垂直和程度线。下面的代码显示包含垂直线的图象。

# Morphological operation to detect vertical lines from an image

img_temp1 = cv2.erode(img_bin, verticle_kernel, iterations=3)

verticle_lines_img = cv2.dilate(img_temp1, verticle_kernel, iterations=3)

cv2.imwrite("verticle_lines.jpg",verticle_lines_img)

# Morphological operation to detect horizontal lines from an image

img_temp2 = cv2.erode(img_bin, hori_kernel, iterations=3)

horizontal_lines_img = cv2.dilate(img_temp2, hori_kernel, iterations=3)

cv2.imwrite("horizontal_lines.jpg",horizontal_lines_img)

用Python完成图象的边框检测算法

包含垂直线的图象

用Python完成图象的边框检测算法

包含程度线的图象

此刻我们将添加这两个图象。这将只要框,并且框中写入的信息将被删除。是以我们可以精确地检测框,并且不会呈现虚假框提取的噪音。

# Weighting parameters, this will decide the quantity of an image to be added to make a new image.

alpha = 0.5

beta = 1.0 - alpha

# This function helps to add two image with specific weight parameter to get a third image as summation of two image.

img_final_bin = cv2.addWeighted(verticle_lines_img, alpha, horizontal_lines_img, beta, 0.0)

img_final_bin = cv2.erode(~img_final_bin, kernel, iterations=2)

(thresh, img_final_bin) = cv2.threshold(img_final_bin, 128,255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

cv2.imwrite("img_final_bin.jpg",img_final_bin)

用Python完成图象的边框检测算法

终究图象仅包含框

此刻我们将对这个图象应用findContours()编制。这将找到一切的边框,我们将从上到下对它们遏制排序。为了对轮廓遏制排序,我们将应用https://www.pyimagesearch.com/2015/04/20/sorting-contours-using-python-and-opencv/供给的函数。我们将采纳自上而下的编制

# Find contours for image, which will detect all the boxes

im2, contours, hierarchy = cv2.findContours(img_final_bin, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# Sort all the contours by top to bottom.

(contours, boundingBoxes) = sort_contours(contours, method="top-to-bottom")

此刻轮回遍历一切轮廓,找到一切框的位置并裁剪具有矩形的零件并将其保管到一个文件夹中

idx = 0

for c in contours:

# Returns the location and width,height for every contour

x, y, w, h = cv2.boundingRect(c)

if (w > 80 and h > 20) and w > 3*h:

idx += 1

new_img = img[y:y+h, x:x+w]

cv2.imwrite(cropped_dir_path+str(idx) + '.png', new_img)

# If the box height is greater then 20, widht is >80, then only save it as a box in "cropped/" folder.

if (w > 80 and h > 20) and w > 3*h:

idx += 1

new_img = img[y:y+h, x:x+w]

cv2.imwrite(cropped_dir_path+str(idx) + '.png', new_img)

此刻它完成了!检讨您的文件夹,您将看到包含每个提取的框的图象。以下所示

用Python完成图象的边框检测算法

 

用Python完成图象的边框检测算法

 

用Python完成图象的边框检测算法

 

所以此刻你可以应用这个图象进一步完成。你可以经过过程增加更改kernel_length参数来获得异常大年夜大年夜的图象中的优胜输入。

寄望:此编制合用于任何处所,用于检测从OMR表就任何Excel表的数据。该编制应用正常的外形学操作,并且它擦除一切外部信息,是以不会呈现用于缺点检测边框的噪声。您可以应用以下编建造为预措置并获得优胜的输入。:)

边框检测的完全Python代码在这里:

import cv2

import numpy as np

def box_extraction(img_for_box_extraction_path, cropped_dir_path):

img = cv2.imread(img_for_box_extraction_path, 0) # Read the image

(thresh, img_bin) = cv2.threshold(img, 128, 255,

cv2.THRESH_BINARY | cv2.THRESH_OTSU) # Thresholding the image

img_bin = 255-img_bin # Invert the image

cv2.imwrite("Image_bin.jpg",img_bin)

# Defining a kernel length

kernel_length = np.array(img).shape[1]//40

# A verticle kernel of (1 X kernel_length), which will detect all the verticle lines from the image.

verticle_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, kernel_length))

# A horizontal kernel of (kernel_length X 1), which will help to detect all the horizontal line from the image.

hori_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_length, 1))

# A kernel of (3 X 3) ones.

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))

# Morphological operation to detect verticle lines from an image

img_temp1 = cv2.erode(img_bin, verticle_kernel, iterations=3)

verticle_lines_img = cv2.dilate(img_temp1, verticle_kernel, iterations=3)

cv2.imwrite("verticle_lines.jpg",verticle_lines_img)

# Morphological operation to detect horizontal lines from an image

img_temp2 = cv2.erode(img_bin, hori_kernel, iterations=3)

horizontal_lines_img = cv2.dilate(img_temp2, hori_kernel, iterations=3)

cv2.imwrite("horizontal_lines.jpg",horizontal_lines_img)

# Weighting parameters, this will decide the quantity of an image to be added to make a new image.

alpha = 0.5

beta = 1.0 - alpha

# This function helps to add two image with specific weight parameter to get a third image as summation of two image.

img_final_bin = cv2.addWeighted(verticle_lines_img, alpha, horizontal_lines_img, beta, 0.0)

img_final_bin = cv2.erode(~img_final_bin, kernel, iterations=2)

(thresh, img_final_bin) = cv2.threshold(img_final_bin, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

# For Debugging

# Enable this line to see verticle and horizontal lines in the image which is used to find boxes

cv2.imwrite("img_final_bin.jpg",img_final_bin)

# Find contours for image, which will detect all the boxes

im2, contours, hierarchy = cv2.findContours(

img_final_bin, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# Sort all the contours by top to bottom.

(contours, boundingBoxes) = sort_contours(contours, method="top-to-bottom")

idx = 0

for c in contours:

# Returns the location and width,height for every contour

x, y, w, h = cv2.boundingRect(c)

# If the box height is greater then 20, widht is >80, then only save it as a box in "cropped/" folder.

if (w > 80 and h > 20) and w > 3*h:

idx += 1

new_img = img[y:y+h, x:x+w]

cv2.imwrite(cropped_dir_path+str(idx) + '.png', new_img)

box_extraction("41.jpg", "./Cropped/")

 

  • 5
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现胸环靶检测,可以使用Python中的计算机视觉库OpenCV。下面是一个简单的示例代码,演示如何使用OpenCV检测靶标: ```python import cv2 # 读取图片 img = cv2.imread("target.jpg") # 将图片转换为灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 对灰度图像进行高斯滤波 gray = cv2.GaussianBlur(gray, (5, 5), 0) # 使用Canny边缘检测算法检测图像的边缘 edges = cv2.Canny(gray, 100, 200) # 在边缘图像中查找轮廓 contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 遍历所有轮廓 for cnt in contours: # 计算轮廓的周长 perimeter = cv2.arcLength(cnt, True) # 使用轮廓周长和面积计算轮廓的近似形状 approx = cv2.approxPolyDP(cnt, 0.02 * perimeter, True) # 如果轮廓近似为4个点,则认为它是一个靶标 if len(approx) == 4: # 绘制靶标的边框 cv2.drawContours(img, [approx], 0, (0, 255, 0), 2) # 显示处理后的图片 cv2.imshow("Target Detection", img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在上述代码中,我们首先读取一张包含靶标的图片,然后将其转换为灰度图像,并使用高斯滤波对其进行平滑处理。接着,我们使用Canny边缘检测算法检测图像的边缘,并在边缘图像中查找轮廓。对于每个轮廓,我们计算其周长和面积,并使用这些值来计算轮廓的近似形状。如果轮廓的近似形状为4个点,则认为它是一个靶标,并在原图像上绘制靶标的边框。最后,我们显示处理后的图片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值