Use opencv2 to change the photo background (from blue to red) and opencv2 certificates
There was a document photo on hand. I tried to replace the blue background with a red or white background. There was not much information found on the internet, and most of them were written based on opencv1. So I tried to write opencv2. The main steps are as follows:
1. Convert RGB image to HSV space
2. Take a small section of the background 20*20 to calculate the average color and saturation of the blue background.
3. Set the threshold and replace the blue background with the red background.
4. Convert the HSV image to RGB space
5. Filter edge Removal
The Code is as follows:
// Change_color.cpp: defines the entry point of the console application. // Change the ID photo from blue to Red # include "stdafx. h "# include <iostream> # include <opencv2 \ core. hpp> # include <opencv2 \ highgui. hpp> # include <opencv2 \ imgproc. hpp> using namespace cv; using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {char * origin = "Original "; char * window = "Image"; char * str = "D: \ jiangshan \ change_color \ IMG_1047.jpg"; namedWindow (origin, 1); namedWindow (window, 1 ); mat image = imread (str ); If (! Image. data) {cout <"image loading problems" <endl; return 0;} Mat roi = image (Rect (20, 20, 20); Mat hsvImg; cvtColor (image, hsvImg, CV_BGR2HSV); // convert the image to HSV Color Space // separate the HSV space, v [0] is H-tone, v [1] is S saturation, v [2] is a v grayscale vector <Mat> v; split (hsvImg, v); Mat roiH = v [0] (Rect (20, 20, 20 )); mat roiS = v [1] (Rect (20, 20, 20); int SumH = 0; int SumS = 0; int avgH, avgS; // average color and average saturation of the blue background // calculate the average color and average saturation of a blue background (int I = 0; I <20; I ++) {for (int j = 0; j <20; j ++ ){/ * SumH = SumH + roiH (I, j); */SumH = int (roiH. at <uchar> (j, I) + SumH; SumS = int (roiS. at <uchar> (j, I) + SumS ;}} avgH = SumH/400; avgS = SumS/400; // traverse the entire image int nl = hsvImg. rows; int nc = hsvImg. cols; int step = 10; for (int j = 0; j <nl; j ++) {for (int I = 0; I <nc; I ++) {// use H.S channels for threshold segmentation, and replace blue with red if (v [0]. at <uchar> (j, I) <= (avgH + 5) & v [0]. at <uchar> (j, I)> = (avgH-5) & (v [1]. at <uchar> (j, I) <= (avgS + 40) & v [1]. at <uchar> (j, I)> = (avgS-40) {// cout <in T (v [0]. at <uchar> (j, I) <endl; v [0]. at <uchar> (j, I) = 0;/* cout <int (v [0]. at <uchar> (j, I) <endl; */} Mat finImg; merge (v, finImg); Mat rgbImg; cvtColor (finImg, rgbImg, cv_hsv 2bgr); // convert the image back to the RGB space imshow (origin, image); imshow (window, rgbImg ); // Add a filter to filter out the value of the edge part (here we should use a low-pass filter, but it doesn't feel good, it's still unnecessary .) // Mat result; // GaussianBlur (rgbImg, result, Size (0.5),);/* imshow (window, result); */waitKey (0 ); // system ("pause"); return 0 ;}//// traverse the entire image // int nl = hsvImg. rows; // int nc = hsvImg. cols * hsvImg. channels (); // for (int j = 0; j <nl; j ++) // {// uchar * data = hsvImg. ptr <uchar> (j); // for (int I = 0; I <nc; I ++) // {// cout <int (data [I]) <"";//}//}
: