【OpenCV实现图像:使用OpenCV生成拼图效果】

文章目录

概要

概要:

拼图效果是一种将图像切割为相邻正方形并重新排列的艺术效果。在生成拼图效果时,可以考虑不同的模式,包括是否考虑间隔和如何处理不能整除的部分。

不考虑间隔,忽略不能整除部分:

相邻正方形之间没有间隔,同时高度不能整除的部分直接被忽略。

示例图中展示了正方形紧密排列,没有任何间隔,不整除的部分被舍弃。

不考虑间隔,对不能整除部分进行空白填充:

相邻正方形之间没有间隔,同时对高度不能整除的部分进行白色填充。

示例图中呈现了正方形之间无缝连接,高度不能整除的部分填充为白色。

考虑间隔,忽略不能整除部分:

相邻正方形之间存在间隙,间隔距离为3像素,同时高度不能整除的部分直接被忽略。

示例图中显示了正方形之间的间隔,不整除的部分被舍弃。

考虑间隔,对不能整除部分进行空白填充:

相邻正方形之间存在间隙,间隔距离为3像素,同时对高度不能整除的部分进行白色填充。

示例图中演示了正方形之间的间隔,高度不能整除的部分填充为白色。

通用配置

为了统一上述代码,我们设置通用配置项如下:

bash 复制代码
config = {
    "cell_num":7,
    "whether_crop_image_height": True,
    "whether_with_gap": True,
    "gap_width":3
}

上述配置中,各项含义如下:

cell_num: 表示每行划分格子个数

whether_crop_image_height: 表示是否对高度不能整除部分进行空白填充

whether_with_gap: 表示相邻正方形之间是否存在间隔

gap_width: 表示相邻正方形间间隔的大小

不考虑间隔代码实现

1). 获取每行格子数目和输入图像信息

bash 复制代码
w_count = config['cell_num']
src_height = img.shape[0]
src_width = img.shape[1]

2).计算每个格子的长度以及结果图像的宽度

bash 复制代码
sub_length = int(src_width / w_count)
new_width = sub_length * w_count

3).根据是否需要对高度进行空白填充,计算结果图像的高度

bash 复制代码
if config['whether_crop_image_height']:
    h_count = int(src_height / sub_length)
else:
    h_count = math.ceil(src_height / sub_length)
new_height = sub_length * h_count

4).对结果图进行赋值

bash 复制代码
img_t = np.zeros(shape=(new_height,new_width,3),dtype=np.uint8) + 255
if config['whether_crop_image_height']:
    img_t = img[:new_height,:new_width,:]
else:
    img_t[:src_height, :new_width, :] = img[:src_height, :new_width, :]

5).画格子

bash 复制代码
 for x_i in range(1, w_count):
    cv2.line(img_t,(x_i*sub_length,0),(x_i*sub_length,new_height-1),color=(205,205,74),thickness=1)
for y_i in range(1, h_count):
    cv2.line(img_t,(0,y_i*sub_length),(new_width-1,y_i*sub_length), color=(205,205,74), thickness=1)

完整代码:

bash 复制代码
import cv2
import numpy as np
import math

# 通用配置
config = {
    "cell_num": 7,
    "whether_crop_image_height": True,
    "whether_with_gap": False,
    "gap_width": 0
}

# 读取图像
img = cv2.imread("img.png")

# 获取每行格子数目和输入图像信息
w_count = config['cell_num']
src_height = img.shape[0]
src_width = img.shape[1]

# 计算每个格子的长度以及结果图像的宽度
sub_length = int(src_width / w_count)
new_width = sub_length * w_count

# 根据是否需要对高度进行空白填充,计算结果图像的高度
if config['whether_crop_image_height']:
    h_count = int(src_height / sub_length)
else:
    h_count = math.ceil(src_height / sub_length)
new_height = sub_length * h_count

# 对结果图进行赋值
img_t = np.zeros(shape=(new_height, new_width, 3), dtype=np.uint8) + 255
if config['whether_crop_image_height']:
    img_t = img[:new_height, :new_width, :]
else:
    img_t[:src_height, :new_width, :] = img[:src_height, :new_width, :]

# 画格子
for x_i in range(1, w_count):
    cv2.line(img_t, (x_i * sub_length, 0), (x_i * sub_length, new_height-1), color=(205, 205, 74), thickness=1)
for y_i in range(1, h_count):
    cv2.line(img_t, (0, y_i * sub_length), (new_width-1, y_i * sub_length), color=(205, 205, 74), thickness=1)

# 展示生成的拼图效果
cv2.imshow('Mosaic Effect', img_t)
cv2.waitKey(0)
cv2.destroyAllWindows()

带填充的结果:

bash 复制代码
import cv2
import numpy as np
import math

# 通用配置
config = {
    "cell_num": 7,
    "whether_crop_image_height": True,
    "whether_with_gap": False,
    "gap_width": 0
}

# 读取图像
img = cv2.imread("img.png")

# 获取每行格子数目和输入图像信息
w_count = config['cell_num']
src_height = img.shape[0]
src_width = img.shape[1]

# 计算每个格子的长度、间隔长度以及结果图像的宽度
sub_length = int(src_width / w_count)
gap_length = int(config["gap_width"])
new_width = sub_length * w_count + gap_length * (w_count - 1)

# 根据是否需要对高度进行空白填充,计算结果图像的高度
if config['whether_crop_image_height']:
    h_count = int((src_height + gap_length) / (sub_length + gap_length))
else:
    h_count = math.ceil((src_height + gap_length) / (sub_length + gap_length))
new_height = sub_length * h_count + gap_length * (h_count - 1)

# 对结果图进行赋值
img_t = np.zeros(shape=(new_height, new_width, 3), dtype=np.uint8) + 255
if config['whether_crop_image_height']:
    for i in range(h_count):
        for j in range(w_count):
            begin_x = sub_length * j
            begin_y = sub_length * i
            src_x = gap_length * j + begin_x
            src_y = gap_length * i + begin_y
            img_t[src_y:src_y + sub_length, src_x:src_x + sub_length, :] = img[begin_y:begin_y + sub_length, begin_x:begin_x + sub_length, :]
else:
    for i in range(h_count):
        for j in range(w_count):
            begin_x = sub_length * j
            begin_y = sub_length * i
            src_x = gap_length * j + begin_x
            src_y = gap_length * i + begin_y
            if i < h_count - 1:
                img_t[src_y:src_y + sub_length, src_x:src_x + sub_length, :] = img[begin_y:begin_y + sub_length, begin_x:begin_x + sub_length, :]
            else:
                diff_height = src_height - sub_length * (h_count - 1)
                img_t[src_y:src_y + diff_height, src_x:src_x + sub_length, :] = img[begin_y:begin_y + diff_height, begin_x:begin_x + sub_length, :]

# 展示生成的拼图效果
cv2.imshow('Mosaic Effect with Fill', img_t)
cv2.waitKey(0)
cv2.destroyAllWindows()
复制代码
不考虑间隔,带填充的情况:
bash 复制代码
img_t = np.zeros(shape=(new_height, new_width, 3), dtype=np.uint8) + 255
if config['whether_crop_image_height']:
    img_t = img[:new_height, :new_width, :]
else:
    img_t[:src_height, :new_width, :] = img[:src_height, :new_width, :]
复制代码
考虑间隔,带填充的情况:
bash 复制代码
img_t = np.zeros(shape=(new_height, new_width, 3), dtype=np.uint8) + 255
if config['whether_crop_image_height']:
    for i in range(h_count):
        for j in range(w_count):
            begin_x = sub_length * j
            begin_y = sub_length * i
            src_x = gap_length * j + begin_x
            src_y = gap_length * i + begin_y
            img_t[src_y:src_y + sub_length, src_x:src_x + sub_length, :] = img[begin_y:begin_y + sub_length, begin_x:begin_x + sub_length, :]
else:
    for i in range(h_count):
        for j in range(w_count):
            begin_x = sub_length * j
            begin_y = sub_length * i
            src_x = gap_length * j + begin_x
            src_y = gap_length * i + begin_y
            if i < h_count - 1:
                img_t[src_y:src_y + sub_length, src_x:src_x + sub_length, :] = img[begin_y:begin_y + sub_length, begin_x:begin_x + sub_length, :]
            else:
                diff_height = src_height - sub_length * (h_count - 1)
                img_t[src_y:src_y + diff_height, src_x:src_x + sub_length, :] = img[begin_y:begin_y + diff_height, begin_x:begin_x + sub_length, :]

在带填充的情况下,会对不能整除的高度部分进行空白填充。带填充的拼图效果会更整齐,而不带填充的情况下可能会有一些截断。

考虑间隔代码实现

1). 获取每行格子数目和输入图像信息

bash 复制代码
w_count = config['cell_num']
src_height = img.shape[0]
src_width = img.shape[1]

2).计算每个格子的长度,间隔长度以及结果图像的宽度

bash 复制代码
sub_length = int(src_width / w_count)
gap_length = int(config["gap_width"])
new_width = sub_length * w_count + gap_length * (w_count -1)

3).根据是否需要对高度进行空白填充,计算结果图像的高度

bash 复制代码
if config['whether_crop_image_height']:
    h_count = int( (src_height + gap_length) / (sub_length+gap_length))
else:
    h_count = math.ceil((src_height + gap_length) / (sub_length+gap_length))
new_height = sub_length * h_count + gap_length * (h_count-1)

4).对结果图进行赋值

bash 复制代码
img_t = np.zeros(shape=(new_height,new_width,3),dtype=np.uint8) + 255
if config['whether_crop_image_height']:
    for i in range(h_count):  
        for j in range(w_count): 
            begin_x = sub_length * j
            begin_y = sub_length * i
            src_x = gap_length * j + begin_x
            src_y = gap_length * i + begin_y
            img_t[src_y:src_y+sub_length,src_x:src_x + sub_length,:] = img[begin_y:begin_y + sub_length,begin_x:begin_x + sub_length,  :]
else:
    for i in range(h_count):  
        for j in range(w_count): 
            begin_x = sub_length * j
            begin_y = sub_length * i
            src_x = gap_length * j + begin_x
            src_y = gap_length * i + begin_y
            if i<h_count-1:
                img_t[src_y:src_y+sub_length,src_x:src_x + sub_length,:] = img[begin_y:begin_y + sub_length,begin_x:begin_x + sub_length,  :]
            else:
                diff_height = src_height - sub_length * (h_count-1)
                img_t[src_y:src_y + diff_height, src_x:src_x + sub_length, :] = img[begin_y:begin_y + diff_height,begin_x:begin_x + sub_length, :]

全部代码

bash 复制代码
import cv2
import numpy as np
import math

# 通用配置
config = {
    "cell_num": 7,
    "whether_crop_image_height": True,
    "whether_with_gap": True,
    "gap_width": 3
}

# 读取图像
img = cv2.imread("img.png")

# 获取每行格子数目和输入图像信息
w_count = config['cell_num']
src_height = img.shape[0]
src_width = img.shape[1]

# 计算每个格子的长度、间隔长度以及结果图像的宽度
sub_length = int(src_width / w_count)
gap_length = int(config["gap_width"])
new_width = sub_length * w_count + gap_length * (w_count - 1)

# 根据是否需要对高度进行空白填充,计算结果图像的高度
if config['whether_crop_image_height']:
    h_count = int((src_height + gap_length) / (sub_length + gap_length))
else:
    h_count = math.ceil((src_height + gap_length) / (sub_length + gap_length))
new_height = sub_length * h_count + gap_length * (h_count - 1)

# 对结果图进行赋值
img_t = np.zeros(shape=(new_height, new_width, 3), dtype=np.uint8) + 255
if config['whether_crop_image_height']:
    for i in range(h_count):
        for j in range(w_count):
            begin_x = sub_length * j
            begin_y = sub_length * i
            src_x = gap_length * j + begin_x
            src_y = gap_length * i + begin_y
            img_t[src_y:src_y + sub_length, src_x:src_x + sub_length, :] = img[begin_y:begin_y + sub_length, begin_x:begin_x + sub_length, :]
else:
    for i in range(h_count):
        for j in range(w_count):
            begin_x = sub_length * j
            begin_y = sub_length * i
            src_x = gap_length * j + begin_x
            src_y = gap_length * i + begin_y
            if i < h_count - 1:
                img_t[src_y:src_y + sub_length, src_x:src_x + sub_length, :] = img[begin_y:begin_y + sub_length, begin_x:begin_x + sub_length, :]
            else:
                diff_height = src_height - sub_length * (h_count - 1)
                img_t[src_y:src_y + diff_height, src_x:src_x + sub_length, :] = img[begin_y:begin_y + diff_height, begin_x:begin_x + sub_length, :]

# 绘制间隔
if config['whether_with_gap']:
    for i in range(h_count):
        for j in range(w_count - 1):
            begin_x = sub_length * (j + 1) + gap_length * j
            begin_y = sub_length * i + gap_length * i
            cv2.line(img_t, (begin_x, begin_y), (begin_x, begin_y + sub_length), color=(205, 205, 74), thickness=1)

# 展示生成的带填充和间隔的拼图效果
cv2.imshow('Mosaic Effect with Fill and Gap', img_t)
cv2.waitKey(0)
cv2.destroyAllWindows()

小结

通过使用OpenCV库和Python编程语言,实现了图像拼图效果的生成。

拼图效果生成: 根据用户的需求,实现了两种不同的拼图效果生成方式。

一种是不考虑间隔,可以选择是否对高度不能整除的部分进行空白填充;另一种是考虑间隔,可以选择相邻正方形之间是否存在间隔,以及间隔的大小,同样可以选择是否对高度不能整除的部分进行空白填充。

通用配置: 引入了通用配置项,用户可以通过修改配置来调整拼图的格子数、是否裁剪高度、是否考虑间隔以及间隔的宽度等参数。

代码优化: 封装了一些功能函数,使代码更加模块化和可读性更强。

相关推荐
乐迪信息16 分钟前
乐迪信息:目标检测算法+AI摄像机:煤矿全场景识别方案
人工智能·物联网·算法·目标检测·目标跟踪·语音识别
学术小白人2 小时前
【EI会议征稿通知】2026年智能感知与自主控制国际学术会议(IPAC 2026)
人工智能·物联网·数据分析·区块链·能源
HyperAI超神经3 小时前
在线教程丨 David Baker 团队开源 RFdiffusion3,实现全原子蛋白质设计的生成式突破
人工智能·深度学习·学习·机器学习·ai·cpu·gpu
Justin3go5 小时前
HUNT0 上线了——尽早发布,尽早发现
前端·后端·程序员
ASKED_20195 小时前
End-To-End之于推荐: Meta GRs & HSTU 生成式推荐革命之作
人工智能
liulanba5 小时前
AI Agent技术完整指南 第一部分:基础理论
数据库·人工智能·oracle
自动化代码美学5 小时前
【AI白皮书】AI应用运行时
人工智能
怕浪猫5 小时前
第一章 JSX 增强特性与函数组件入门
前端·javascript·react.js
小CC吃豆子5 小时前
openGauss :核心定位 + 核心优势 + 适用场景
人工智能
一瞬祈望6 小时前
⭐ 深度学习入门体系(第 7 篇): 什么是损失函数?
人工智能·深度学习·cnn·损失函数