Brian
Brian

Reputation: 305

Unable to split RGB channels

Using OpenCV 3.2.0 on Windows, I'm trying to split an image into its component channels. I created the source image file using MS Paint (saved as 24-bit BMP), and set each object to its pure color. As you can see from the results, each channel contains everything BUT the data for that channel. I'm confused. How do I get just the green data in the green image, and so on? I'm using the following code:

#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <string>
#include <cstdlib>
#include "opencv2\opencv.hpp"

using namespace std;
using namespace cv;

const string    source_window = "Source";
const string    red_window = "Red";
const string    green_window = "Green";
const string    blue_window = "Blue";

int main (int Argc, char** Argv)
    {
    Mat         src = imread ("Test.bmp");
    vector<Mat> rgb;

    namedWindow (source_window, WINDOW_AUTOSIZE);
    namedWindow (red_window, WINDOW_AUTOSIZE);
    namedWindow (green_window, WINDOW_AUTOSIZE);
    namedWindow (blue_window, WINDOW_AUTOSIZE);

    imshow (source_window, src);
    cv::split (src, rgb);
    imshow (red_window, rgb [2]);
    imshow (green_window, rgb [1]);
    imshow (blue_window, rgb [0]);

    waitKey (0);
    }

Results

Upvotes: 2

Views: 335

Answers (1)

alexisrozhkov
alexisrozhkov

Reputation: 1632

This is how it should look. Consider this:

  • White color in RGB: 255, 255, 255
  • Red color in RGB: 255, 0, 0
  • Green color in RGB: 0, 255, 0
  • Blue color in RGB: 0, 0, 255

If you split your image into channels, you wont be able to see red object on white background in red channel (because both object and background have values 255), same for other channels.

To make your sample work as you expect you should fill background with black color. This way each channel will "contain" only corresponding figure.

Upvotes: 2

Related Questions