• openCV抠图实验


    #include "pch.h"
    #include <opencv2/core/core.hpp>
    #include "opencv2/imgproc/imgproc.hpp"
    #include <opencv2/highgui/highgui.hpp>
    #include <iostream>
    using namespace cv;
    using namespace std;
    
    int main(){
        Mat image;
        image = imread("1.jpg", IMREAD_COLOR);
        if (!image.data){
            cout << "Could not open or find the image" << endl;
            return -1;
        }
    
        // threshold to get mask
        int threshold_value = 10;
        int max_BINARY_value = 256;
        Mat mask;
        mask = imread("mask.png", 0);
    
        // mask image
        Mat img_masked;
        image.copyTo(img_masked, mask);
    
        imshow("image", image);
        imshow("mask", mask);
        imshow("img_masked", img_masked);
        waitKey(0);
        return 0;
    }
    #include <opencv2/opencv.hpp>
    #include <opencv2/xfeatures2d.hpp>
    #include<opencv2/face.hpp>
    #include<iostream>
    #include<math.h>
    #include <string>
    #include<fstream>
    
    using namespace cv::face;
    using namespace cv;
    using namespace std;
    using namespace cv::xfeatures2d;
    
    int  main() {
        Mat src = imread("/Users/war/Desktop/2.jpeg");
        imshow("src", src);
        //组装数据
        
        int width = src.cols;
        int height = src.rows;
        int samplecount = width * height;
        int dims = src.channels();
        //行数为src的像素点数,列数为通道数,每列数据分别为src的bgr,从上到下 从左到右顺序读数据
        Mat points(samplecount, dims, CV_32F, Scalar(10));
        int ind = 0;
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                ind = row * width + col;//
                Vec3b bgr = src.at<Vec3b>(row, col);
                points.at<float>(ind, 0) = static_cast<int>(bgr[0]);
                points.at<float>(ind, 1) = static_cast<int>(bgr[1]);
                points.at<float>(ind, 2) = static_cast<int>(bgr[2]);
            }
        }
        //运行kmeans
        int numCluster = 4;
        Mat labels;
        Mat centers;
        TermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 0.1);
        kmeans(points, numCluster, labels, criteria, 3, KMEANS_PP_CENTERS, centers);
        //去背景+遮罩生成
        Mat mask = Mat::zeros(src.size(), CV_8UC1);
        int index = src.rows * 2 + 2;//不取边缘的左上点,往里靠2个位置
        int cindex = labels.at<int>(index, 0);
        int height1 = src.rows;
        int width1 = src.cols;
        Mat dst;//人的轮廓周围会有一些杂点,所以需要腐蚀和高斯模糊取干扰
        src.copyTo(dst);
        for (int row = 0; row < height1; row++) {
            for (int col = 0; col < width1; col++) {
                index = row * width1 + col;
                int label = labels.at<int>(index, 0);
                if (label == cindex) {
                    dst.at<Vec3b>(row, col)[0] = 0;
                    dst.at<Vec3b>(row, col)[1] = 0;
                    dst.at<Vec3b>(row, col)[2] = 0;
                    mask.at<uchar>(row, col) = 0;
                }
                else {
                    dst.at<Vec3b>(row, col) = src.at<Vec3b>(row, col);
                    mask.at<uchar>(row, col) = 255;//人脸部分设为白色,以便于下面的腐蚀与高斯模糊
                }
            }
        }
        imshow("dst", dst);
        imshow("mask", dst);
        //腐蚀+高斯模糊
        Mat k = getStructuringElement(MORPH_RECT, Size(3, 3));
        erode(mask, mask, k);
        GaussianBlur(mask, mask, Size(3, 3), 0, 0);
        imshow("gaosimohu", mask);
        //通道混合
        RNG rng(12345);
        Vec3b color;
        color[0] = 180;//rng.uniform(0, 255);
        color[1] =180;//rng.uniform(0, 255);
        color[2] =238;//rng.uniform(0, 255);
        Mat result(src.size(), src.type());
        
        double w = 0.0;
        int b = 0, g = 0, r = 0;
        int b1 = 0, g1 = 0, r1 = 0;
        int b2 = 0, g2 = 0, r2 = 0;
        
        double time = getTickCount();
        for (int row = 0; row < height1; row++) {
            for (int col = 0; col < width; col++) {
                int m = mask.at<uchar>(row, col);
                if (m == 255) {
                    result.at<Vec3b>(row, col) = src.at<Vec3b>(row, col);//前景
                }
                else if (m == 0) {
                    result.at<Vec3b>(row, col) = color; // 背景
                }
                else {//因为高斯模糊的关系,所以mask元素的颜色除了黑白色还有黑白边缘经过模糊后的非黑白值
                    w = m / 255.0;
                    b1 = src.at<Vec3b>(row, col)[0];
                    g1 = src.at<Vec3b>(row, col)[1];
                    r1 = src.at<Vec3b>(row, col)[2];
                    b2 = color[0];
                    g2 = color[0];
                    r2 = color[0];
                    
                    b = b1 * w + b2 * (1.0 - w);
                    g = g1 * w + g2 * (1.0 - w);
                    r = r1 * w + r2 * (1.0 - w);
                    
                    result.at<Vec3b>(row, col)[0] = b;//最终边缘颜色值
                    result.at<Vec3b>(row, col)[1] = g;
                    result.at<Vec3b>(row, col)[2] = r;
                    
                }
            }
        }
        cout << "time=" << (getTickCount() - time) / getTickFrequency() << endl;
        imshow("backgroud repalce", result);
        waitKey(0);
    }
  • 相关阅读:
    读取纯真IP数据库C++源代码
    Ubuntu 安装VMware tools 解决方法讨论
    VMware虚拟机磁盘压缩
    使用 TestLink 进行测试管理
    正则表达式30分钟入门教程(转)
    HDU 1325 Is It A Tree? POJ 1308 Is It A Tree? (并查集+入度,判断一个有向图是树)
    POJ 1703 Find them, Catch them (并查集)
    POJ 1182 食物链(并查集)
    POJ 1984 Navigation Nightmare (并查集)
    POJ 2236 Wireless Network (并查集)
  • 原文地址:https://www.cnblogs.com/war1111/p/10972326.html
Copyright © 2020-2023  润新知