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

后端 未结 2 594
孤街浪徒
孤街浪徒 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: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) );
    

提交回复
热议问题