基于K-means算法的数码迷彩生成——python实现

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:625fdd48-e5ae-4875-8d18-d87f70b09ef2

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:4d999251-6a87-4ce8-b57f-68c5bee10f55

文章目录

前言

提示:本文主要是根据西安工业大学喻钧教授的论文——仿造数码迷彩的设计方法,来复现了一种根据目标背景图像生成仿造数码迷彩的设计方法。首先将背景图案进行颜色空间转换,采用K均值聚类分析方法提取背景主色。由侦测距离确定数码单元的最小尺寸,最后生成数码迷彩斑块。

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:4e151d98-f0ce-42b9-aae3-ec053bbed897

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:e7eaaa5f-1ee2-4238-b9c5-7d5b39dbfbda

一、数码迷彩

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:a971f836-125a-4f4c-85f3-e0fca2cca320

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:038115a5-c7a6-4caa-a319-7076b4df1319

二、K-means算法提取主色

这个看我的上一篇文章:基于python的K-means聚类提取图片主色

三、python算法实现

1、在IDE中导入需要的库

import cv2
import numpy as np
from skimage import io
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

2、K-means聚类提取主色

将输入的图像进行k-means聚类操作,提取三种主要的背景颜色。将提取的三种背景主色分布用不同的标签标记。

#####################图像转换到Lab空间+背景主色提取########################
#利用k-means聚类算法提取背景图的主色
img=cv2.imread("cao3.jpg")
img=cv2.medianBlur(img, 3)
[m0,n0,q0]=img.shape

#规范尺寸方便后续操作
img_rgb=cv2.resize(img,(120,120))
[m,n,q]=img_rgb.shape

k=3
img_lab = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2LAB)
#cv2.imshow('Lab',img_lab)
#io.imsave('img_lab.jpg', img_lab)
img1 = img_lab.reshape((img_lab.shape[0] * img_lab.shape[1], img_lab.shape[2]))

#构建聚类器
kmeans=KMeans(n_clusters=k,max_iter=4000, init='k-means++', n_init=50)
kmeans.fit(img1)#聚类
lab=kmeans.labels_#获取聚类标签
label=lab.reshape([m,n])#将聚类结果转换成图像大小的矩阵

#获取聚类中心(背景主色)
centroids = kmeans.cluster_centers_
centroids = centroids.astype(np.uint8)

label1=np.array(label,dtype=np.float64)
label1=cv2.resize(label1,(n0,m0))
plt.figure(0)
plt.subplot(221),plt.imshow(label1,cmap='gray'),plt.title('label')

3、生成迷彩斑块

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:c7ade60b-a724-430a-a303-7cff6f181f9a

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:1fbb3be0-11ba-433f-81eb-86d1f1791831

s=3
for i in range(0,m,s):
    for j in range(0,n,s):
        sumi=0
        for x in range(i,i+s):
            for y in range(j,j+s):
                sumi=label[x,y]+sumi
        meani=round(sumi/(s*s))
        for x in range(i,i+s):
            for y in range(j,j+s):
                label[x,y]=meani

img_lab2=np.zeros_like(img_lab)
for i in range(m):
    for j in range(n):
        if label[i,j]==0:
            img_lab2[i,j]=centroids[0]
        elif label[i,j]==1:
            img_lab2[i,j]=centroids[1]
        elif label[i,j]==2:
            img_lab2[i,j]=centroids[2]
img_lab2=cv2.resize(img_lab2,(n0,m0))
img_rgb2=cv2.cvtColor(img_lab2, cv2.COLOR_LAB2BGR)

4、数码迷彩的生成

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:bccfa75f-057a-4ba8-a098-5d6ccca39057

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:186dc2f5-9940-4166-a4be-08be835597ef

基于K-means算法的数码迷彩生成——python实现

; 参考文献

喻钧,双晓.仿造数码迷彩的设计方法[J].应用科学学报,2012,30(04):331-334.

Original: https://blog.csdn.net/qq_36896838/article/details/117630824
Author: 就很头铁
Title: 基于K-means算法的数码迷彩生成——python实现

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/560661/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球