在 table 和 "refocus" 上检测页面边框
Detecting borders of a page on a table and then "refocus"
我有以下图片,我想检测页面的边框,然后重新聚焦以查看 "only" 页面。我怎样才能开始用 opencv3 和 Python 3 编写代码?
您可以简单地使用阈值方法将纸张与背景分开。演示:
读取图像并转换为灰色。
image = cv2.imread("page.jpg")
gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
检查直方图以选择阈值。更多 here
color = ('b','g','r')
fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(1,2,1)
ax.imshow(image)
ax1 = fig.add_subplot(1,2,2)
for i,col in enumerate(color):
histogram = cv2.calcHist([image],[i],None,[256],[0,256])
ax1.plot(histogram,color = col)
ax1.set_xlim([0,256])
使用模糊去除笔记本的细节。
blurred_gray_image = cv2.blur(gray_image,(21,21))
做thresholding。使用我们从直方图中得到的值。
_,thresholded_blurry_image = cv2.threshold(blurred_gray_image,165,255,cv2.THRESH_BINARY)
检测 contours(未分割的闭合形状)。
contours, hierarchy = cv2.findContours(thresholded_blurry_image,
cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
如果有轮廓,则将最大轮廓的轮廓绘制到原始图像的副本上。 post 用于查找最大轮廓。
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() 函数来保存图像。
希望这个答案能满足您的问题。但请注意,这种方法并不总是有效,因为我们自己评估直方图并手动选择阈值。如果您想采用更通用的方法,请尝试 adaptive thresholding
或借助算法评估直方图值。祝你好运。
我有以下图片,我想检测页面的边框,然后重新聚焦以查看 "only" 页面。我怎样才能开始用 opencv3 和 Python 3 编写代码?
您可以简单地使用阈值方法将纸张与背景分开。演示:
读取图像并转换为灰色。
image = cv2.imread("page.jpg")
gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
检查直方图以选择阈值。更多 here
color = ('b','g','r')
fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(1,2,1)
ax.imshow(image)
ax1 = fig.add_subplot(1,2,2)
for i,col in enumerate(color):
histogram = cv2.calcHist([image],[i],None,[256],[0,256])
ax1.plot(histogram,color = col)
ax1.set_xlim([0,256])
使用模糊去除笔记本的细节。
blurred_gray_image = cv2.blur(gray_image,(21,21))
做thresholding。使用我们从直方图中得到的值。
_,thresholded_blurry_image = cv2.threshold(blurred_gray_image,165,255,cv2.THRESH_BINARY)
检测 contours(未分割的闭合形状)。
contours, hierarchy = cv2.findContours(thresholded_blurry_image,
cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
如果有轮廓,则将最大轮廓的轮廓绘制到原始图像的副本上。
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() 函数来保存图像。
希望这个答案能满足您的问题。但请注意,这种方法并不总是有效,因为我们自己评估直方图并手动选择阈值。如果您想采用更通用的方法,请尝试 adaptive thresholding
或借助算法评估直方图值。祝你好运。