c++ - OpenCV:获取 3 channel RGB 图像,拆分 channel 并仅使用 R

我只想查看 RGB 图像中的 R+G channel ,因为当蓝色 channel 被移除时,我可以获得更好的对比度来检测物体。我使用 OpenCV 拆分 channel ,但是在将蓝色 channel 设置为 0 后合并相同的 channel 时,我的代码无法编译。

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image,fin_img;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

   namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
                      // Show our image inside it.

    // Create Windows
    namedWindow("Red",1);
    namedWindow("Green",1);
    namedWindow("Blue",1);

    // Create Matrices (make sure there is an image in input!)

    Mat channel[3];
    imshow( "Original Image", image ); 


    // The actual splitting.
    split(image, channel);


   channel[0]=Mat::zeros(Size(image.rows, image.cols), CV_8UC1);//Set blue channel to 0

    //Merging red and green channels

    merge(channel,image);
    imshow("R+G", image);

    waitKey(0);//Wait for a keystroke in the window
    return 0;
}

我可以对我哪里出错有任何反馈吗?我怀疑是将蓝色 channel 设置为 0。有没有更好的方法将其设置为 0?有没有办法使用 cvMixChannels() 来做到这一点?

最佳答案

你需要改变这些行

    channel[0]=Mat::zeros(Size(image.rows, image.cols), CV_8UC1);//Set blue channel to 0

    //Merging red and green channels
    merge(channel,image);

    channel[0]=Mat::zeros(image.rows, image.cols, CV_8UC1);//Set blue channel to 0

    //Merging red and green channels
    merge(channel,3,image);

编辑

根据您的评论,这里是完整的代码和结果。

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

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image,fin_img;
    image = imread("bgr.png", CV_LOAD_IMAGE_COLOR);   // Read the file

    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

   namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
                      // Show our image inside it.

    // Create Windows
    namedWindow("Red",1);
    namedWindow("Green",1);
    namedWindow("Blue",1);

    // Create Matrices (make sure there is an image in input!)

    Mat channel[3];
    imshow( "Original Image", image );


    // The actual splitting.
    split(image, channel);


   channel[0]=Mat::zeros(image.rows, image.cols, CV_8UC1);//Set blue channel to 0

    //Merging red and green channels

    merge(channel,3,image);
    imshow("R+G", image);
    imwrite("dest.jpg",image);

    waitKey(0);//Wait for a keystroke in the window
    return 0;
}

源图片

没有蓝色分量的结果

关于c++ - OpenCV:获取 3 channel RGB 图像,拆分 channel 并仅使用 R+G 查看图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20341910/

相关文章:

c++ - 对两个对应的数组进行排序

c++ - 不能在数组上使用 .begin() 或 .end()

c++ - 如何以跨平台的方式获取(几乎)唯一的系统标识符?

c++ - Visual Studio 可以在重建前提示吗?

c++ - 如何检查 STL 迭代器是否指向任何东西?

c++ - 在 C++ 中,是否可以获取函数的返回类型以便在不调用该函数的情况下声明一个变量?

c++ - 有没有办法在 Visual Studio 中获取某些 c++ 函数的调用图?

c++ - 为什么 PRIu64 在此代码中不起作用?

c++ - 如何获得 high_resolution_clock 的精度?

c++ - 使用 boost::graph 获取特定边缘