opencv python3 找图片色块_python-3.x-如何使用opencv python在各种彩色背景中查找文档边缘? [各种背景下的文档扫描]...

以下代码可以帮助您检测/细分图像中的页面…

import cv2

import matplotlib.pyplot as plt

import numpy as np

image = cv2.imread('test_p.jpg')

image = cv2.imread('test_p.jpg')

print(image.shape)

ori = image.copy()

image = cv2.resize(image, (image.shape[1]//10,image.shape[0]//10))

调整图像大小以使操作更快,以便我们可以实时工作.

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

gray = cv2.GaussianBlur(gray, (11,11), 0)

edged = cv2.Canny(gray, 75, 200)

print("STEP 1: Edge Detection")

plt.imshow(edged)

plt.show()

cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

cnts = sorted(cnts[1], key = cv2.contourArea, reverse = True)[:5]

在这里,我们将仅考虑基于面积的排序列表中的前5个轮廓

高斯模糊的大小在这里是位敏感的,因此请根据图像大小选择相应的大小.

经过上述操作,图像可能看起来像..

for c in cnts:

### Approximating the contour

#Calculates a contour perimeter or a curve length

peri = cv2.arcLength(c, True)

approx = cv2.approxPolyDP(c, 0.01 * peri, True)

# if our approximated contour has four points, then we

# can assume that we have found our screen

screenCnt = approx

if len(approx) == 4:

screenCnt = approx

break

# show the contour (outline)

print("STEP 2: Finding Boundary")

cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)

image_e = cv2.resize(image,(image.shape[1],image.shape[0]))

cv2.imwrite('image_edge.jpg',image_e)

plt.imshow(image_e)

plt.show()

最终图像可能看起来像…

在获得最终图像之后,其他事情可能会处理.

我想这个答案会有所帮助…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值