图像油画滤镜艺术效果 python实现（超像素方法）

实现步骤

• 第一步：超像素分割区域
• 第二步：分割区域材质取样替代原像素

from skimage.segmentation import slic,mark_boundaries
from skimage import io
import matplotlib.pyplot as plt
import numpy as np
import cv2 as cv

import time

class Texture(object):
def __init__(self,np_matarr,idx=0,flag=1,center_ptxy=(0,0),segments=None):
self.np_matarr=np_matarr
self.id=idx
self.flag=flag
self.center_ptxy=center_ptxy
self.colordic={'red':(255,0,0),'blue':(0,255,0),'green':(0,0,255),'default':(np_matarr[0,0,:][0],np_matarr[0,0,:][1],np_matarr[0,0,:][2])}
self.segments=segments

def get_reflect(self,srcimg,is_show=False,reflect_color='red'):
ptcr=np.where(self.segments==self.id)
color=None
if isinstance(reflect_color,tuple): #Multitype input color
color=reflect_color
else:
color=self.colordic[reflect_color]
srcimg[ptcr[0],ptcr[1],:]=color
if is_show:
plt.imshow(srcimg)
plt.show()
return srcimg

def add_roi(backimg,logoimg,logox0,logoy0):
backimg=backimg.copy()
logoRs,logoCs,logoChels=logoimg.shape#算出logo的空间参数
for y in range(logoy0,logoy0+logoRs):
for x in range(logox0,logox0+logoCs):
backimg[y,x,:]=logoimg[y-logoy0,x-logox0,:]
return backimg

def roi_cutPoint(srcimg,x0,x1,y0,y1):#切割图像
return srcimg[y0:y1,x0:x1]

def hwSet(pixels_sum,limit=300):
w,h,fr,bc=4,3,0,0
for i in range(1,limit):
w,h=4*i,3*i
fr=bc
bc=12*(i**2)
#print('i={},{},{}'.format(i,fr,bc))
if (pixels_sum<=bc)and(pixels_sum>=fr):
break
return h,w

def make_TextureRGB(mat_id_3,is_show=False):
lens,_=mat_id_3.shape
h,w=hwSet(lens)
s=h*w
re_texture=np.zeros([h,w,3],dtype=np.uint8)
deta=(h*w)-lens
deta_idxs=np.random.randint(0,(lens-1),[deta]) # 对于mat自拓充的index
idxn,idx=0,0
for r in range(h):
for c in range(w):
if idx<lens:
re_texture[r,c,:]=mat_id_3[idx]
idx+=1
else:
re_texture[r,c,:]=mat_id_3[deta_idxs[idxn]]
idxn+=1
if is_show:
plt.imshow(re_texture)
plt.show()
return re_texture

def superPixels(img,is_show=False):
segments = slic(img, n_segments=4000, compactness=3,enforce_connectivity=True,convert2lab=True) #compactness 越大越规则
#450,10 sumpixels=44540
if is_show:
out=mark_boundaries(img,segments)*255 # make boundaries to show
out=add_roi(img,out,0,0)
plt.imshow(out)
plt.show()
return segments,segments.max()

img = io.imread("src_images/00001.jpg") # 读取图片
####

#img=roi_cutPoint(img1,299, 469, 105, 367) # 对roi x0x1y0y1区域进行裁剪
segments,blocks=superPixels(img,is_show=False)
Texture_list=[]
for idx in range(1,blocks+1):
ptcr=np.where(segments==idx)
arr=img[ptcr[0],ptcr[1],:]
texture_mat=make_TextureRGB(arr,is_show=False)
Texture_list.append(Texture(np_matarr=texture_mat,idx=idx,segments=segments))
####

imgv=img
for i in range(len(Texture_list)):
imgv=Texture_list[i].get_reflect(imgv,is_show=False,reflect_color='default')

plt.imshow(imgv)
plt.show()



12-26 1515

05-10 1090

01-28 9470

09-13 3663

08-30 503

06-11 1088

09-12 4157

09-11 4749

12-04 2551

10-16 1790

08-06 327

10-24

10-04

08-17

07-03 687

10-11

02-06 1666

08-06 865

06-06 2418

03-29 374

03-11 200

07-01 6999

03-08 1066

05-09 1504

05-16 685

05-11 2318

05-02 1652

05-12 8262

03-11 155

02-07 395

02-15 1万+

02-06 19万+

04-14

09-06 1万+

01-04 2659

03-19 81万+

06-20 1万+

11-29 1万+

08-14 6462

03-13 14万+

04-05 109

02-23 527

09-29 1350

02-14 7万+

12-01 3431

02-11 12万+

03-31 4410

12-13 610

08-13 418

02-06 6万+

05-12 362

01-30 7万+

08-05 94

10-06 1752

02-11 9万+

10-24 3711

02-13 29万+

11-12 1547

01-14 11万+

04-02 134

02-26 8万+

11-28 297

03-26 194

04-04 9882

02-19 9万+

02-20 18

01-05 27万+

03-29 91

08-31 1913

02-03 18万+

02-15 18万+

02-25 37万+

12-03 359

04-14 58万+

02-03 3万+

11-12 80

03-16 2662

浮雕，雕刻，木刻，灯光，油画滤镜的原理及实现 opencv+vs

©️2019 CSDN 皮肤主题: 程序猿惹谁了 设计师: 上身试试