OpenCV cv::Mat 'ones' for multi-channel matrix?

后端 未结 2 596
孤街浪徒
孤街浪徒 2021-01-17 11:34

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).

相关标签:
2条回答
  • 2021-01-17 12:23

    You can also initialize like this:

    Mat img;
    
    /// Lots of stuff here ...
    
    // Need to initialize again for some reason:
    img = Mat::Mat(Size(width, height), CV_8UC3, CV_RGB(255,255,255));
    
    0 讨论(0)
  • 2021-01-17 12:28

    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 讨论(0)
提交回复
热议问题