首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从研究论文的图像中提取(裁剪)图区域

问如何从研究论文的图像中提取(裁剪)图区域
EN

Stack Overflow用户
提问于 2019-12-12 17:20:14
回答 1查看 1.2K关注 0票数 2

我想从这张研究论文的图片中提取所有的数字/图表。我怎么能这么做?

​

​

--我只想要数字部分--我有什么办法能把那部分剪出来吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-13 01:40:55

为了只提取图/图,这里有一种使用OpenCV的方法:

  1. Obtain二值图像。加载图像,转换为灰度和Otsu阈值,得到二值image.
  2. Connect文本轮廓。利用文本在段落中结构的观察,可以用水平轮廓展开,将单个单词连接成单一的contour.
  3. Remove非图轮廓。使用方位比和轮廓区域找到轮廓并进行滤波。我们通过填写contour.
  4. Form中的单个边界框有效地删除非图轮廓。迭代剩余的轮廓并确定边框coordinates
  5. Extract ROI。/使用Numpy切片.

提取该图。

下面是每个步骤的可视化:

阈值图像

​

​

水平核扩张

​

​

过滤以删除非图轮廓。

​

​

检测图包围盒

​

​

提取ROI

​

​

注:这种方法假设图像中只有一个图。如果存在多个ROI,则可以删除步骤4以获得多个ROI,并将每个ROI保存为一个单独的映像。我相信这将是一个简单的改变:)

代码

代码语言:javascript
复制
import cv2
import numpy as np

# Load image, grayscale, Otsu's threshold
image = cv2.imread('1.png')
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Dilate with horizontal kernel
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20,10))
dilate = cv2.dilate(thresh, kernel, iterations=2)

# Find contours and remove non-diagram contours
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    area = cv2.contourArea(c)
    if w/h > 2 and area > 10000:
        cv2.drawContours(dilate, [c], -1, (0,0,0), -1)

# Iterate through diagram contours and form single bounding box
boxes = []
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    x, y, w, h = cv2.boundingRect(c)
    boxes.append([x,y, x+w,y+h])

boxes = np.asarray(boxes)
x = np.min(boxes[:,0])
y = np.min(boxes[:,1])
w = np.max(boxes[:,2]) - x
h = np.max(boxes[:,3]) - y

# Extract ROI
cv2.rectangle(image, (x,y), (x + w,y + h), (36,255,12), 3)
ROI = original[y:y+h, x:x+w]

cv2.imshow('image', image)
cv2.imshow('thresh', thresh)
cv2.imshow('dilate', dilate)
cv2.imshow('ROI', ROI)
cv2.waitKey()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59309811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档