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 7637879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:02:22+00:00 2026-05-31T08:02:22+00:00

The code below was wrtting to access the elements of 3 RGB images converted

  • 0

The code below was wrtting to access the elements of 3 RGB images converted to matrices and place them in a bigger matrix. However, I have know realised that CV_MAT_ELEM(mat, type, row, col) is used only for accessing elements of single channel matrices, whereas my images are all 3 channel. Would would I go about fixing this code then, to access elements of a 3 channel matrix instead of a single channel matrix?

#include "cv.h" 
#include "highgui.h" 
#include "iostream" 

using namespace std; 

void cvDoubleMatPrint (const CvMat* mat)
{
int i, j;
for (i = 0; i < mat->rows; i++)
{
for (j = 0 ; j < mat->cols; j++)
{
    printf ( "%f ", cvGet2D (mat, i , j));
}
printf ( "\n" );
}
}


int main( int argc, char* argv ) 
{ 
CvMat *img0, *img1, *img2,  *img0_mat, *img1_mat, *img2_mat, *col0, *col1, *col2, *superMat = NULL;

img0 = cvLoadImageM("C:\\small\\walk mii.jpg", CV_LOAD_IMAGE_UNCHANGED);    
img1 = cvLoadImageM("C:\\small\\wave mii.jpg", CV_LOAD_IMAGE_UNCHANGED);  
img2 = cvLoadImageM("C:\\small\\fantasy.jpg", CV_LOAD_IMAGE_UNCHANGED); 

CvMat img0_header ,img1_header, img2_header;

col0 = cvReshape(img0, &img0_header, 0, 4800);
col1 = cvReshape(img1, &img1_header, 0, 4800);
col2 = cvReshape(img2, &img2_header, 0, 4800);

superMat = cvCreateMat(4800, 3, CV_8UC1);
cvSetZero(superMat);

for(int i=0; i<col0->height; i++)
{
CV_MAT_ELEM( *superMat, double, i, 0 ) = CV_MAT_ELEM( *col0, double, i, 0 );
}

for(int j=0; j<col1->height; j++)
{
CV_MAT_ELEM( *superMat, double, j, 1 ) = CV_MAT_ELEM( *col1, double, j, 0 );
}

 for(int k=0; k<col2->height; k++)
{
CV_MAT_ELEM( *superMat, double, k, 2 ) = CV_MAT_ELEM( *col2, double, k, 0 );
}


cvDoubleMatPrint(superMat);

cvWaitKey(0);
return 0;
  • 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-05-31T08:02:23+00:00Added an answer on May 31, 2026 at 8:02 am

    I’m not as familiar with the C API, but here is how you can accomplish what you are doing with the C++ API.

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    
    #include <iostream>
    #include <vector>
    
    using namespace cv;
    using namespace std;
    
    int main(int /*argc*/, char** /*argv*/)
    {
        cv::RNG rng = cv::theRNG();
    
        // create 3 random 10x10 matrices to simulate your situation.
        vector<Mat> matrices;
        for(int i = 0; i < 3; i++)
        {
            Mat temp(Size(10, 10), CV_64FC3);
            rng.fill(temp, RNG::UNIFORM, 0.0, 1.0);
            matrices.push_back(temp);
        }
    
        // reshape the matrices to have 1 row and 1 channel (i.e., 10x10x3 -> 300x1).
        vector<Mat> singleRows;
        vector<Mat>::iterator i;
        for(i = matrices.begin(); i != matrices.end(); i++)
        {
            singleRows.push_back(i->reshape(1, 1));
        }
    
        // finally, concatenate the matrices to make a 300x3 matrix.
        Mat concatenated;
        for(i = singleRows.begin(); i != singleRows.end(); i++)
        {
            concatenated.push_back(i->clone());
        }
    
        cout << concatenated.size().width << "x" << concatenated.size().height << endl;
    
        return 0;
    }
    

    If you need to access an RGB value in the C++ API see my other post on how to use the cv::Vec3b class. In general, I would recommend using the C++ API over the C API as it has more features and is much easier to use. Also, have a look at OpenCV functions split and merge. These functions are useful for multi-channel management. Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code below is working well as long as I have class ClassSameAssembly in same
I currently have the code below in my WPF application which does exactly what
The code below fails when running on Windows 7, with default access levels. (This
When writing the below my code locks up on GetResponse. Why? try { WebRequest
I'm writing code to do Xml serialization. With below function. public static string SerializeToXml(object
I constantly find myself writing similar code like the example below: if (object[Object Name]
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Code below is not working as expected to detect if it is in design
The code below shows a sample that I've used recently to explain the different
The code below gives me this mysterious error, and i cannot fathom it. I

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.