簡體   English   中英

獲取 OpenCV Mat 中唯一像素值的列表

[英]Obtaining list of unique pixel values in OpenCV Mat

OpenCV Mat是否有等效於np.unique()bincount() 我正在使用 C++,所以不能只轉換為numpy數組。

不,那里沒有! 不過,您可以編寫自己的代碼:

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

查找單個通道 cv::Mat 的唯一元素。

參數:

input :它將被視為一維。

sort :對唯一值進行排序(可選)。

此類函數的實現非常簡單,但是,以下僅適用於單通道CV_32F

#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;
}

例子:

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;

輸出:

1 3 5 7 9 30 50 70 90 

您可以嘗試構建一個直方圖,其 bin 數量等於可能的像素值數量。

這是使用標准庫實現的另一個建議。

opencv-unique.cpp

#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstdint>

/**
 * @brief Find unique elements of an OpenCV image
 *
 * @tparam type is the C++ type to access the image elements.
 * @param in is the OpenCV single-channel image to find the unique values. Note: This
 * modifies the image. Make a copy with .clone(), if you need the image afterwards.
 *
 * @returns vector of unique elements
 */
template<typename type>
std::vector<type> unique(cv::Mat in) {
    assert(in.channels() == 1 && "This implementation is only for single-channel images");
    auto begin = in.begin<type>(), end = in.end<type>();
    auto last = std::unique(begin, end);    // remove adjacent duplicates to reduce size
    std::sort(begin, last);                 // sort remaining elements
    last = std::unique(begin, last);        // remove duplicates
    return std::vector<type>(begin, last);
}

int main() {
    cv::Mat img = (cv::Mat_<uint16_t>(3, 4) << 1, 5, 3, 4, 3, 1, 5, 5, 1, 3, 4, 3);

    std::cout << "unique values: ";
    auto u = unique<uint16_t>(img);
    for (auto v : u)
        std::cout << v << " ";
    std::cout << std::endl;

    return 0;
}

編譯並執行產生:

$ g++ -Wall opencv-unique.cpp -o unique -lopencv_core -I /usr/include/opencv4 && ./unique
unique values: 1 3 4 5

上面的版本適用於單通道圖像。 您可以將其擴展到多通道圖像(以獲得獨特的顏色),如下所示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM