c++ - 为什么 Mat.forEach 不会改变自己?

标签 c++ algorithm opencv

基本上,我尝试使用较少的像素来表示图像本身。

步骤如下:

  1. 假设我将输入一个大小为 [1000*600] 的图像,然后我得到 600_000 像素 (rgb),这可能是 [600_000, 3] 个 vector 。 K-Means 用于获取其聚类中心。

  2. 图像中的每个像素将放置在通过 K-Means 找到的簇中最近的邻居。

来源是:

template <typename T>
void NN(Point3_<T>& pixel, const Mat& points)
{
    vector<T> vt {pixel.x, pixel.x, pixel.z};
    double min_dist = LDBL_MAX;
    int min_index = -1;
    for (int i = 0; i < points.rows; ++ i)
    {
        double dist = norm(vt, points.row(i), NORM_L2);
        if (dist < min_dist) 
        {
            min_dist = dist;
            min_index = i;
        }
    }
    // assert(min_index != -1);
    pixel.x = points.at<T>(min_index, 0);
    pixel.y = points.at<T>(min_index, 1);
    pixel.z = points.at<T>(min_index, 2);
}

template <typename T>
void NN(Mat& img, const Mat& points)
{
    timer::start("assign");
    img.forEach<Point3_<T>>([&points](Point3_<T> &pixel, const int position[])
        {
            NN(pixel, points);
        });
    timer::stop<ms>();
}

Mat kmeans(const Mat& original_img, const int K)
{
    Mat img;
    original_img.reshape(3, original_img.rows * original_img.cols)
        .convertTo(img, CV_32FC3);

    timer::start("K-means cluster");
    // Require img.type() == CV_32F
    Mat clusters = BOWKMeansTrainer(K).cluster(img);
    timer::stop<ms>();

    // Type 5 -> Type 0: 32FC1 -> 8UC1
    // K rows, 3 cols, 8UC1
    clusters.convertTo(clusters, CV_8UC1);
    Mat output_img = original_img;
    NN<uchar>(output_img, clusters);

    // assert won't fire, why?
    assert(equal(original_img.begin<uchar>(), original_img.end<uchar>(),
        output_img.begin<uchar>()));

    return output_img;
}

int main(int argc, char* argv[])
{   
    vector<int> ks {2, 16};
    string filename = "1";
    string pathname = string("./img/") + filename + ".jpg";

    Mat img = imread(pathname);
    for (const int& K: ks)
    {
        imshow(int_to_string(K), kmeans(img, K));
        // write_img(filename, "kmeans", K, kmeans(img, K));
    }

    std::cout << "Press enter to continue...";
    cin.get();
}

问题是:

  1. kmeans() 中的 assert() 不会触发。也就是说,mat 对象 original_imgoutput_img 相同。怎么会这样?

  2. main() 中的两个 imwrite() 将显示两个相同的 2 值图像。也就是说,K=2 的 K-Means 有效,而 K=16 的 K-Means 。请注意,如果我们每次执行输出一张图像,一切都很好。

错误输出如下:

The buggy output

原始图像和 K=16 的 K-Means 如下所示:

The original image

K-Means with K=16

最佳答案

感谢上帝!我找到了原因。

在 kmeans() 中,下面的代码将调用 Mat 的复制构造函数,它花费 O(1) 将 original_img 的 header 分配给 output_img 的。

Mat output_img = original_img;

这就是断言不会触发的原因。

关于c++ - 为什么 Mat.forEach 不会改变自己?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34921069/

相关文章:

algorithm - 找出能容纳另一个矩形的面积最小的矩形

python - 如何精确求解具有大整数系数(超过整数)的二次方程?

python - 使用 python 和 opencv 显示完整图像时出现白色边框

python - 使用opencv和python清理图像背景后如何正确提取字母?

c++ - QFileSystem entryInfo 不返回完整的目录名称

c++ - 作为左值引用或右值拷贝的数据成员

c++ - 加载DLL和直接调用的区别

c++ - 如何计算 OpenCV C++ 中按列聚合的亮度直方图

algorithm - 多个商店和元素的最佳购买策略

node.js - 如何使用node-gyp安装node_modules?