【问题标题】:Concatenate/merge images with opencv python使用opencv python连接/合并图像
【发布时间】:2020-03-13 03:01:43
【问题描述】:

我正在尝试从手写手稿创建基于纹理的图像。在对输入图像(来自 IAM 数据库的文本行的二进制图像)进行一些预处理之后,使用垂直轮廓投影将行分割成单词/字符。分割的单词/字符的大小不同,我想连接/合并它以形成所需的基于纹理的图像。输出图像的大小使得连接不可能。我正在使用带有 python 的 openCV 来做到这一点,我想要一些想法或方法来完成这样的任务。该方法的灵感来自这篇文章:R. K. Hanusiak the article link 在第 219-220 页中的“Writer verification using texture-based features”。

Concatenated images alined with it's center of mass

Text sample on the left, and it's texture-base images on the right

【问题讨论】:

标签: python image opencv image-processing


【解决方案1】:

这是一个可能的解决方案。当然,你必须调整一些参数......

我的示例代码做了什么:

  • 应用threshold 并反转 (bitwise_not) 图像以获得黑色背景和白色字母的二进制图像
  • 应用一个小的dilate 来合并一些小元素并减少检测次数
  • 使用findContours 来...查找轮廓:)
  • 计算每个轮廓的boundingRect 和area,返回检测到书写的矩形(区域可用于过滤不需要的小元素)
  • 准备一个与源图像重叠的带有轮廓和矩形的图像(这部分是调试所必需的)

检测后,代码继续创建您想要的新“纹理图像”:

  • total_width 是所有矩形宽度的总和
  • mean_height 是所有矩形高度的平均值
  • total_lines 是新图像中的行数;由total_width 和mean_height 计算得出,因此生成的图像近似为正方形
  • 在一个循环中,我们将每个矩形从src 图像复制到newImg
  • curr_line 和 curr_width 跟踪粘贴 src 矩形的位置
  • 我使用cv.min() 将每个新矩形融合到newImg 中;这类似于 Photoshop 中的“变暗”混合模式

显示检测的图像:

生成的纹理图像:

代码...

import cv2 as cv
import numpy as np
import math

src = cv.imread("handwriting.jpg")
src_gray = cv.cvtColor(src,cv.COLOR_BGR2GRAY)

# apply threshold
threshold = 230
_, img_thresh = cv.threshold(src_gray, threshold, 255, 0)
img_thresh = cv.bitwise_not(img_thresh)

# apply dilate
dilatation_size = 1
dilatation_type = cv.MORPH_ELLIPSE
element = cv.getStructuringElement(dilatation_type, (2*dilatation_size + 1, 2*dilatation_size+1), (dilatation_size, dilatation_size))
img_dilate = cv.dilate(img_thresh, element)

# find contours
contours = cv.findContours(img_dilate, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)

# calculate rectangles and areas
boundRect = [None]*len(contours[1])
areas = [None]*len(contours[1])
for i, c in enumerate(contours[1]):
    boundRect[i] = cv.boundingRect(c)
    areas[i] = cv.contourArea(c)

# set drawing 
drawing = np.zeros((src.shape[0], src.shape[1], 3), dtype=np.uint8)

# you can use only contours bigger than some area
for i in range(len(contours[1])):
    if areas[i] > 1:
        color = (50,50,0)
        cv.rectangle(drawing, (int(boundRect[i][0]), int(boundRect[i][1])), \
          (int(boundRect[i][0]+boundRect[i][2]), int(boundRect[i][1]+boundRect[i][3])), color, 2)

# set newImg
newImg = np.ones((src.shape[0], src.shape[1], 3), dtype=np.uint8)*255
total_width = 0
mean_height = 0.0
n = len(boundRect)
for r in (boundRect):
    total_width += r[2]
    mean_height += r[3]/n

total_lines = math.ceil(math.sqrt(total_width/mean_height))
max_line_width = math.floor(total_width/total_lines)

# loop through rectangles and perform a kind of copy paste
curr_line = 0
curr_width = 0
for r in (boundRect):
    if curr_width > max_line_width:
        curr_line += 1
        curr_width = 0
    # this is the position in newImg, where to insert source rectangle
    pos = [curr_width, \
           curr_width + r[2], \
           math.floor(curr_line*mean_height), \
           math.floor(curr_line*mean_height) + r[3] ]
    s = src[r[1]:r[1]+r[3], r[0]:r[0]+r[2], :]
    d = newImg[pos[2]:pos[3], pos[0]:pos[1], :]
    newImg[pos[2]:pos[3], pos[0]:pos[1], :] = cv.min(d,s)
    curr_width += r[2]

cv.imwrite('detection.png',cv.subtract(src,drawing))
cv.imshow('blend',cv.subtract(src,drawing))

crop = int(max_line_width*1.1)
cv.imwrite('texture.png',newImg[:crop, :crop, :])
cv.imshow('newImg',newImg[:crop, :crop, :])

cv.waitKey()

【讨论】:

  • 注意:我在新图像的每一行顶部对齐了矩形,而不是您提到的“质心”...但是您可以根据需要调整它跨度>
  • 感谢您的回答,它对我有用,我将其改为使用“质心”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-05
  • 1970-01-01
  • 2022-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多