How to use OpenCV to realize the function of changing blue background to white background in C++.
This article mainly introduces how to use OpenCV in C++ to achieve the function of changing blue background to white background, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.
The main steps are:
1. Convert RGB image to HSV space
two。 Take a small piece of the background 20 to 20 to calculate the average hue and saturation of the blue background.
3. Set the threshold and replace the blue background with the red background
4. Convert HSV image to RGB space
5. Filter to remove edge effect
The specific code is:
/ / change_color.cpp: defines the entry point for the console application. / / change the ID photo from blue background to red background / / # include "stdafx.h" # include # include using namespace cv;using namespace std;int main () {char * origin= "Original"; char * window= "Image"; char * str= "C:\\ Users\\ ltc\\ Desktop\ nihao.jpg"; namedWindow (origin,1); namedWindow (window,1); Mat image=imread (str); if (! image.data) {cout