Access opencv matrix CV_32S element

20,814

Solution 1

The general rule for Matrices typenames in OpenCV is:

 CV_<bit_depth>(S|U|F)C<number_of_channels>

S = Signed integer
U = Unsigned integer
F = Float 

So depending on which one of the previous letters (S,U,F) you have, you will be casting <int>, <unsigned integer> or <float>.

Solution 2

CV_32SC1 is a 1 Channel of Signed 32 bit integer, then I think that X.at<int>() should do.

Mat already 'knows' how to address a pixel, the type just cast the bits to the C++ value you require for the expression evaluation.

I found here some explanation about the notation.

Share:
20,814
Robert Kirchhoff
Author by

Robert Kirchhoff

Updated on July 09, 2022

Comments

  • Robert Kirchhoff
    Robert Kirchhoff almost 2 years

    If I have a matrix of type CV_32SC1, what typename should I use in function Mat::at?

    e.g.

    Mat X;  // for example eye matrix of size 10,10,and type CV_32SC1
    X.at<??????>(1,1)=5;
    

    How can I find the typename for other matrix types?

  • Mick MacCallum
    Mick MacCallum over 11 years
    Please actually read the reviews you are clicking approve on.
  • Martin R.
    Martin R. almost 8 years
    In this case I would prefer <int32_t> to <int>, as the C++ standard does not specify the bit-size of integral types.
  • greggo
    greggo about 7 years
    Not quite true. with at<T>, the addressing in x direction is based entirely on the size of T . If your matrix is CV_8UC3, then X.at<cv::Vec3b>(0,2) will get you the third pixel on the first row; but X.at<uint8_t>(0,2*3+1) will give you the second channel of the same pixel (i.e the same byte as X.at<cv::Vec3b>(0,2)[1] Because of this, the pitch in x direction is based on a compile-time constant sizeof(T) rather than on a value calculated from the type stored in the Mat object, which makes a considerable difference in performance. It also supports 'flat' view of multi-channel arrs.
  • greggo
    greggo about 7 years
    There is no CV_32U* type. But you can still use X.at<uint32_t>(i,j) if your CV_32SC1 array actually contains unsigned values.