Theory generates initial block compute overlap block similarity adjustment Edge Implementation generate initial block generate overlapping block adjustment edge implementation effect chart
Recently done, you need to use the texture synthesis texture synthesis knowledge, after reading some papers, began to use the MATLAB to achieve, but because I master the general implementation of MATLAB, the realization of the process appeared a variety of problems, and then replaced by C + + rewrite, the effect is better, The summary is as follows. theory
The texture synthesis is mainly derived from a classic paper image quilting for texture synthesis and Transfer, this paper describes the texture synthesis and delivery process, which only describes the texture synthesis section. Part of the Map source Blog Park, if there is infringement deleted. Building the initial block
In this article, you need to use some of the variables: block size tilesize, overlapping size overlapsize, synthetic texture size height,width. First we need to select a piece as the initial block to place in the upper-left corner of the synthetic texture. As shown in the following illustration:
To enhance randomness, you can use a random function to select chunks in the original diagram. calculating the similarity of overlapping blocks
Next we extend the composite texture from the initial block. Extracts overlapping blocks, which are the ones with no slashes in the red box. Traverse in the original texture to get the nearest block. And then cover this piece to the position. There are three kinds of situations that can be used as overlapping blocks: horizontal, vertical, both.
Adjust edges
If the selected block is added directly to the corresponding position, the problem of uneven edges appears. So we need to select a suitable edge to insert, so it seems to be generally harmonious. This is a greedy algorithm, with a cross-section of examples: first in the first row to find the two closest pixels (1,pos), and then on the next line, only need to look for (2,pos-1~pos+1) three pixel differences. Therefore, two edges are formed and the edges are merged to replace the boundary.
Implement
Using the VS 2015+OPENCV 3.1 implementation, the picture is using a single channel. Building the initial block
Generate a random location, where the copy is used in the OpenCV of the light copy of the features, for the ROI operation is the operation of the source image.
void Generateinit (Mat &dest, Mat &source)
{
Mat roi_d (dest, Rect (0, 0, tilesize, tilesize));
Mat roi_s (source, Rect (Random (Defaultsize-tilesize), Random (Defaultsize-tilesize), Tilesize, tilesize));
Roi_s.copyto (roi_d);
}
Building overlapping blockssir. "Overlap only" in the first row and then generate "vertical overlap only" In the first column and then start generating "overlap"
The calculation of similarity is directly calculated by the absolute difference between pixels, where OpenCV is very convenient to provide vector-type operation.
Absdiff (area, area_t, res); Store the result in res
auto Tmp_dis = SUM (res) [0];//sum gets a three-dimensional vector, but there's only one channel.
Adjust edges
There are overlapping calculations with transverse longitudinal:
1. Calculate the vertical direction of the edge path
2. Merge two paths
3. Depth-First search mask "determines which region each pixel belongs to, uses DFS, and starts from the lower-right corner." I have to say, matlab implementation of recursion is very slow "
DFS code, tags tag whether search for
void Solve (set<point> & points, vector<vector<bool>> &mask, int x, int y, vector<vector<bool>> &tags)
{
if (x < 0 | | | y < 0) return
;
Mask[x][y] = true;
Tags[x][y] = true;
int xn[][2] = { -1,0,0,-1};
for (int i = 0; i < 2; ++i)
{
int tmp_x = x + xn[i][0];
int tmp_y = y + xn[i][1];
if (tmp_x < 0 | | | tmp_y < 0)
continue;
Point P (tmp_x, tmp_y);
if (Points.count (P) | | tags[tmp_x][tmp_y])
continue;
Solve (points, mask, tmp_x, tmp_y, tags);
}
implementation of the effect diagram
The effect graph implemented here does not smooth insert the first row and the first column, so it is a bit stiff. In addition, each time you choose the most recent as a candidate block, if you add some randomness, the effect will be more peaceful.
Effect 1:
Effect 2:
Complete code See GitHub address
If there is a mistake, please correct me.