• OpenCv 016---图像ROI与ROI操作


    1 前备知识

    null

    2 所用到的主要OpenCv API

    skip

    3 程序代码

    #include<opencv2/opencv.hpp>
    #include<iostream>
    
    using namespace cv;
    using namespace std;
    
    int main(int argc, char** argv)
    {
        Mat src = imread("G:\CVworkstudy\program_wwx\Research society140\ZhaiZhigang140\colormap.png");
        namedWindow("input", WINDOW_AUTOSIZE);
        imshow("input", src);
        int h = src.rows;
        int w = src.cols;
    
        //get ROI
        int cy = h / 2;
        int cx = w / 2;
        Rect rect(cx - 100, cy - 100, 200, 200);
        Mat roi = src(rect);
        imshow("roi", roi);
    
        Mat image = roi.clone();
        // modify ROI
        roi.setTo(Scalar(255, 0, 0));
        imshow("result", src);
        
        //modif copy roi
        image.setTo(Scalar(255, 0, 0));
        imshow("result", src);
        imshow("copy roi", image);
    
        //example with ROI - generate mask
        Mat src2 = imread("G:\CVworkstudy\program_wwx\Research society140\ZhaiZhigang140\zhengjianzhao.jpg");
        imshow("src2", src2);
        Mat hsv, mask;
        cvtColor(src2, hsv, COLOR_BGR2HSV);
        //That is, dst (I) is set to 255 (all 1 -bits) if src (I) is within the
        //specified 1D, 2D, 3D, ... box and 0 otherwise.
        inRange(hsv, Scalar(100, 43, 46), Scalar(124, 255, 255), mask);//within the wise set to 255,if not,set 0
        imshow("mask", mask);//get the mask exclusive person
    
        //extract person ROI
        Mat person;
        bitwise_not(mask, mask);
        Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
        morphologyEx(mask, mask, MORPH_CLOSE, kernel);
        bitwise_and(src2, src2, person, mask);
        imshow("person", person);
    
        //generate background
        Mat result = Mat::zeros(src2.size(), src2.type());
        result.setTo(Scalar(0, 0, 255));
    
        //combine background + person
        Mat dst;
        bitwise_not(mask, mask);
        bitwise_or(person, result, dst, mask);
        add(dst, person, dst);
    
        imshow("dst", dst);
        waitKey(0);
        return 0;
    }

    4 运行结果

    后面几张图省略,主要是实现证件照背景颜色替换,由蓝色替换为红色,颜色HSV范围表参见:

    OpenCv 009---色彩空间与色彩空间转换

    5 扩展及注意事项

    null

    One day,I will say "I did it"
  • 相关阅读:
    Android Things专题 1.前世今生
    用Power BI解读幸福星球指数
    [leetcode]Simplify Path
    字段的划分完整的问题
    k-means算法MATLAB和opencv代码
    【Oracle】RAC下的一些经常使用命令(一)
    Java中经常使用缓存Cache机制的实现
    jenkins环境自动部署
    jenkins环境搭建
    springboot单元测试@test的使用
  • 原文地址:https://www.cnblogs.com/Vince-Wu/p/11391561.html
Copyright © 2020-2023  润新知