gpt4 book ai didi

image-processing - 掩码聚类

转载 作者:太空宇宙 更新时间:2023-11-03 21:22:53 24 4
gpt4 key购买 nike

其实我已经在official Q&A问过这个问题了但还没有得到任何答复。

我的任务是使用kmeans clusterization不是针对整个图像,而是针对它的蒙版部分。所以作为输入我有两张图片:

  1. 蒙版图像。
  2. 图像已转换为 Lab 色彩空间。

如果我在 n 集群上对图像进行聚类,在使用掩码进行集群化之后,我希望图像具有 n+1 个集群(+1 因为掩码)。

当然,我进行了研究并用谷歌搜索,但一无所获。

感谢您的任何建议。

最佳答案

创建另一个图像,复制其中的数据未屏蔽数据,并使用此矩阵执行您的 kmeans。事情是这样的:

[编辑]

以下不起作用,它只会遮蔽掩码中的像素,但 tmp 具有与原始图像相同的尺寸。 简历::垫tmp; labImage.copyTo(tmp, mask);

你应该事先分配 tmp 矩阵,并在掩码上用一个循环填充它:

cv::Mat tmp = cv::Mat::zeros(cv::countNonZero(mask), 1, labImage.type());
int counter = 0;
for (int r = 0; r < mask.rows; ++r)
for (int c = 0; c < mask.cols; ++c)
if (!mask.at<unsigned char>(r, c))
// I assume Lab pixels are stored as a vector of floats
tmp.at<cv::Vec3f>(counter++, 0) = labImage.at<cv::Vec3b>(r, c);

[/编辑]

cv::kmeans(tmp, k, labels);

// Now to compute your image of labels
cv::Mat labelsImage = cv::Mat(labImage.size(), CV_32S, k); // initialize pixel values to K, which is the index of your N+1 cluster

// Now loop through your pixel mask and set the correspondance in your labelImage
int counter = 0;
for (int r = 0; r < mask.rows; ++r)
for (int c = 0; c < mask.cols; ++c)
if (!mask.at<unsigned char>(r, c))
labelsImage.at<int>(r, c) = labels.at<int>(counter++, 0);

关于image-processing - 掩码聚类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12988341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com