Home > Back-end >  Matlab code to generate random texture on the net, not too will use, is there a big help to instruct
Matlab code to generate random texture on the net, not too will use, is there a big help to instruct

Time:03-21

% 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 2 in short
% tilesize: the dimensions of each square tiles. Should divide the size (X) evenly
% n: The number of tiles to be placed in The output image, in The each dimension
% overlap: The amount of overlap to allow between pixels (def: 1/6 tilesize)
Err: % 2 when computing the list of compatible tiles (def: 0.1)

The 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 z=1

If (nargin <5) err=0.002;
end;

If (nargin <4) overlap=round (tilesize/6);
end;

% the if (size (X, 1) ~=size (X, 2))
% error (' Must be square ');
% end;

If (overlap>=tilesize) error (' overlap must be less than tilesize ');
end;

Destsize tilesize -=n * (n - 1) * overlap Y=zeros (destsize destsize, 3);

For I=1: n, for j=1: n, force=(I - 1) * tilesize - (I - 1) * overlap + 1; StartJ=(j - 1) * tilesize - * overlap (j - 1) + 1; EndI=force + 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 (force: endI, startJ: endJ, 1:3).
% for k=1:3,
% % end; % M=zeros (tilesize tilesize);
% M (1: overlap, :)=1;
% M (:, 1: overlap)=1;
% % y=y (force: endI, startJ: endJ, 1:3).
A=% (y (:, :, 1). * M) + (y (:, :, 2). * M) + (y (:, :, 3). * M);
% a2=sum (the sum (the sum (a. ^ 2)));
% % % a2=sum (the sum (the sum (a. ^ 2)));
% %=filter2 b2 (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); %=filter2 b2 (X, zerosones (tilesize tilesize)
% % a2=sum (the sum (Y (force: endI, startJ: startJ + overlap - 1, 1:3). ^ 2)) +...
% the sum (the sum (Y (force: force + overlap - 1, startJ + overlap: endJ, 1:3). ^ 2));
% b2=sum (the sum (
% useconv=1; If (useconv==0) % Compute the distances from the template to target for all the I, j for a=1: size (distances, 1) v1=Y (force: endI, startJ: endJ, 1:3). A for b=1: size (distances, 2), v2=X (a: b, a + tilesize - 1: 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 the if (m> 1) distances=SSD (X, Y (force: endI, startJ: startJ + overlap - 1, 1:3)); Distances=distances (1: end, 1: end - tilesize + overlap); end; % Compute the short from the source to top overlap region the if (I> 1) Z=SSD (X, Y (force: force + overlap - 1, startJ: endJ, 1:3)); Z=Z (1: end - tilesize + overlap, 1: end); If (m> 1) distances=distances + Z; The else distances=Z; end; end; % If both are greater, compute the short of the overlap the If (I> 1 & j> 1) Z=SSD (X, Y (force: force + 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); Independence idx=candidates (ceil (rand (1) * length (candidates))); [sub (1), sub (2)]=ind2sub (size (distances), independence idx); Fprintf (' Picked tile (% d, % d) out of % d candidates. The Best error=%. 4 f \ n ', sub (1), sub (2), length (candidates), Best); % If we do the simple quilting (no cut), just copy the image If (simple) Y (force: endI, startJ: endJ, 1:3)=X (sub (1) : sub (1) + tilesize - 1, sub (2) : sub (2) + tilesize - 1, 1:3). Else % the Initialize the mask to all ones M=ones (tilesize tilesize); % We have left a overlap the if (m> 1) % Compute the SSD in the border region E=(X (sub (1) : sub (1) + tilesize - 1, sub (2) : sub overlap (2) + 1) - Y (force: endI, startJ: startJ + overlap - 1)). ^ 2; C=% Compute the mincut array mincut (E, 0); % Compute the mask and the write to the destination M (1: end, 1: overlap)=double (C>=0); % Y (force: endI startJ: endJ, :)=filtered_write (Y (force: endI, startJ: endJ, :),... % X (sub (1) : sub (1) + tilesize - 1, sub (2) : sub (2) + tilesize - 1, :), M); % Y (force: endI startJ: endJ, 1:3)=X (sub (1) : sub (1) + tilesize - 1, sub (2) : sub (2) + tilesize - 1, 1:3). % % Compute the mask and the write to the destination M=zeros (tilesize tilesize); % M (1: end, 1: overlap)=double (C==0). % Y (force: endI startJ: endJ, :)=filtered_write (Y (force: endI, startJ: endJ, :),... % repmat (255, [tilesize tilesize, 3]), M); end; % We have a top overlap the 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 (force: force + overlap - 1, startJ: endJ))) ^ 2; C=% Compute the mincut array mincut (E, 1); nullnullnullnullnullnullnullnullnullnullnullnull
  • Related