码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 图像分块及拼接(二)python代码


    图像分块及拼接

    • 问题
    • python 代码
    • 效果图

    问题

    图像分块及拼接代码中对图像分块不均匀,本文给出的代码,图像分块大小相同。改进方法:图像分块不均匀时,填充事情shape相同

    python 代码

    import numpy as np
    import cv2 as cv
    import matplotlib.pyplot as plt
    
    
    def get_patch(img,patch_size):
        imgs = []
        shape_list = []
        h, w, n = img.shape
        new_h, new_w = patch_size, patch_size
        col = int(w / patch_size) + 1
        row = int(h / patch_size) + 1
        patch_n = col * row
        top = 0
        for r in range(row):
            insert_zeros_row = np.empty((0, 0, 3))
            foot = top + new_h
            if foot > h:
                foot_gap = foot - h
                foot = h
                insert_zeros_row = np.zeros((foot_gap, new_w,3))
            left = 0
            for c in range(col):
                insert_zeros_col = np.empty((0, 0, 3))
                right = left + new_w
                if right > w:
                    right_gap = right - w
                    right = w
                    if r==row-1:
                        insert_zeros_col = np.zeros((new_h-foot_gap, right_gap, 3))
                    else:
                        insert_zeros_col = np.zeros((new_h, right_gap, 3))
    
                img_patch = img[top:foot, left:right]
                shape_list.append(img_patch.shape)
                if insert_zeros_col.shape[0] > 0:
                    img_patch = np.hstack((img_patch, insert_zeros_col))
                    insert_zeros_col = np.empty((0, 0, 3))
                if insert_zeros_row.shape[0] > 0:
                    img_patch = np.vstack((img_patch, insert_zeros_row))
                left = left + new_w
                imgs.append(img_patch)
            top = top + new_h
        return imgs,shape_list, row, col
    def jointImage(imgs,shape_list,h_n,w_n,):
        for h in range(h_n):
            # 按行拼接
            for w in range(w_n):
                # 按列拼接img
                if w==0:
                   imgs_c=np.array(imgs[h*w_n+w][:shape_list[h*w_n+w][0],:shape_list[h*w_n+w][1]])
                else:
                   img_c=np.array(imgs[h*w_n+w][:shape_list[h*w_n+w][0],:shape_list[h*w_n+w][1]])
                   imgs_c=np.hstack((imgs_c,img_c))
                # print(imgs[h * w_n + w].shape)
            if h==0:
                imgs_h=imgs_c
            else:
                imgs_h=np.vstack((imgs_h,imgs_c))
        return imgs_h
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    完整代码查看我的github [代码](https://github.com/c1h2e3n4y5u6n7/Image-processing-and-deep-learning/blob/master/patch.py)

    效果图

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    我的学习:从本科到研究生的认识与实践经验总结
    快速从0-1完成聊天室开发——环信ChatroomUIKit功能详解
    Maika 与越南童模们受邀请参加中国上海时装周 hanakimi 品牌开幕
    Nmap使用技巧总结
    WebGPU缓冲区更新最佳实践
    lenovo联想笔记本小新 潮7000-14IKBR 2018款(81GA)原装出厂Windows10系统镜像
    基于springboot+vue实现学校田径运动会管理系统【附项目源码+论文说明】
    node---模块
    【网络服务&数据库教程】09 Tomcat 与 Rsync 服务
    第一季:3类和实例初始化【Java面试题】
  • 原文地址:https://blog.csdn.net/weixin_42491648/article/details/133920357
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号