Preface
Pixel wind first appeared in 8bit video games. Due to the size of computer memory and the single display color, only a small number of pixels can be used to present content, but many classic pixel games have been achieved. With the improvement of memory capacity and screen resolution, the limitation of memory and display media is no longer a problem, and the pixel style has gradually evolved into a unique creative style.
The general drawing process of pixel drawing includes hooking, coloring, etc., and drawing pixel by pixel requires a lot of time. Some popular art methods, such as line drawing and painting, have gradually appeared automated or semi-automatic generation methods. This article will implement the SLIC
[1] algorithm from scratch and implement a tool for generating pixel art.
What is the SLIC algorithm
The reason why pixel painting is not easy to draw is because direct down-sampling cannot accurately capture key pixels, and it easily leads to loss of edge information, and the generated pixel painting is often unsatisfactory. Manual hooking and coloring are all for selecting appropriate pixels. Therefore, our problem becomes how to select the appropriate pixels for coloring.
First, introduce a concept-super pixel. Super pixel is an image segmentation technology proposed and developed by Xiaofeng Ren in 2003. It refers to an irregular pixel block with certain visual significance composed of adjacent pixels with similar texture, color, brightness and other characteristics [1].
By dividing the picture into super pixels, similar pixel clusters can be obtained. Similar pixels are filled with the same color, and the resulting pixel picture will be more reasonable.
The methods of super pixel segmentation include contour extraction, clustering, and gradient ascent. SLIC
super pixel segmentation algorithm (simple linear iterative clustering, simple linear iterative clustering
) proposed by the paper [1]
is one of them. It is based on the K-means
clustering algorithm, clustering according to the color and distance characteristics of the pixels to achieve good segmentation results, and several SLIC
super pixel segmentation algorithm, 060d570f3b2d9c has the advantages of simplicity, flexibility, good effect, and fast processing speed.
How to implement the SLIC algorithm
The basic process of SLIC
Image preprocessing.
The image
RGB
color space to theCIE-Lab
color space. TheLab
color space is more in line with human visual perception of colors. The distance in this space can reflect the color difference that people feel, and the related calculations are more accurate.Lab
color space also has three channels, namelyl
,a
,b
, whereinl
representative luminance, values ranging[0,100]
,a
represents from green to red component values ranging[-128,127]
,b
represents the blue to yellow component, The value range is[-128,127]
.RGB
is no direct conversion formula between 060d570f3b3039 andLAB
RGB
toXYZ
color space and then toLAB
. For the code, see the complete code at the end of the article.Initialize the cluster centers.
Determine the number of super pixels according to the parameters, that is, how many regions need to be divided into. Assuming that the picture has
N
pixels, it is expected to be divided intoK
super pixels, each super pixel size isN/K
, the distance between adjacent centers isS=Sqr(N/K)
, andK
cluster coordinates are obtained.Optimize the initial cluster centers.
3*3
neighborhood of the cluster center, the pixel with the smallest gradient is selected as the new cluster center.Regarding the image as a two-dimensional discrete function, the gradient is the derivation of this function. When the value of adjacent pixels changes, there will be a gradient, and the gradient of the pixel on the edge is the largest. Moving the cluster center to the place with the smallest gradient can prevent it from falling onto the edge contour and affect the clustering effect.
The gradient calculation of the discrete gradient is not deduced in detail here. Since it contains a number of squares and roots, the calculation is relatively large, and it is generally simplified to the operation of approximating the square and the square root with the absolute value. The simplified formula for the gradient of the pixel with the calculated coordinate of
(i,j)
Among them,
(i+1,j)
and(i,j+1)
are the coordinates of the point on the right side of the pixel and the point below the pixel.l(a,b)
is the luminance channel valuel
(a,b)
coordinate.Calculate the distance between the pixel point and the cluster center.
Calculate the distance between the pixel point and each cluster center in the neighborhood
2S*2S
in the area of the cluster center distance S.Here the distance used is Euclidean distance, total distance
D
thedc
color distanceds
spatial distance between the two part composition. The formula is as follows:If directly
l
,a
,b
,x
,y
spliced into a vector calculating the distance, when the change in the size of the super-pixel,x
,y
value may be taken to be very large, if such a map1000*1000
, spatial distance can reach1000*Sqr(2)
, And the maximum color distance is only10*Sqr(2)
. As a result, the weight of theds
Therefore, it needs to be normalized, divided by the maximum value, which is the initial width of the super pixel,
S
, and map the value to[0,1]
.And it will give a color space distance to a fixed value
m
adjusted affecting the right color and the spatial distance from the weight,m
the range[1,40]
.The distance formula becomes
When
m
is larger, the value of the color space divided bym
is smaller, that is, the larger the weight of the spatial distance, the generated pixels will be more regular in shape. Whenm
smaller, the color distance weight is larger, and the super pixel will be more at the edge. It is compact, and the shape and size are relatively irregular.Pixel classification.
Mark the category of each pixel as the category with the smallest distance from the cluster center.
Recalculate the cluster centers.
Calculate the average vector value of all pixels belonging to the same cluster, and get the cluster center again.
Iterate the process of
4~6
Until the distance between the old cluster center and the new cluster center is less than a certain threshold or reaches a certain number of iterations, generally speaking, when the number of iterations reaches
10
, the algorithm can reach convergence.Cluster optimization.
At the end of the iteration, there may be isolated pixels that do not belong to the same connected domain as the cluster center, and the connected algorithm can be used to assign them to the nearest cluster label.
The paper did not give a specific implementation algorithm. However, the application scenario of this article is to generate pixel paintings, which will downsample the pixels, and will not refine to each pixel. Therefore, this article does not perform clustering optimization processing.
To sum up, the SLIC algorithm process is roughly the same as K-means
, and iteratively calculates the cluster with the smallest distance. The difference is that only the S
distance of the cluster center are calculated, which reduces a lot of calculation.
Generate pixel art
Based on the SLIC
algorithm, we can already divide a picture into N
super pixels. The pixels in each superpixel are similar. In other words, each pixel is classified as a super pixel and has a cluster center. Then assign the color of the pixel to the color of its cluster center to get the effect we want.
Set a certain step size stride
, use Canvas
, and assign the pixel to the color of the cluster center stride
And everyone's subjective feelings about pixel art are inconsistent, in order to let users have more choices and get their own satisfactory results. More manual intervention parameters can be exposed, such as canceling the termination condition of clustering optimization, and changing the number of iterations and the final step size of the pixel value to be set by the user. Manually set parameters include
- Super pixel size
blocksize
; theblocksize
super pixel segmentation. - The number of iterations is
iters
; theiters
, the more accurate the segmentation result and the longer the calculation time. - The color space weight
weight
; theweight
greater the influence of the color on the segmentation result. - Take the pixel step size
stride
; thestride
, the closer the generated pixel image is to the super pixel, the more delicate it is.
Implement user interaction interface
As a tool, a user interaction interface is naturally needed. The front-end interface is based on HTML/Javascript/CSS
, and Canvas API
used to draw image content, while the user interaction panel selects the dat.gui
[3] library. dat.gui
is a lightweight graphical interface library, very suitable for parameter modification, often used as a demonstration of visual Demo. Supported parameter types include Number
, String
, Boolean
, custom functions, etc. Corresponding response events can be bound to different properties, and the event will be triggered automatically when the property value changes.
Add the following properties and events for generating pixelated tools:
- When the
iters、stride、blockSize、weight
(color space weight m) parameter changes, re-SLIC
algorithm and re-draw the calculation result; - Add
Upload image
andExport image
buttons to support users to upload pictures and download pixelated pictures;
Superimpose a layer of Canvas
Canvas
canvas layer of the drawn image to visualize the results of the algorithm and add the following functions
grid
switch controls whether to draw a pixel grid;Centers
switch controls whether to display cluster centers;Contours
switch controls whether to display cluster edge contours;
The drawing of the cluster center point Centers
ctx.fillRect
center point coordinates.
To draw the super pixel contour Contours
, the contour points need to be calculated first.
You can compare each pixel with the surrounding 8
pixels. If the number of pixels with different cluster centers is greater than 2
, it means that there are more than two different types of points around the pixel, and this point is a contour . The effect is as follows:
Finally, you have a simple tool for generating pixel art.
full version code address (JS version)
references
[1] Achanta R, Shaji A, Smith K, Lucchi A, Fua P, Su ̈sstrunk S. SLIC superpixels. Technical Report. IVRG CVLAB; 2010.
[2] Gerstner T , Decarlo D , Alexa M , et al. Pixelated image abstraction with integrated user constraints[J]. Computers & graphics, 2013.
[3] https://github.com/dataarts/dat.gui
Welcome to pay attention to the Bump Lab blog: aotu.io
Or follow the AOTULabs official account (AOTULabs) and push articles from time to time.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。