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);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM