Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9249685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:15:40+00:00 2026-06-18T10:15:40+00:00

I need to create a ‘red’ image from a grayscale image. I am using

  • 0

I need to create a ‘red’ image from a grayscale image. I am using this code:

void build_red(const cv::Mat& in, cv::Mat& out) {
    out = Mat::zeros(in.rows, in.cols, CV_8UC1);

    Mat zeros = Mat::zeros(in.rows, in.cols, CV_8UC1);
    Mat tmp;
    in.convertTo(tmp, CV_8UC1);

    vector<Mat> ch;
    ch.push_back(zeros);
    ch.push_back(zeros);
    ch.push_back(tmp);

    cout << "Using " << ch.size() << " channels" << endl;
    merge(ch, out);
} // build_red

With some explanations:

void build_red(const cv::Mat& in, cv::Mat& out) {

in is the input matrix, out the output.

out = Mat::zeros(in.rows, in.cols, CV_8UC1);

allocate some space for out (may be useless, but part of my attempts)

    Mat zeros = Mat::zeros(in.rows, in.cols, CV_8UC1);
    Mat tmp;
    in.convertTo(tmp, CV_8UC1);

Create an empty matrix with the same size and convert the input image to single-channel uchar.

    vector<Mat> ch;
    ch.push_back(zeros);
    ch.push_back(zeros);
    ch.push_back(tmp);

    cout << "Using " << ch.size() << " channels" << endl;
    merge(ch, out);

Create a vector with three channels, then merge them into ‘out’.

However, when I run the code I get the following message:

     Using 3 channels

and the following exception:

OpenCV Error: Bad number of channels (Source image must have 1, 3 or 4 channels) 
in cvConvertImage, file /[...]/libs/OpenCV-2.4.0/modules/highgui/src/utils.cpp, 
line 611
terminate called after throwing an instance of 'cv::Exception'
what():  /[...]/libs/OpenCV-2.4.0/modules/highgui/src/utils.cpp:611: 
error: (-15) Source image must have 1, 3 or 4 channels in function cvConvertImage

Could you please help me? From my inexperienced point of view, the type of the images is the same and the number of channels is correct.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:15:41+00:00Added an answer on June 18, 2026 at 10:15 am

    Why are you converting the image if you have a grayscale image present?

    Just create two empty matrix of the same size for Blue and Green.

    And you have defined your output matrix as 1 channel matrix. Your output matrix must contain at least 3 channels. (Blue, Green and Red). Where Blue and Green will be completely empty and you put your grayscale image as Red channel of the output image.

    #include <opencv2/highgui/highgui.hpp> 
    #include <stdio.h>
    using namespace std;
    using namespace cv;
    
    int main()
    {
        Mat img, g, fin_img;
        img = imread("Lenna.png",CV_LOAD_IMAGE_GRAYSCALE);
        vector<Mat> channels;
    
        g = Mat::zeros(Size(img.rows, img.cols), CV_8UC1);
    
        channels.push_back(g);
        channels.push_back(g);
        channels.push_back(img);
    
        merge(channels, fin_img);
        imshow("img", fin_img);
        waitKey(0);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Visual Studio 2008 and I need create a DataTable from a Excel
This is my code. I need create 10 files with this format: 0.txt 1.txt
I am using this API to create an online image gallery for my application
Need to create a custom DNS name server using C which will check against
I need create an interface like the image in android. Please help me. thanks
I need create Javascript array from xml .. I`v get xml data from poly
I to need create a programmatic equivalent using delphi language... or could someone post
I want add new user from another script and I need create password for
I am doing a website using php , i need create a pdf doc
I need create one item in my ToolStripMenuItem with this feature: if I check

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.