简体   繁体   中英

why doesn't the following code show the Red channel of an image?

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

int main()
{
    Mat src = imread("image.png", 1);
    namedWindow("src", 1); 
    imshow("src", src);

    vector<Mat> rgbChannels(3);
    split(src, rgbChannels);

    namedWindow("R", 1); 
    imshow("R", rgbChannels[2]);

    waitKey(0);
    return 0;
}

在此处输入图片说明

.

I was expecting something like the following:

在此处输入图片说明

why doesn't the above code show the Red channel? why does it show a grayscale image?

if the image is split into 3 channels, each matrix should show one of the colors of r, g, and b. isn't that so?

Your code is correct; however, OpenCV is showing the channel as grayscale. Mat does not keep the information about "where" the data came from. In other words, it does not know it was a red channel, so when you call imshow , it displays it as a single-channel image.

What you can do is build up an empty image with 2 zero'd channels and the one you want to visualize.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM