OpenCV 交换两个颜色通道

#include 
#include 
#include 
#include 
using namespace cv;
 
  
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QFile file("1.rgba");
    if(!file.open(QIODevice::ReadOnly))
        return -1;
    QByteArray arr = file.readAll();
    Mat ori(100, 720, CV_8UC4, arr.data());
    //Mat dest = Mat::zeros(100, 720,  CV_8UC4);
    //cvtColor(ori, dest, COLOR_RGBA2BGRA);
 
  
 
  
    QFile bgra("3A");
    bgra.open(QIODevice::WriteOnly);
    qDebug() << ori.channels();
    //channel 0 = R
    //channel 1 = G
    //channel 2 = B
    //channel 3 = A
    for(int y = 0; y < ori.rows; y++)
    {
        for(int x = 0; x < ori.cols; x++)
        {
 
  
            /*uchar rgba[4] = {0};
            rgba[0] = dest.at(y, x)[2];
            rgba[1] = dest.at(y, x)[1];
            rgba[2] = dest.at(y, x)[0];
            rgba[3] = dest.at(y, x)[3];
            bgra.write((const char*)rgba, 4);*/
            for( int c = 0; c < 4; c++)
            {
                uchar  chr = 0;
                if(c == 3)
                {
                    chr = 255;
                    bgra.write((const char*)&chr, 1);
                    continue;
                }
                bgra.write((const char*)&chr, 1);
            }
 
  
        }
    }
    bgra.close();
    qDebug() << "file generated";
    return a.exec();
}
 
 

你可能感兴趣的:(code,snippet,c++,opencv,开发语言)