• 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"
  • 相关阅读:
    架构之道(1)
    看板管理(1)
    交互原型图
    Sequence Diagram时序图
    安卓项目的「轻」架构
    安卓ButtomBar实现方法
    工具类BitMap 把网络URL图片转换成BitMap
    使用OkHttp上传图片到服务器
    BaseAdapter教程(2) BaseAdapter的notifyDataSetChanged动态刷新
    开发中时间变换问题汇总
  • 原文地址:https://www.cnblogs.com/Vince-Wu/p/11391561.html
Copyright © 2020-2023  润新知