zoukankan      html  css  js  c++  java
  • 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"
  • 相关阅读:
    VMware虚拟机安装
    dmesg功能介绍
    Linux查看MAC地址方法
    linux介绍
    spring boot整合mybatis框架及增删改查(jsp视图)
    idea 热部署的配置
    idea注释类,方法
    idea2018破解
    BootStrap简单table
    解决bootstrap模态框居中问题
  • 原文地址:https://www.cnblogs.com/Vince-Wu/p/11391561.html
Copyright © 2011-2022 走看看