Disclosure of Invention
The technical problem to be solved by the invention is to provide a computer oil painting generation method, which is simple and has high implementation efficiency, aiming at the defects that the computer oil painting generation methods in the prior art are too complex and redundant and have low implementation efficiency.
The technical scheme adopted by the invention for solving the technical problems is as follows: a method for generating a computer oil painting is constructed, and comprises the following steps:
A. receiving a source image to be processed, judging whether the source image is a color image or a gray image, and if the source image is the color image, executing the step B; if yes, executing step C;
B. converting the color image into a gray image;
C. sampling points are taken in a grid mode on the gray-scale image, and random offset is carried out on the sampling points;
D. calculating a gradient at the sampling point by using an edge operator in a horizontal direction and an edge operator in a vertical direction based on the gray value of the sampling point;
E. calculating a stroke direction from the calculated gradient;
F. taking pixels in 3-by-3 neighborhood as a window by taking the sampling point as the center, and calculating the edge intensity in the window by using an edge operator in the 45-degree direction and an edge operator in the 135-degree direction;
G. determining a stroke radius according to the edge strength;
H. taking the pixel value of the corresponding position of the sampling point on the source image as the pixel value of the current brush;
I. and drawing sampling points according to the stroke direction, the stroke radius and the pixel value of the current brush.
In the method for generating the computer oil painting, in the step B, the color drawing is converted into a gray scale drawing according to the following formula:
Y1 = 0.299*R + 0.587*G + 0.114*B
wherein, Y1 is the gray value of the current pixel of the gray map, and R, G, B is the color values of the red, green and blue channels of the current pixel of the color map.
In the method for generating the computer oil painting, in the step B, the color drawing is converted into a gray scale drawing according to the following formula:
y2= (29 × B + 150 × G + 76 × R + 255)/256; or Y2= (29 × B + 150 × G + 76 × R + 255) > >8
Wherein Y2 is the gray value of the current pixel of the gray map, R, G, B is the color values of the red, green and blue channels of the current pixel of the color map, respectively, and >8 represents a right shift of 8 bits.
In the method for generating a computer oil painting of the present invention, the step C includes:
C1. dividing the gray scale map into a plurality of grids, wherein each grid comprises 7 × 7 pixel points;
C2. taking the vertex of each grid as a sampling point;
C3. generating a random value within a value range of 1-3, wherein the position offset of the sampling point in the X direction is three times of the generated random value, and the position offset of the sampling point in the Y direction is the generated random value;
C4. adding the position of the sampling point in the X direction and the position of the sampling point in the Y direction to the position offset amount in the X direction and the position offset amount in the Y direction, respectively.
In the method for generating a computer oil painting according to the present invention, the step D includes:
D1. matrix multiplication is carried out on the edge operator in the horizontal direction and the gray value of the sampling point on the gray map to obtain the edge intensity in the horizontal direction;
D2. matrix multiplication is carried out on the edge operator in the vertical direction and the gray value of the sampling point on the gray map to obtain the edge intensity in the vertical direction;
D3. and calculating the gradient at the sampling point according to the edge intensity of the sampling point in the horizontal direction and the edge intensity of the sampling point in the vertical direction.
In the method for generating a computer oil painting, in the step E, the vertical direction of the gradient is the stroke direction.
In the method for generating the computer oil painting, in the step F, the edge strength in the window is calculated according to the following formula:
wherein E is1Edge operator in 45 DEG orientation, E2For the edge operator in the 135 ° direction, I (x, y) is the pixel point in the window, and e (x, y) is the edge strength in the window.
In the step G, the stroke radius is determined by looking up a table according to a pre-stored correspondence table between the edge strength and the radius.
In the method for generating the computer oil painting, a drawing mark is set for each position, and the drawing mark is used for marking whether the drawing of the corresponding position is finished or not;
after step I, the generation method further comprises:
J. altering the drawing indicia.
The invention also constructs a computer oil painting generation system, comprising:
the receiving and judging module is used for receiving a source image to be processed and judging whether the source image is a color image or a gray image;
the grey-scale map generation module is used for converting the color map into a grey-scale map;
the random offset module is used for taking sampling points on the gray-scale image in a grid mode and carrying out random offset on the sampling points;
a gradient calculation module for calculating a gradient at the sampling point using a horizontal direction edge operator and a vertical direction edge operator based on the gray value of the sampling point;
a stroke direction calculation module for calculating a stroke direction according to the calculated gradient;
the edge intensity calculation module is used for taking pixels in 3 x 3 neighborhoods as a window by taking the sampling point as the center, and calculating the edge intensity in the window by using an edge operator in the 45-degree direction and an edge operator in the 135-degree direction;
a stroke radius determination module for determining a stroke radius according to the edge strength;
the pixel value determining module is used for taking the pixel value of the corresponding position of the sampling point on the source image as the pixel value of the current brush;
and the drawing module is used for drawing sampling points according to the stroke direction, the stroke radius and the pixel value of the current brush.
By implementing the technical scheme of the invention, the method is simple and the realization efficiency is high when the oil painting is generated by using a computer.
Detailed Description
As shown in fig. 1, in a flowchart of a first embodiment of a method for generating a computer oil painting, the method includes:
s1, receiving a source image to be processed, judging whether the source image is a color image or a gray image, and if the source image is the color image, executing a step S2; if yes, go to step S3;
s2, converting the color image into a gray image;
s3, sampling points are taken on the gray-scale image in a grid mode, and random offset is carried out on the sampling points;
s4, calculating the gradient at the sampling point by using an edge operator in the horizontal direction and an edge operator in the vertical direction based on the gray value of the sampling point;
s5, calculating a stroke direction according to the calculated gradient;
s6, taking pixels in 3-x 3 neighborhood as a window by taking the sampling point as the center, and calculating the edge intensity in the window by using an edge operator in the 45-degree direction and an edge operator in the 135-degree direction;
s7, determining a stroke radius according to the edge strength;
s8, taking the pixel value of the corresponding position of the sampling point on the source image as the pixel value of the current brush, wherein in the step, it needs to be explained that if the source image is a color image, the pixel value of the corresponding position of the sampling point is an RGB value, and if the source image is a gray level image, the pixel value of the corresponding position of the sampling point is a gray level value;
and S9, drawing sampling points according to the stroke direction, the stroke radius and the pixel value of the current brush.
It should be noted that steps S3 to S9 are only processing performed on one sampling point, and those skilled in the art should understand that in actual operation, the same processing needs to be performed on all sampling points, which is not described herein. In addition, the sequence of steps in this embodiment is merely for explanation and is not intended to limit the scope of the present invention, and in other embodiments, the sequence of steps S4 to S5, steps S6 to S7, and step S8 may be interchanged and may be performed simultaneously.
The technical scheme of the embodiment is implemented, the method is simple, and the realization efficiency is high. The implementation of each step will be specifically described below.
In step S2, the color map may be converted into a grayscale map according to the following formula:
Y1 = 0.299*R + 0.587*G + 0.114*B
wherein, Y1 is the gray value of the current pixel of the gray map, and R, G, B is the color values of the red, green and blue channels of the current pixel of the color map. It should be noted here that the image processing in the present application is performed on a pixel-by-pixel basis.
Preferably, in order to realize fast calculation on the computer, the formula of the gray value of the current pixel point of the obtained gray map may be rewritten as:
y2= (29 × B + 150 × G + 76 × R + 255)/256; or Y2= (29 × B + 150 × G + 76 × R + 255) > >8
Where >8 indicates a bit-wise shift by 8 bits to the right, the calculation is equivalent to a division by 256; y2 is the gray value of the current pixel of the gray map, and R, G, B is the color values of the red, green and blue channels of the current pixel of the color map.
In step S3, with reference to fig. 2, step S3 may specifically include:
s31, dividing the gray scale map into a plurality of grids, wherein each grid comprises 7 × 7 pixel points;
s32, taking the top point of each grid as a sampling point;
s33, generating a random value in a value range of 1-3, wherein the position offset of the sampling point in the X direction is three times of the generated random value, and the position offset of the sampling point in the Y direction is the generated random value, and it should be noted that the position offset in the X direction and the position offset in the Y direction can be calculated according to the following formulas:
Offset_x = 3 * (Rand()%3)
Offset_y = Rand()%3
wherein Offset _ X is a position Offset amount in the X direction; offset _ Y is a position Offset amount in the Y direction; rand ()%3 represents a remainder obtained by dividing a generated random variable by 3, namely a random value in a value range of 1-3;
s34, adding the position of the sampling point in the X direction and the position of the sampling point in the Y direction to the position offset in the X direction and the position offset in the Y direction respectively.
In step S4, with reference to fig. 3, step S4 may specifically include the following steps:
s41, performing matrix multiplication on the edge operator in the horizontal direction and the gray value of the sampling point on the gray map to obtain the edge strength in the horizontal direction;
s42, performing matrix multiplication on the edge operator in the vertical direction and the gray value of the sampling point on the gray map to obtain the edge intensity in the vertical direction, wherein in the steps S41 and S42, the edge operator S in the horizontal directionxAnd edge operator S in the horizontal directionySobel operators of 3 x 3 each, and
s43, calculating the gradient of the sampling point according to the edge strength of the sampling point in the horizontal direction and the edge strength of the sampling point in the vertical direction.
In step S5, the stroke direction may be calculated according to the following formula
:
Wherein,
the gradient direction of the current sampling point.
In the step S6, the edge strength in the window may be calculated according to the following formula:
wherein E is1Edge operator in 45 DEG orientation, E2For the edge operator in the 135 ° direction, I (x, y) is the pixel point in the window, and e (x, y) is the edge strength in the window.
In step S7, a table of correspondence between edge intensity and radius may be stored in advance, and a linear relationship exists between the edge intensity and the radius. When determining the stroke radius, the stroke radius is determined by means of a table look-up.
In addition, in a preferred embodiment, a drawing flag is set for each position, the drawing flag is used to identify whether drawing of the corresponding position is completed, and after step S9, the generating method further includes: and changing the drawing mark of the corresponding position. In this way, strokes in the same layer can be prevented from overlapping.
In a logic diagram of a first embodiment of a computer oil painting generation system shown in fig. 4, the generation system includes:
the receiving and judging module 01 is used for receiving a source image to be processed and judging whether the source image is a color image or a gray image;
a grayscale image generation module 02 for converting the color image into a grayscale image;
the random offset module 03 is configured to take sampling points in a grid manner on the grayscale image and perform random offset on the sampling points;
a gradient calculation module 04, configured to calculate a gradient at the sampling point by using a horizontal edge operator and a vertical edge operator based on the grayscale value of the sampling point;
a stroke direction calculation module 05, configured to calculate a stroke direction according to the calculated gradient;
an edge strength calculation module 06, configured to open a 3 × 3 window with the sampling point as a center, and calculate an edge strength in the window by using an edge operator in a 45 ° direction and an edge operator in a 135 ° direction;
a stroke radius determining module 07, configured to determine a stroke radius according to the edge strength;
a pixel value determining module 08, configured to use a pixel value of the corresponding position of the sample point on the source image as a pixel value of the current brush;
and the drawing module 09 is used for drawing sampling points according to the stroke direction, the stroke radius and the pixel value of the current brush.
In a preferred embodiment, the generating system may further include: and the drawing mark module is used for changing the drawing mark at the corresponding position after the drawing is finished.
It should be noted that the preferred schemes in all the generation methods can be applied to the corresponding generation systems, and are not described herein again.
The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention, and various modifications and changes may be made by those skilled in the art. Any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention should be included in the scope of the claims of the present invention.