learning
learning

Reputation: 25

How to find element in matrix using Opencv

I am novice in OpenCV. I want to use equality operator between a vector element and matrix to cross check where both values are equal or not. How do that?

Thanks in advance

Upvotes: 0

Views: 1421

Answers (1)

bweber
bweber

Reputation: 4092

Im not sure if I understand your question correctly, but if you just want to compare one element of the vector to one element of the matrix this could be done in the following way:

cv::Vec3b vec(1, 2, 3);
cv::Mat mat(2, 2, CV_8UC1);
if (vec[0] == mat.at<uchar>(2, 1)) {
    ...
}

This compares the first element of a uchar vector to the second-row, first-column element of a uchar matrix.

If you want to iterate over the matrix, do it the following way:

cv::Mat mat(2, 2, CV_8UC1);
uchar* ptr;
for (int row = 0; row < mat.rows; ++row) {
    ptr = mat.ptr<uchar>(row);
    for (int col = 0; cols < mat.cols; ++col) {
        if(ptr[col] == ...)
    }
}

EDIT: equivalent for float, just exchange uchar for float:

cv::Vec3f vec(1, 2, 3);
cv::Mat mat(2, 2, CV_32FC1);
if (vec[0] == mat.at<float>(2, 1)) {
    ...
}

And:

cv::Mat mat(2, 2, CV_32FC1);
float* ptr;
for (int row = 0; row < mat.rows; ++row) {
    ptr = mat.ptr<float>(row);
    for (int col = 0; cols < mat.cols; ++col) {
        if(ptr[col] == ...)
    }
}

If you have multiple channels ptr[col] returns not one value but an OpenCV vector of the matrix' data type and as many dimensions as you've got channels. You can also directly add another [] operator with the index of the channel you want to access:

if(ptr[col][channel] == ...)

How to find out the data type of your cv::Mat?

From the type and depth specifier of your matrix you can see what data type they store:

  • CV_8U - 8-bit unsigned integers ( 0..255 )
  • CV_8S - 8-bit signed integers ( -128..127 )
  • CV_16U - 16-bit unsigned integers ( 0..65535 )
  • CV_16S - 16-bit signed integers ( -32768..32767 )
  • CV_32S - 32-bit signed integers ( -2147483648..2147483647 )
  • CV_32F - 32-bit floating-point numbers ( -FLT_MAX..FLT_MAX, INF, NAN )
  • CV_64F - 64-bit floating-point numbers ( -DBL_MAX..DBL_MAX, INF, NAN )

These are the depth specifiers for matrices. You can find out the depth of your matrix by calling cv::Mat::depth(). They specify which data type one element has. The type specifiers you use when creating a matrix also contain the information how many channels the matrix shall have, just add Cx to the depth specifier, x being the amount of channels , e.g. CV_8UC3 would be a matrix with three channels and 8bit unsigned chars as data type (so a pretty normal 8bit image). This information can be obtained from an existing matrix by calling cv::Mat::type(). The amount of channels is returned by cv::Mat::channels().

For OpenCV Vectors those type specifiers are similar:

typedef Vec<uchar, 2> Vec2b;
typedef Vec<uchar, 3> Vec3b;
typedef Vec<uchar, 4> Vec4b;

typedef Vec<short, 2> Vec2s;
typedef Vec<short, 3> Vec3s;
typedef Vec<short, 4> Vec4s;

typedef Vec<int, 2> Vec2i;
typedef Vec<int, 3> Vec3i;
typedef Vec<int, 4> Vec4i;

typedef Vec<float, 2> Vec2f;
typedef Vec<float, 3> Vec3f;
typedef Vec<float, 4> Vec4f;
typedef Vec<float, 6> Vec6f;

typedef Vec<double, 2> Vec2d;
typedef Vec<double, 3> Vec3d;
typedef Vec<double, 4> Vec4d;
typedef Vec<double, 6> Vec6d;

Upvotes: 1

Related Questions