MATLAB 图片三角风格化(low poly)

2024-01-08 13:48

本文主要是介绍MATLAB 图片三角风格化(low poly),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

该篇博
客内容已上传至Github
链接:https://github.com/slandarer/low_poly_poisson


三角化效果:

在这里插入图片描述

在这里插入图片描述

步骤
1.图片灰度化后进行sobel卷积检测边缘
oriPic=imread('test.jpg');% use sobel algorithm to detect image edges
if size(oriPic,3)==3grayPic=rgb2gray(oriPic);
elsegrayPic=oriPic;
end
sobelPic=sobelConv2_gray(grayPic);

sobel边缘检测函数:
使用矩阵运算加快运行速度

	function sobelPic=sobelConv2_gray(oriPic)Hx=[-1 0 1;-2 0 2;-1 0 1];Hy=[1 2 1;0 0 0;-1 -2 -1];[rows,cols]=size(oriPic);exPic=uint8(zeros([rows+2,cols+2]));exPic(2:rows+1,2:cols+1)=oriPic;exPic(2:rows+1,1)=oriPic(:,1);exPic(2:rows+1,cols+2)=oriPic(:,cols);exPic(1,2:cols+1)=oriPic(1,:);exPic(rows+2,2:cols+1)=oriPic(rows,:);exPic(1,1)=oriPic(1,1);exPic(rows+2,1)=oriPic(rows,1);exPic(1,cols+2)=oriPic(1,cols);exPic(rows+2,cols+2)=oriPic(rows,cols);Gx=zeros([rows,cols]);Gy=Gx;for ii=1:3for jj=1:3tempPic=double(exPic(ii:rows+ii-1,jj:cols+jj-1));Gx=Gx+tempPic.*Hx(ii,jj);Gy=Gy+tempPic.*Hy(ii,jj);endendsobelPic=uint8(sqrt(Gx.^2+Gy.^2));end

原图:
在这里插入图片描述
卷积结果:
在这里插入图片描述

2.泊松圆盘采样

该算法的详解可以看看这一篇:
http://devmag.org.za/2009/05/03/poisson-disk-sampling/
使用泊松圆盘取样可以产生更加均匀的随机点:
左:随机取点
中:先构建网格,在网格交错点附近添加扰动
右:泊松圆盘取样
在这里插入图片描述
我们对泊松圆盘采样算法进行两次改写,分别应用于边缘处,及其他部分的采点,改写分别为:

  1. 边缘采点所采集点位置只能是sobel边缘检测较亮位置
  2. 其他部分采点密度不同时两点之间最小距离会随之改变,且其他部分采点以边缘点为初始集
edgePic=sobelPic;
edgePic(edgePic<max(max(edgePic)).*0.4)=0;
[edgeX,edgeY]=find(edgePic>0);
edgePntList=[edgeY,edgeX];% set the triangle density
redge=min(size(sobelPic))/80;
rmax=min(size(sobelPic))/20;
rmin=min(size(sobelPic))/40;% use poisson disc sampling to select points
edgePntList=poissonEdge(edgePntList,redge);
pntList=poissonDisk(sobelPic,[rmin,rmax],30,edgePntList);

其中
redge :边缘上两个取样点最近距离
rmax :边缘外 亮度最低时两个采样点最近距离
rmin :边缘外 亮度最高时两个采样点最近距离

当希望使边缘更加细致时,可以将redge后面的除数变大以获得更加密集的采样,例如:

redge=min(size(sobelPic))/120;

边缘采样函数

    function resultSet=poissonEdge(edgeList,R)preSet=edgeList;resultSet=[0 0];resultSet(1,:)=[];times=0;while times<150tempPos=randi([1,size(preSet,1)],1);selectedPnt=preSet(tempPos,:);dis=sqrt(sum((edgeList-selectedPnt).^2,2));candidate=find(dis>=R&dis<=2*R);if length(candidate)>30pntSet=edgeList(candidate(1:30),:);elsepntSet=edgeList(candidate,:);endflag=0;for j=1:size(pntSet,1)pnt=pntSet(j,:);if size(resultSet,1)==0resultSet=[resultSet;pnt];preSet=[preSet;pnt];flag=1;elsedis=sqrt(sum((resultSet-pnt).^2,2));if all(dis>=R)resultSet=[resultSet;pnt];preSet=[preSet;pnt];flag=1;endendendif flag==1preSet(tempPos,:)=[];times=0;elsetimes=times+1;end disp(['edge pnt num:',num2str(size(resultSet,1))]);endend

其他区域采样函数

    function resultSet=poissonDisk(grayPic,R,K,edgePntList)[m,n]=size(grayPic);preSet=edgePntList;resultSet=[edgePntList;1,1;n,m;1,m;n,1];grayPic=double(255-grayPic);cmin=min(min(grayPic));cmax=max(max(grayPic));rMap=grayPic-cmin;rMap=rMap./(cmax-cmin).*(R(2)-R(1))+R(1);times=0;while times<500tempPos=randi([1,size(preSet,1)],1);selectedPnt=preSet(tempPos,:);r=rMap(round(selectedPnt(2)),round(selectedPnt(1)));theta=rand(K,1).*2*pi;radius=rand(K,1).*r+r;x=radius.*cos(theta)+selectedPnt(1);y=radius.*sin(theta)+selectedPnt(2);flag=0;for j=1:Kpnt=[x(j),y(j)];if pnt(1)>=1&&pnt(2)>=1&&pnt(1)<=n&&pnt(2)<=mif size(resultSet,1)==0resultSet=[resultSet;pnt];preSet=[preSet;pnt];flag=1;elsedis=sqrt(sum((resultSet-pnt).^2,2));if all(dis>=r)resultSet=[resultSet;pnt];preSet=[preSet;pnt];flag=1;   endend endendif flag==1preSet(tempPos,:)=[];times=0;elsetimes=times+1;enddisp(['pnt num:',num2str(size(resultSet,1))]);end    end

边缘采样效果:
在这里插入图片描述
其他区域采样效果:
在这里插入图片描述

3.Delaunay三角剖分
% construct the delaunay triangle
DT=delaunay(pntList(:,1),pntList(:,2));

在这里插入图片描述

4.重心法取色

我们要为每个三角形设置一个颜色,最简单的方法是取每个三角形重心处的颜色,即((x1,y1)+(x2,y2)+(x3,y3))/3处的颜色:

% calculate the pixel value at the center of gravity of the triangle
vset1=pntList(DT(:,1),:);
vset2=pntList(DT(:,2),:);
vset3=pntList(DT(:,3),:);
barycenter=round((vset1+vset2+vset3)./3);
tempList=barycenter(:,2)+(barycenter(:,1)-1)*size(oriPic,1);
if size(oriPic,3)==3   Rchannel=oriPic(:,:,1);Gchannel=oriPic(:,:,2);Bchannel=oriPic(:,:,3);colorList(:,:,1)=Rchannel(tempList);colorList(:,:,2)=Gchannel(tempList);colorList(:,:,3)=Bchannel(tempList);
elsecolorList(:,:,1)=oriPic(tempList);colorList(:,:,2)=oriPic(tempList);colorList(:,:,3)=oriPic(tempList);
end
5.图片展示
% show picture
z=zeros([size(pntList,1),1]);
trisurf(DT,pntList(:,1),pntList(:,2),z,'CData',colorList,'EdgeColor','none')
ax=gca;
hold(ax,'on')
set(ax,'XTick',[],'YTick',[],'XColor','none','YColor','none')
axis equal
set(ax,'YDir','reverse','View',[0,90])

在这里插入图片描述

6.完整代码
function lowPoly()
oriPic=imread('test.jpg');% use sobel algorithm to detect image edges
if size(oriPic,3)==3grayPic=rgb2gray(oriPic);
elsegrayPic=oriPic;
end
sobelPic=sobelConv2_gray(grayPic);edgePic=sobelPic;
edgePic(edgePic<max(max(edgePic)).*0.4)=0;
[edgeX,edgeY]=find(edgePic>0);
edgePntList=[edgeY,edgeX];% set the triangle density
redge=min(size(sobelPic))/80;
rmax=min(size(sobelPic))/20;
rmin=min(size(sobelPic))/40;% use poisson disc sampling to select points
edgePntList=poissonEdge(edgePntList,redge);
pntList=poissonDisk(sobelPic,[rmin,rmax],30,edgePntList);
% imshow(sobelPic)
% hold on
% scatter(pntList(:,1),pntList(:,2),3,'filled')% construct the delone triangle
DT=delaunay(pntList(:,1),pntList(:,2));
%triplot(DT,pntList(:,1),pntList(:,2));% calculate the pixel value at the center of gravity of the triangle
vset1=pntList(DT(:,1),:);
vset2=pntList(DT(:,2),:);
vset3=pntList(DT(:,3),:);
barycenter=round((vset1+vset2+vset3)./3);
tempList=barycenter(:,2)+(barycenter(:,1)-1)*size(oriPic,1);
if size(oriPic,3)==3   Rchannel=oriPic(:,:,1);Gchannel=oriPic(:,:,2);Bchannel=oriPic(:,:,3);colorList(:,:,1)=Rchannel(tempList);colorList(:,:,2)=Gchannel(tempList);colorList(:,:,3)=Bchannel(tempList);
elsecolorList(:,:,1)=oriPic(tempList);colorList(:,:,2)=oriPic(tempList);colorList(:,:,3)=oriPic(tempList);
end% show picture
z=zeros([size(pntList,1),1]);
trisurf(DT,pntList(:,1),pntList(:,2),z,'CData',colorList,'EdgeColor','none')
ax=gca;
hold(ax,'on')
set(ax,'XTick',[],'YTick',[],'XColor','none','YColor','none')
axis equal
set(ax,'YDir','reverse','View',[0,90])%% Correlation Functions============================================function resultSet=poissonEdge(edgeList,R)preSet=edgeList;resultSet=[0 0];resultSet(1,:)=[];times=0;while times<150tempPos=randi([1,size(preSet,1)],1);selectedPnt=preSet(tempPos,:);dis=sqrt(sum((edgeList-selectedPnt).^2,2));candidate=find(dis>=R&dis<=2*R);if length(candidate)>30pntSet=edgeList(candidate(1:30),:);elsepntSet=edgeList(candidate,:);endflag=0;for j=1:size(pntSet,1)pnt=pntSet(j,:);if size(resultSet,1)==0resultSet=[resultSet;pnt];preSet=[preSet;pnt];flag=1;elsedis=sqrt(sum((resultSet-pnt).^2,2));if all(dis>=R)resultSet=[resultSet;pnt];preSet=[preSet;pnt];flag=1;endendendif flag==1preSet(tempPos,:)=[];times=0;elsetimes=times+1;end disp(['edge pnt num:',num2str(size(resultSet,1))]);endendfunction resultSet=poissonDisk(grayPic,R,K,edgePntList)[m,n]=size(grayPic);preSet=edgePntList;resultSet=[edgePntList;1,1;n,m;1,m;n,1];grayPic=double(255-grayPic);cmin=min(min(grayPic));cmax=max(max(grayPic));rMap=grayPic-cmin;rMap=rMap./(cmax-cmin).*(R(2)-R(1))+R(1);times=0;while times<500tempPos=randi([1,size(preSet,1)],1);selectedPnt=preSet(tempPos,:);r=rMap(round(selectedPnt(2)),round(selectedPnt(1)));theta=rand(K,1).*2*pi;radius=rand(K,1).*r+r;x=radius.*cos(theta)+selectedPnt(1);y=radius.*sin(theta)+selectedPnt(2);flag=0;for j=1:Kpnt=[x(j),y(j)];if pnt(1)>=1&&pnt(2)>=1&&pnt(1)<=n&&pnt(2)<=mif size(resultSet,1)==0resultSet=[resultSet;pnt];preSet=[preSet;pnt];flag=1;elsedis=sqrt(sum((resultSet-pnt).^2,2));if all(dis>=r)resultSet=[resultSet;pnt];preSet=[preSet;pnt];flag=1;   endend endendif flag==1preSet(tempPos,:)=[];times=0;elsetimes=times+1;enddisp(['pnt num:',num2str(size(resultSet,1))]);end    endfunction sobelPic=sobelConv2_gray(oriPic)Hx=[-1 0 1;-2 0 2;-1 0 1];Hy=[1 2 1;0 0 0;-1 -2 -1];[rows,cols]=size(oriPic);exPic=uint8(zeros([rows+2,cols+2]));exPic(2:rows+1,2:cols+1)=oriPic;exPic(2:rows+1,1)=oriPic(:,1);exPic(2:rows+1,cols+2)=oriPic(:,cols);exPic(1,2:cols+1)=oriPic(1,:);exPic(rows+2,2:cols+1)=oriPic(rows,:);exPic(1,1)=oriPic(1,1);exPic(rows+2,1)=oriPic(rows,1);exPic(1,cols+2)=oriPic(1,cols);exPic(rows+2,cols+2)=oriPic(rows,cols);Gx=zeros([rows,cols]);Gy=Gx;for ii=1:3for jj=1:3tempPic=double(exPic(ii:rows+ii-1,jj:cols+jj-1));Gx=Gx+tempPic.*Hx(ii,jj);Gy=Gy+tempPic.*Hy(ii,jj);endendsobelPic=uint8(sqrt(Gx.^2+Gy.^2));end
end
7.成品展示

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

这篇关于MATLAB 图片三角风格化(low poly)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/583655

相关文章

利用Python脚本实现批量将图片转换为WebP格式

《利用Python脚本实现批量将图片转换为WebP格式》Python语言的简洁语法和库支持使其成为图像处理的理想选择,本文将介绍如何利用Python实现批量将图片转换为WebP格式的脚本,WebP作为... 目录简介1. python在图像处理中的应用2. WebP格式的原理和优势2.1 WebP格式与传统

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪

Python中图片与PDF识别文本(OCR)的全面指南

《Python中图片与PDF识别文本(OCR)的全面指南》在数据爆炸时代,80%的企业数据以非结构化形式存在,其中PDF和图像是最主要的载体,本文将深入探索Python中OCR技术如何将这些数字纸张转... 目录一、OCR技术核心原理二、python图像识别四大工具库1. Pytesseract - 经典O

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

Go语言如何判断两张图片的相似度

《Go语言如何判断两张图片的相似度》这篇文章主要为大家详细介绍了Go语言如何中实现判断两张图片的相似度的两种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 在介绍技术细节前,我们先来看看图片对比在哪些场景下可以用得到:图片去重:自动删除重复图片,为存储空间"瘦身"。想象你是一个

使用Python实现base64字符串与图片互转的详细步骤

《使用Python实现base64字符串与图片互转的详细步骤》要将一个Base64编码的字符串转换为图片文件并保存下来,可以使用Python的base64模块来实现,这一过程包括解码Base64字符串... 目录1. 图片编码为 Base64 字符串2. Base64 字符串解码为图片文件3. 示例使用注意

c/c++的opencv实现图片膨胀

《c/c++的opencv实现图片膨胀》图像膨胀是形态学操作,通过结构元素扩张亮区填充孔洞、连接断开部分、加粗物体,OpenCV的cv::dilate函数实现该操作,本文就来介绍一下opencv图片... 目录什么是图像膨胀?结构元素 (KerChina编程nel)OpenCV 中的 cv::dilate() 函

使用Python实现调用API获取图片存储到本地的方法

《使用Python实现调用API获取图片存储到本地的方法》开发一个自动化工具,用于从JSON数据源中提取图像ID,通过调用指定API获取未经压缩的原始图像文件,并确保下载结果与Postman等工具直接... 目录使用python实现调用API获取图片存储到本地1、项目概述2、核心功能3、环境准备4、代码实现