OpenCV Learning Record: filter: woodcut & nostalgia
Wood Carving Filter
The wood carving filter is actually the binarization processing of the image. The binarization processing of an image sets the RGB value of each pixel to 0 or 255. Before binarization an image, perform grayscale processing on the image. grayscale is to set the RGB value of each pixel to the same size. There are three methods for grayscale image processing: the maximum value method, the average method or the weight method.
Maximum Value Method: As the name implies, the maximum value of three RGB components is used as the gray value, that is, gray = max (R, G, B). This method converts Grayscale Images with high brightness.
Average Method: Take the average of three RGB components as the gray value, that is, gray = (R + G + B)/3. The grayscale image generated by this method is gentle.
Weight Method: Calculate the gray-scale values of the three RGB components at different ratios. Human eyes are most sensitive to green, followed by red, and have the lowest sensitivity to blue. Therefore, gray images are easy to recognize. Generally, grayscale images have the best effect. The formula is as follows:
Gray = 0.30R + 0.59G + 0.11B.
Binarization is based on the grayscale image. All pixels whose gray value is greater than or equal to the threshold value are determined to belong to a specific object, and their gray value is 255, otherwise, these pixels are excluded from the object area. The gray value is 0, indicating the background or exceptional object area. The formula is as follows:
Gray = gray> threshold? 255: 0. To implement the wood carving filter, set the threshold to 127.
OpenCV has a dedicated grayscale image processing function: cvtColor ()
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 )
Parameter description:
Src: input image dst: output image code: color space conversion code. This value determines the conversion mode. to convert a color image to a gray image, set this value to CV_BGR2GRAY dstCn, which is generally 0.
This function can convert images in multiple modes, such as RGB, HSV, HLS, and Gray. The third parameter determines which mode to convert.
After the color image is grayscale, the image will change from three channels to one channel.
The full version code is as follows:
Void muKeFilter (Mat & srcImage) {Mat dstImage = srcImage; cvtColor (dstImage, dstImage, CV_BGR2GRAY); int rowNum = dstImage. rows; // number of rows to be processed int colNum = dstImage. cols; // Number of columns to be processed for (int j = 0; j
(J); for (int I = 0; I
127? 255: 0 ;}}}
If you do not need the cvtColor () function to perform grayscale preprocessing on the color image, you can use your own code to grayscale the image, as shown in the following code:
Int rowNum = srcImage. rows; // number of rows to be processed int colNum = srcImage. cols; // Number of columns to be processed for (int j = 0; j
(J); for (int I = 0; I
127? 255: 0; // binarization row [I * 3] = gray; row [I * 3 + 1] = gray; row [I * 3 + 2] = gray ;}}
At this time, the image is still three channels.
OpenCV has a dedicated binarization function.
double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
Parameter description:
Src: input image, must be a single-channel dst: output image thresh: Threshold Value maxval: Maximum Value
Type: There are five methods to set the value, such
Therefore, if this filter is written in the simplest way, there is very little code, as shown below: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;"> # Include # Include Using namespace cv; using namespace std; void muKeFilter (Mat & srcImage); int main () {Mat srcImage = imread ("lena.jpg"); if (! SrcImage. data | srcImage. empty () {cout <"An error occurred while reading the image! "<
:
Nostalgia Filter
A nostalgic filter is used to make a photo yellow. Main algorithm ideas:
Calculate the new RGB value according to the following formula:
int R = 0.393*r + 0.769*g + 0.189*b;
int G = 0.349*r + 0.686*g + 0.168*b;
int B = 0.272*r + 0.534*g + 0.131*b;
The RGB value must be between 0 and 255.
The main code is as follows:
# Include # Include Using namespace cv; using namespace std; void huaiJiuFilter (Mat & srcImage); int main () {Mat srcImage = imread ("lena.jpg"); if (! SrcImage. data | srcImage. empty () {cout <"An error occurred while reading the image! "< (J); for (int I = 0; I (0.393 * r + 0.769 * g + 0.189 * B); int G = static_cast (0.349 * r + 0.686 * g + 0.168 * B); int B = static_cast (0.272 * r + 0.534 * g + 0.131 * B); data [I * 3 + 2] = max (0, min (R, 255 )); data [I * 3 + 1] = max (0, min (G, 255); data [I * 3] = max (0, min (B, 255) ;}} imshow ("Nostalgic filter", srcImage );}
: