Python 3.x 检测表上页面的边框,然后;“重新聚焦”;

Python 3.x 检测表上页面的边框,然后;“重新聚焦”;,python-3.x,opencv3.0,Python 3.x,Opencv3.0,我有下面的图片,我想检测页面的边框,然后重新聚焦到“仅”页面。如何开始用opencv3和Python 3编写代码 您可以简单地使用阈值方法将纸张与背景分离。证明: 读取图像并转换为灰色 image = cv2.imread("page.jpg") gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) 检查直方图以选择阈值。更多 使用模糊删除笔记本的细节 blurred_gray_image = cv2.blur(gray_image,(21,2

我有下面的图片,我想检测页面的边框,然后重新聚焦到“仅”页面。如何开始用opencv3和Python 3编写代码


您可以简单地使用阈值方法将纸张与背景分离。证明:

读取图像并转换为灰色

image = cv2.imread("page.jpg")
gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
检查直方图以选择阈值。更多

使用模糊删除笔记本的细节

blurred_gray_image = cv2.blur(gray_image,(21,21))
做。使用我们从直方图中得到的值

_,thresholded_blurry_image = cv2.threshold(blurred_gray_image,165,255,cv2.THRESH_BINARY)
检测(不可分割的闭合形状)

如果存在任何轮廓,则将最大轮廓的轮廓绘制到原始图像的副本。寻找最大轮廓的帖子

output = image.copy()
if len(contours) != 0:
    c = max(contours, key = cv2.contourArea)
    # coordinates of the contour
    x,y,w,h = cv2.boundingRect(c)
    cv2.rectangle(output,(x,y),(x+w,y+h),(0,255,0),2)
显示结果

output = cv2.cvtColor(output,cv2.COLOR_BGR2RGB)
plt.imshow(output)

可以使用cv2.imwrite()函数保存图像。
希望这个答案能满足你的问题。但是请注意,这种方法并不总是有效的,因为我们自己评估直方图,并手动选择阈值。如果您想采用更通用的方法,请尝试
自适应阈值
或借助算法计算直方图值。祝您好运。

非常感谢您给出了详细而清晰的答案!没问题,我很乐意帮忙:)。只需确保理解每一步及其背后的逻辑。保持编码:D
output = image.copy()
if len(contours) != 0:
    c = max(contours, key = cv2.contourArea)
    # coordinates of the contour
    x,y,w,h = cv2.boundingRect(c)
    cv2.rectangle(output,(x,y),(x+w,y+h),(0,255,0),2)
output = cv2.cvtColor(output,cv2.COLOR_BGR2RGB)
plt.imshow(output)