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

  • Home
  • SEARCH
  • 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 481363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:59:12+00:00 2026-05-13T00:59:12+00:00

I’m trying to learn how to use OpenCV’s new C++ interface. How do I

  • 0

I’m trying to learn how to use OpenCV’s new C++ interface.

How do I access elements of a multi channel matrix? For example:

Mat myMat(size(3, 3), CV_32FC2);

for (int i = 0; i < 3; ++i)
{
    for (int j = 0; j < 3; ++j)
    {
        //myMat_at_(i,j) = (i,j);
    }
}

What is the easiest way to do this? Something like cvSet2D of the old interface.
What is the most efficient way? Similar to using direct pointers in the old interface.

  • 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-13T00:59:13+00:00Added an answer on May 13, 2026 at 12:59 am
    typedef struct elem_ {
            float f1;
            float f2;
    } elem;
    elem data[9] = { 0.0f };
    CvMat mat = cvMat(3, 3, CV_32FC2, data );
    
    float f1 = CV_MAT_ELEM(mat, elem, row, col).f1;
    float f2 = CV_MAT_ELEM(mat, elem, row, col).f2;
    
    CV_MAT_ELEM(mat, elem, row, col).f1 = 1212.0f;
    CV_MAT_ELEM(mat, elem, row, col).f2 = 326.0f;
    

    Update : for OpenCV2.0

    1. choose one type to represent the element

    Mat (or CvMat) has 3 dimensions: row, col, channel.
    We can access one element (or pixel) in the matrix by specifying the row and col.

    CV_32FC2 means the element is 32bit floating point value with 2 channels.
    So elem in above code is one acceptable representation of CV_32FC2.

    You can use other representations you like. For example :

    typedef struct elem_ { float val[2];    } elem;
    typedef struct elem_ { float x;float y; } elem;
    

    OpenCV2.0 adds some new types to represent the element in the matrix,like :

    template<typename _Tp, int cn> class CV_EXPORTS Vec // cxcore.hpp (208)
    

    So we can use Vec<float,2> to represent CV_32FC2, or use :

    typedef Vec<float, 2> Vec2f; // cxcore.hpp (254)
    

    See the source code to get more type that can represent your element.
    Here we use Vec2f

    2. access the element

    The easiest and efficiant way to access the element in the Mat class is Mat::at.
    It has 4 overloads :

    template<typename _Tp> _Tp& at(int y, int x);                // cxcore.hpp (868)
    template<typename _Tp> const _Tp& at(int y, int x) const;    // cxcore.hpp (870)
    template<typename _Tp> _Tp& at(Point pt);                    // cxcore.hpp (869)
    template<typename _Tp> const _Tp& at(Point pt) const;        // cxcore.hpp (871)
    // defineded in cxmat.hpp (454-468)
    
    // we can access the element like this :
    Mat m( Size(3,3) , CV_32FC2 );
    Vec2f& elem = m.at<Vec2f>( row , col ); // or m.at<Vec2f>( Point(col,row) );
    elem[0] = 1212.0f;
    elem[1] = 326.0f;
    float c1 = m.at<Vec2f>( row , col )[0]; // or m.at<Vec2f>( Point(col,row) );
    float c2 = m.at<Vec2f>( row , col )[1];
    m.at<Vec2f>( row, col )[0] = 1986.0f;
    m.at<Vec2f>( row, col )[1] = 326.0f;
    

    3. interact with old interface

    Mat provides 2 conversion functions:

    // converts header to CvMat; no data is copied     // cxcore.hpp (829)
    operator CvMat() const;                            // defined in cxmat.hpp
    // converts header to IplImage; no data is copied
    operator IplImage() const;
    
    // we can interact a Mat object with old interface :
    Mat new_matrix( ... );
    CvMat old_matrix = new_matrix;  // be careful about its lifetime
    CV_MAT_ELEM(old_mat, elem, row, col).f1 = 1212.0f;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 230k
  • Answers 230k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Still not sure what the error actually means, but the… May 13, 2026 at 2:00 am
  • Editorial Team
    Editorial Team added an answer This is done using media types. With them, you can… May 13, 2026 at 2:00 am
  • Editorial Team
    Editorial Team added an answer Found it, with extensive use of RSeek. The sink() function… May 13, 2026 at 2:00 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.