栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python一键生成论文比较图(含选框放大)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python一键生成论文比较图(含选框放大)

文章目录
  • python一键生成论文比较图(含选框放大)
    • 代码
    • 调用
    • 效果

python一键生成论文比较图(含选框放大) 代码

灰度图,RGB图都能用。

from skimage import io
import numpy as np
from PIL import Image
import os
import cv2


def VisualizationEnlarge(imgs, startpoints, patch_size, white_line, box_weights, colors):
    num_img= len(imgs)
    num_enlarge=len(startpoints)
    img0=imgs[0]
    im_shape = img0.shape

    blank_canvas = np.ones([im_shape[0] + im_shape[0] * num_enlarge + white_line*(num_enlarge) +10,
                        im_shape[1] * num_img + white_line*(num_img-1) +10, 3])*255.

    for i in range(num_img):
        if img0.ndim==3:
            blank_canvas[5:im_shape[0] + 5, 5 + i * im_shape[1] + (i) * white_line:5 + (i + 1) * im_shape[1] + (i) * white_line] = imgs[i]
        else:
            blank_canvas[5:im_shape[0]+5, 5+i*im_shape[1]+(i)*white_line:5+(i+1)*im_shape[1]+(i)*white_line] = np.stack((imgs[i],)*3, axis=-1)
        for j in range(num_enlarge):
            cv2.rectangle(blank_canvas,
                      (5+startpoints[j][1]+i*im_shape[1]+i*white_line, 5+startpoints[j][0] ),
                      (5+startpoints[j][1]+i*im_shape[1]+i*white_line+patch_size, 5+startpoints[j][0]+patch_size),
                      colors[j], box_weights)

    for j in range(num_enlarge):
        point = startpoints[j]
        for i in range(num_img):
            patch = imgs[i][5+point[0]:5+point[0]+patch_size, 5+point[1]:5+point[1]+patch_size]
            patch_resize = np.array(Image.fromarray(np.uint8(patch)).resize((im_shape[0], im_shape[1]), resample=3))
            if img0.ndim==3:
                blank_canvas[5+im_shape[0]+white_line + j*(im_shape[0]+white_line):5+im_shape[0]*2+white_line+(j)*(im_shape[0]+white_line),
                            5+i*im_shape[1]+(i)*white_line:5+(i+1)*im_shape[1]+(i)*white_line]=patch_resize
            else:
                blank_canvas[5+im_shape[0]+white_line + j*(im_shape[0]+white_line):5+im_shape[0]*2+white_line+(j)*(im_shape[0]+white_line),
                            5+i*im_shape[1]+(i)*white_line:5+(i+1)*im_shape[1]+(i)*white_line]=np.stack((patch_resize,)*3, axis=-1)
            cv2.rectangle(blank_canvas,
                          (5+i*im_shape[1]+(i)*white_line, 5+im_shape[0]+white_line + j*(im_shape[0]+white_line)),
                          (5+(i+1)*im_shape[1]+(i)*white_line, 5+im_shape[0]*2+white_line+(j)*(im_shape[0]+white_line)),
                          colors[j], box_weights)
    return blank_canvas

调用
folder='Kodak24\'
sub_imgs=['kodim01.png',
          'kodim02.png',
          'kodim03.png',
          'kodim04.png',
          'kodim05.png',
          'kodim06.png']
startpoints=[[372, 281], [135,80], (0,0)]
colors = [(255,0,0), (0,255,0), (0,0,255)]
patch_size=128
imgs=[]
for i in range(len(sub_imgs)):
    imgs.append(io.imread(folder + sub_imgs[i]))
canvas = VisualizationEnlarge(imgs, startpoints, patch_size = 128, white_line= 25, box_weights=8, colors=colors)
io.imsave(folder+'tmp.png', canvas.astype('uint8'))

imgs: 第一行各图
startpoints:选框放大区域的起始点坐标(y(宽度方向)坐标在前)
colors :要与startpoints长度一致,框的颜色
patch_size:框的大小
white_line:图与图之间间隙的宽度
box_weights:框线的粗细

效果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/971399.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号