网上matlab生成随机纹理的代码,不太会用,有没有大佬帮忙指导一下,万分感谢!

闫总的女粉丝 2021-03-20 05:27:04
%function Y = imagequilt(X, tilesize, n, overlap, err) %Performs the Efros/Freeman Image quilting algorithm on the input % %Inputs % X: The source image to be used in synthesis % tilesize: the dimensions of each square tile. Should divide size(X) evenly % n: The number of tiles to be placed in the output image, in each dimension % overlap: The amount of overlap to allow between pixels (def: 1/6 tilesize) % err: used when computing list of compatible tiles (def: 0.1) function Y = imagequilt(X, tilesize, n, overlap, err) X = double(X); if( length(size(X)) == 2 ) X = repmat(X, [1 1 3]); elseif( length(size(X)) ~= 3 ) error('Input image must be 2 or 3 dimensional'); end; simple = 1Z if( nargin < 5 ) err = 0.002; end; if( nargin < 4 ) overlap = round(tilesize / 6); end; % if( size(X,1) ~= size(X,2) ) % error('Must be square'); % end; if( overlap >= tilesize ) error('Overlap must be less than tilesize'); end; destsize = n * tilesize - (n-1) * overlap Y = zeros(destsize, destsize, 3); for i=1:n, for j=1:n, startI = (i-1)*tilesize - (i-1) * overlap + 1; startJ = (j-1)*tilesize - (j-1) * overlap + 1; endI = startI + tilesize -1 ; endJ = startJ + tilesize -1; %Determine the distances from each tile to the overlap region %This will eventually be replaced with convolutions distances = zeros( size(X,1)-tilesize, size(X,2)-tilesize ); % K = ones(tilesize, overlap); % y = Y(startI:endI,startJ:endJ,1:3); % for k=1:3, % % end; % M = zeros(tilesize, tilesize); % M(1:overlap,:) = 1; % M(:,1:overlap) = 1; % % y = Y(startI:endI,startJ:endJ,1:3); % a = (y(:,:,1) .* M) + (y(:,:,2) .* M) + (y(:,:,3) .* M); % a2 = sum(sum(sum(a.^2))); % % %a2 = sum(sum(sum(a.^2))); % % b2 = filter2(M, b.^2); % % ab = filter2(a, b); % % distances = sqrt(a2 + (2*ab + b2)); % distances = distances(1:size(X,1)-tilesize, 1:size(X,2)-tilesize); % b2 = filter2(X, zerosones(tilesize, tilesize) % % a2 = sum(sum(Y(startI:endI, startJ:startJ+overlap-1, 1:3).^2)) + ... % sum(sum(Y(startI:startI+overlap-1,startJ+overlap:endJ, 1:3).^2)); % b2 = sum(sum( % useconv = 1; if( useconv == 0 ) %Compute the distances from the template to target for all i,j for a = 1:size(distances,1) v1 = Y(startI:endI, startJ:endJ, 1:3); for b = 1:size(distances,2), v2 = X(a:a+tilesize-1,b:b+tilesize-1, 1:3); distances(a,b) = myssd( double((v1(:) > 0)) .* (v1(:) - v2(:)) ); %distances(a,b) = D; end; end; else %Compute the distances from the source to the left overlap region if( j > 1 ) distances = ssd( X, Y(startI:endI, startJ:startJ+overlap-1, 1:3) ); distances = distances(1:end, 1:end-tilesize+overlap); end; %Compute the distance from the source to top overlap region if( i > 1 ) Z = ssd( X, Y(startI:startI+overlap-1, startJ:endJ, 1:3) ); Z = Z(1:end-tilesize+overlap, 1:end); if( j > 1 ) distances = distances + Z; else distances = Z; end; end; %If both are greater, compute the distance of the overlap if( i > 1 && j > 1 ) Z = ssd( X, Y(startI:startI+overlap-1, startJ:startJ+overlap-1, 1:3) ); Z = Z(1:end-tilesize+overlap, 1:end-tilesize+overlap); distances = distances - Z; end; %distances = distances(1:end-tilesize, 1:end-tilesize); end; %Find the best candidates for the match best = min(distances(:)); candidates = find(distances(:) <= (1+err)*best); idx = candidates(ceil(rand(1)*length(candidates))); [sub(1), sub(2)] = ind2sub(size(distances), idx); fprintf( 'Picked tile (%d, %d) out of %d candidates. Best error=%.4f\n', sub(1), sub(2), length(candidates), best ); %If we do the simple quilting (no cut), just copy image if( simple ) Y(startI:endI, startJ:endJ, 1:3) = X(sub(1):sub(1)+tilesize-1, sub(2):sub(2)+tilesize-1, 1:3); else %Initialize the mask to all ones M = ones(tilesize, tilesize); %We have a left overlap if( j > 1 ) %Compute the SSD in the border region E = ( X(sub(1):sub(1)+tilesize-1, sub(2):sub(2)+overlap-1) - Y(startI:endI, startJ:startJ+overlap-1) ).^2; %Compute the mincut array C = mincut(E, 0); %Compute the mask and write to the destination M(1:end, 1:overlap) = double(C >= 0); %Y(startI:endI, startJ:endJ, :) = filtered_write(Y(startI:endI, startJ:endJ, :), ... % X(sub(1):sub(1)+tilesize-1, sub(2):sub(2)+tilesize-1, :), M); %Y(startI:endI, startJ:endJ, 1:3) = X(sub(1):sub(1)+tilesize-1, sub(2):sub(2)+tilesize-1, 1:3); %Compute the mask and write to the destination % M = zeros(tilesize, tilesize); % M(1:end, 1:overlap) = double(C == 0); % Y(startI:endI, startJ:endJ, :) = filtered_write(Y(startI:endI, startJ:endJ, :), ... % repmat(255, [tilesize, tilesize, 3]), M); end; %We have a top overlap if( i > 1 ) %Compute the SSD in the border region E = ( X(sub(1):sub(1)+overlap-1, sub(2):sub(2)+tilesize-1) - Y(startI:startI+overlap-1, startJ:endJ) ).^2; %Compute the mincut array C = mincut(E, 1); %Compute the mask and write to the destination M(1:overlap, 1:end) = M(1:overlap, 1:end) .* double(C >= 0); %Y(startI:endI, startJ:endJ, :) = filtered_write(Y(startI:endI, startJ:endJ, :), ... % X(sub(1):sub(1)+tilesize-1, sub(2):sub(2)+tilesize-1, :), M); end; if( i == 1 && j == 1 ) Y(startI:endI, startJ:endJ, 1:3) = X(sub(1):sub(1)+tilesize-1, sub(2):sub(2)+tilesize-1, 1:3); else %Write to the destination using the mask Y(startI:endI, startJ:endJ, :) = filtered_write(Y(startI:endI, startJ:endJ, :), ... X(sub(1):sub(1)+tilesize-1, sub(2):sub(2)+tilesize-1, :), M); end; end; image(uint8(Y)); drawnow; end; end; figure; image(uint8(Y)); function y = myssd( x ) y = sum( x.^2 ); function A = filtered_write(A, B, M) for i = 1:3, A(:, :, i) = A(:,:,i) .* (M == 0) + B(:,:,i) .* (M == 1); end
...全文
469 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
源码链接: https://pan.quark.cn/s/0f7c75cb3aad ### MIPI Video Mode 与 Command Mode 的差异 #### 一、引言 MIPI (Mobile Industry Processor Interface) 是一种用于连接移动设备中处理器及其外围设备的标准化接口。MIPI 接口支持多种协议,其中包含 DSI (Display Serial Interface) 和 DCS (Display Control Interface) 等协议。在 MIPI 接口的应用中,主要涉及两种工作模式:Video Mode(视频模式)和 Command Mode(命令模式)。本文旨在系统性地阐述这两种模式的工作机制、特性以及实际应用环境。 #### 二、LCD RAM 概念说明 在进一步探讨 MIPI 的两种模式之前,有必要对文中提及的“LCD RAM”概念进行明确。实际上,“LCD RAM”并非一个通用术语,而是本文作者用来描述 LCD 控制器中用于存储显示数据的内存区域。LCD(Liquid Crystal Display,液晶显示屏)通常配备一个控制 IC(Integrated Circuit,集成电路),该控制 IC 可能内置 RAM 以缓存显示数据。 #### 三、MIPI Video Mode(视频模式) **定义:** - 视频模式是一种类似于传统 RGB 接口的工作模式,它要求主机持续不断地向显示器传输刷新数据。 - 在这种模式下,数据和控制信号以报文的形式通过 MIPI 总线进行传输。 - 显示器本身无需配备帧缓冲器,因为主机会周期性地刷新屏幕。 **特点:** 1. **实时性高:** 主机需要不...
源码下载地址: https://pan.quark.cn/s/a4b39357ea24 在电磁模拟技术中,CST(Computer Simulation Technology)是一种被广泛采纳的软件工具,它主要用于电磁场、微波、天线以及射频系统的设计工作。本资料将详细分析CST软件中离散端口的具体配置方法,这些方法对于提升仿真结果的精确度和专业水准具有决定性作用。离散端口在CST软件中扮演着模拟信号输入或输出的重要角色,它们构成了仿真模型不可或缺的部分。在配置离散端口时,一个核心的原则是保证端口的方向与网格线保持一致,这是因为这样做能够有效降低计算过程中产生的误差,并确保仿真数据的有效性。如果未能遵循这一指导原则,可能会引发未知的计算问题,进而导致仿真结果失去可靠性。 在CST软件中配置离散端口,通常需要借助“Pick Points”这一功能。通过选择“Pick Edge Center”选项,端口将被设定在模型边缘的中心位置上。然而,这种做法并不总是能够确保端口与网格线保持平行。在某些特定情形下,模型的几何构造可能不允许直接选取一个与网格线平行的边作为端口的安装位置。 为了克服这一挑战,可以采用多种不同的策略。如果模型本身已经包含一条与馈电口平行的边,那么可以直接利用这条边来建立端口,此时CST软件会自动调整端口使其与网格线对齐。另一种可选的方法是,当模型不具备现成的平行边时,用户可以手动构建一个几何结构,比如一个立方体,并使其边缘与馈电口平行。通过这种方式,新建立的几何结构的边缘就可以作为端口的位置,从而确保端口与网格线的平行关系。 在实施上述操作时,必须关注端口尺寸的合理性和物理意义的一致性。端口的尺寸应当依据实际天线馈电部分的尺寸进行适当调整,过大的端口或...
代码下载链接: https://pan.quark.cn/s/a4b39357ea24 【使用TensorFlow进行图像识别】 图像识别作为计算机视觉领域的关键任务之一,其核心在于通过算法解析和理解图像所包含的信息。在此资源中,我们将集中探讨如何借助功能强大的深度学习框架TensorFlow来执行手写数字识别。手写数字识别构成了众多实际应用的基础,例如自动支票处理、光学字符识别(OCR)等场景。 TensorFlow是由Google创建的一个开源库,它主要用于数值运算和机器学习,尤其在深度学习方面表现卓越。其核心优势在于可以构建并训练复杂的神经网络架构,并且在多种硬件环境中实现高效执行,涵盖CPU和GPU平台。 在此实践项目中,我们将运用TensorFlow来构建一个卷积神经网络(CNN)模型,这种架构是处理图像数据的理想选择。CNNs通过模仿人脑视觉皮层的运作机制,能够自主地提取图像中的关键特征,进而达成识别目标。在手写数字识别的特定情境下,这些特征可能涉及笔画的几何形态、走向以及相互间的连接模式。 对于CNN的基础结构,我们需要具备相应的认知,其通常由卷积层、池化层、全连接层以及激活函数等部分组成。卷积层借助滤波器(亦称卷积核)对图像进行扫描,以捕捉局部特征;池化层则用于降低数据维度,同时保留核心信息;全连接层将特征向量映射至各类别的概率分布;而激活函数如ReLU则通过引入非线性元素,使模型能够学习更为复杂的模式。 在此案例中,建议采用MNIST数据集,这是一个广泛用于手写数字识别的标准测试集。该数据集包含60,000个训练样本和10,000个测试样本,每个样本均为28x28像素的灰度图像,代表0到9这十个数字中的某一个。为了训练模型,必须首先加载数据,并...

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi GAME,图形处理/多媒体
社区管理员
  • GAME,图形处理/多媒体社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧