Listing Unique Pixel Values ​​in OpenCV Mat

Is there an equivalent of np.unique() or bincount() for OpenCV Mat ? I work with C ++, so I can’t just convert to a numpy array.

+7
c ++ arrays matrix opencv computer-vision
source share
2 answers

No does not exist! . You can write your own code:

 std::vector<float> unique(const cv::Mat& input, bool sort = false) 

Find the unique elements of one channel cv :: Mat.

Options:

input: it will be processed as if it were 1-D.

sort: sorts unique values ​​(optional).

The implementation of such a function is fairly straightforward, but the following only works with a single CV_32F channel :

 #include <algorithm> #include <vector> std::vector<float> unique(const cv::Mat& input, bool sort = false) { if (input.channels() > 1 || input.type() != CV_32F) { std::cerr << "unique !!! Only works with CV_32F 1-channel Mat" << std::endl; return std::vector<float>(); } std::vector<float> out; for (int y = 0; y < input.rows; ++y) { const float* row_ptr = input.ptr<float>(y); for (int x = 0; x < input.cols; ++x) { float value = row_ptr[x]; if ( std::find(out.begin(), out.end(), value) == out.end() ) out.push_back(value); } } if (sort) std::sort(out.begin(), out.end()); return out; } 

Example:

 float data[][3] = { { 9.0, 3.0, 7.0 }, { 3.0, 9.0, 3.0 }, { 1.0, 3.0, 5.0 }, { 90.0, 30.0, 70.0 }, { 30.0, 90.0, 50.0 } }; cv::Mat mat(3, 5, CV_32F, &data); std::vector<float> unik = unique(mat, true); for (unsigned int i = 0; i < unik.size(); i++) std::cout << unik[i] << " "; std::cout << std::endl; 

Outputs:

 1 3 5 7 9 30 50 70 90 
+12
source share

You can try to build a histogram with the number of boxes equal to the number of possible pixel values.

+2
source share

All Articles