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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:30:11+00:00 2026-06-11T03:30:11+00:00

When working with 1-channel (e.g. CV_8UC1 ) Mat objects in OpenCV, this creates a

  • 0

When working with 1-channel (e.g. CV_8UC1) Mat objects in OpenCV, this creates a Mat of all ones: cv::Mat img = cv::Mat::ones(x,y,CV_8UC1).

However, when I use 3-channel images (e.g. CV_8UC3), things get a little more complicated. Doing cv::Mat img = cv::Mat::ones(x,y,CV_8UC3) puts ones into channel 0, but channels 1 and 2 contain zeros. So, how do I use cv::Mat::ones() for multi-channel images?

Here’s some code that might help you to see what I mean:

void testOnes() {
 int x=2; int y=2; //arbitrary

 // 1 channel
 cv::Mat img_C1 = cv::Mat::ones(x,y,CV_8UC1);
 uchar px1 = img_C1.at<uchar>(0,0); //not sure of correct data type for px in 1-channel img
 printf("px of 1-channel img: %d \n", (int)px1); //prints 1

 // 3 channels
 cv::Mat img_C3 = cv::Mat::ones(x,y,CV_8UC3); //note 8UC3 instead of 8UC1
 cv::Vec3b px3 = img_C3.at<cv::Vec3b>(0,0);
 printf("px of 3-channel img: %d %d %d \n", (int)px3[0], (int)px3[1], (int)px3[2]); //prints 1 0 0
}

So, I would have expected to see this printout: px of 3-channel img: 1 1 1, but instead I see this: px of 3-channel img: 1 0 0.

P.S. I did a lot of searching before posting this. I wasn’t able to resolve this by searching SO for “[opencv] Mat::ones” or “[opencv] +mat +ones”.

  • 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-11T03:30:12+00:00Added an answer on June 11, 2026 at 3:30 am

    I don’t use OpenCV, but I believe I know what’s going on here. You define a data-type, but you are requesting the value ‘1’ for that. The Mat class appears not to pay attention to the fact that you have a multi-channel datatype, so it simply casts ‘1’ as a 3-byte unsigned char.

    So instead of using the ones function, just use the scalar constructor:

    cv::Mat img_C3( x, y, CV_8UC3, CV_RGB(1,1,1) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get the channel api working. This is what I have
I am using this autocomplete Plugin working with Jquery: jQuery Autocomplete Mod You use
I'm working with QT and OpenCV, I have this square that I need to
I am working with images in C++ with OpenCV. I wrote code with an
I'm currently working on a irc bot for a multi-lingual channel, and I'm encountering
Working with H2 I get this error when I try to write a row
Been working on this problem of collision detection and there appears to be 3
I have some working code that uses IImgCtx to load images, but I can't
I am working through the book Learning OpenCV from the O'Reilly series and am
I added a database writer destination to a working mirth channel. The destination is

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.