转摘请注明:https://i.cnblogs.com/EditPosts.aspx?opt=1
有时候我们需要不一样颜色的证件照,下面就用OpenCV来实现证件照的蓝底、红底等换颜色;
代码如下:
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat g_srcImage = imread("11.jpg");
imshow("g_srcImage", g_srcImage);
waitKey(); vector<Mat> g_vChannels;
split(g_srcImage, g_vChannels); Mat imageBlueChannel = g_vChannels.at();
Mat imageGreenChannel = g_vChannels.at();
Mat imageRedChannel = g_vChannels.at(); Mat img_B = ( * imageBlueChannel - imageGreenChannel - imageRedChannel);
imshow("img_B", img_B);
waitKey(); threshold(img_B, img_B, , , CV_THRESH_OTSU);
imshow("img_B_g", img_B);
waitKey(); //对每个轮廓进行分析
vector <Mat> contours;
vector <Vec4i> hierarchy;
double min_area = ;//小于50的认为不是数字 findContours(img_B, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); // fill external contours
if (!contours.empty() && !hierarchy.empty())
{
for (int idx = ; idx < contours.size(); idx++)
{
//Mat temp_img(Size(img_B.size()), CV_8U, Scalar(0));
drawContours(img_B, contours, idx, Scalar(), CV_FILLED, );//填充 形成区域 drawContours(g_srcImage, contours, idx, Scalar(,,), CV_FILLED, );//填充 形成区域 }
}
imshow("g_srcImage11", g_srcImage);
waitKey();
imwrite("g_srcImage.bmp", g_srcImage); }
效果图: