OpenCV——RGB三通道分離


opencv 和 matlab 在處理彩色圖像的時候,通道的存儲順序是不同的。

matlab 的排列順序是R,G,B; 而在opencv中,排列順序是B,G,R。 

下面通過一個小程序看看opencv中的三個通道。


// PS_Algorithm.h

#ifndef PS_ALGORITHM_H_INCLUDED
#define PS_ALGORITHM_H_INCLUDED

#include <iostream>
#include <string>
#include "cv.h"
#include "highgui.h"
#include "cxmat.hpp"
#include "cxcore.hpp"

using namespace std;
using namespace cv;

#endif // PS_ALGORITHM_H_INCLUDED


// Public_Function.h

#ifndef PUBLIC_FUNCTION_H_INCLUDED
#define PUBLIC_FUNCTION_H_INCLUDED

#include <iostream>
#include <string>
#include "cv.h"
#include "highgui.h"
#include "cxmat.hpp"
#include "cxcore.hpp"

using namespace std;
using namespace cv;

void Show_Image(Mat&, const string &);
void Save_Image(Mat&, const string &);

#endif // PUBLIC_FUNCTION_H_INCLUDED


/*
The program will Divides multi-channel array
into several single-channel arrays.
We can get the single-channel from a multi-channel
image.
*/

#include "PS_Algorithm.h"
#include "Public_Function.h"
using namespace std;
using namespace cv;

int main()
{
    string Img_name("4.jpg");
    Mat Image_in;
    Image_in=imread(Img_name);
    Show_Image(Image_in, Img_name);


    // convert the type of the image.
    // from the uchar to the float.
    Mat Image_2(Image_in.size(), CV_32FC3);
    Image_in.convertTo( Image_2, CV_32FC3);

    Mat r(Image_in.rows, Image_in.cols, CV_32FC1);
    Mat g(Image_in.rows, Image_in.cols, CV_32FC1);
    Mat b(Image_in.rows, Image_in.cols, CV_32FC1);


    Mat out[]={b,g,r};
    split(Image_2, out);

    // 三個通道的順序是 b,g,r.
    b=out[0]/255;
    g=out[1]/255;
    r=out[2]/255;

    Show_Image(g, "g");
 
    waitKey();
}


原圖



R 通道



G 通道



B 通道



免責聲明!

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



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