OpenCV Split Merge - eiichiromomma/CVMLAB GitHub Wiki

(OpenCV) チャネルの分割

カラー画像をRed, Green, Blue, (Alpha)のチャネルに分ける

split, merge, mixChannels (2.x)

mixChannelsはチャネルの入れ替え。複数画像間でも可能。from_toの書き方と,予めMatを確保しておく必要がある。

    //  main.cpp
    //  Merge & Split
    //
    //  Created by Eiichiro Momma on 2014/08/12.
    //  Copyright (c) 2014 Eiichiro Momma. All rights reserved.
    //

    #include <iostream>
    #include <stdio.h>
    #include <opencv2/opencv.hpp>
    #include <opencv2/core/core.hpp>

    int main(int argc, const char * argv[])
    {
        cv::Mat img = cv::imread("lena.jpg",cv::IMREAD_ANYCOLOR);
        //split
        cv::Mat *pImg = new cv::Mat[3];
        std::vector<cv::Mat> vImg;
        cv::split(img, pImg);
        cv::split(img, vImg);
        cv::imshow("pImg0", pImg[0]);
        cv::imshow("pImg1", pImg[1]);
        cv::imshow("pImg2", pImg[2]);
        std::vector<cv::Mat>::iterator it;
        int count=0;
        for (it = vImg.begin(); it!=vImg.end(); it++) {
            char wname[256];
            sprintf(wname, "vImg[%d]",count++);
            cv::imshow(wname, *it);
        }
        
        //merge
        cv::Mat pdst;
        cv::Mat vdst;
        cv::merge(pImg, 3, pdst);
        cv::merge(vImg, vdst);
        cv::imshow("pdst", pdst);
        cv::imshow("vdst", vdst);
        
        //mixChannels
        //0chを1chに,1chを2chに,2chを0chに(B->G, G->R, R->B)
        int from_to[] = {0, 1, 1,2, 2, 0};
        cv::Mat vMixed(img.rows, img.cols, img.type());
        std::vector<cv::Mat> dv;
        dv.push_back(vMixed);
        cv::mixChannels(vImg, dv, from_to, 3);
        cv::imshow("vMixed", vMixed);
        cv::Mat pMixed(img.rows, img.cols, img.type());
        cv::Mat dm[] = {pMixed};
        cv::mixChannels(pImg, 3, dm, 1, from_to, 3);
        cv::imshow("pMixed", pMixed);
        cv::waitKey(0);
        return 0;
        
    }

使う関数

OpenCV cvCvtPixToPlane

⚠️ **GitHub.com Fallback** ⚠️