我们可以使用 openCV 加载具有索引颜色的 2d 图像并调整其大小吗?

发布于 2024-12-17 19:32:47 字数 1028 浏览 2 评论 0原文

因此,我有一个图像 cv::Mat 创建为索引 2D 矩阵,颜色为 1,2,3,... 最多 255。我想一次性调整图像大小,但这样做就像我目前所做的那样 - 分别为每个索引,以免获得混合颜色:

//...
std::map<unsigned char , cv::Mat* > clusters;
for(int i = 0; i < sy; ++i)
{
    for(int j = 0; j < sx; ++j)
    {
        unsigned char current_k = image[i][j];
        if (clusters[current_k] == NULL) {
            clusters[current_k] = new cv::Mat();
            (*clusters[current_k]) = cv::Mat::zeros(cv::Size(sx, sy), CV_8UC1);
        }
        (*clusters[current_k]).row(i).col(j) = 255;
    }
}

std::vector<cv::Mat> result;
for( std::map<unsigned char, cv::Mat*>::iterator it = clusters.begin(); it != clusters.end(); ++it )
{
    cv::Mat filled(cv::Size(w, h), (*it->second).type());
    cv::resize((*it->second), filled, filled.size(), 0,0, CV_INTER_CUBIC);

    cv::threshold( filled, filled, 1, 255, CV_THRESH_BINARY);
    result.push_back(filled);
}

那么,OpenCV 可以帮助我实现索引图像的自动化(这样我就无法为每个索引创建 cv::Mat每个簇都有一个正确的调整大小)?

So, I have an image cv::Mat created as an indexed 2D matrix with colors 1,2,3,... up to 255. I want to resize my image all at once but do it like I currently do - individually for each index, so as not to get mixed colors:

//...
std::map<unsigned char , cv::Mat* > clusters;
for(int i = 0; i < sy; ++i)
{
    for(int j = 0; j < sx; ++j)
    {
        unsigned char current_k = image[i][j];
        if (clusters[current_k] == NULL) {
            clusters[current_k] = new cv::Mat();
            (*clusters[current_k]) = cv::Mat::zeros(cv::Size(sx, sy), CV_8UC1);
        }
        (*clusters[current_k]).row(i).col(j) = 255;
    }
}

std::vector<cv::Mat> result;
for( std::map<unsigned char, cv::Mat*>::iterator it = clusters.begin(); it != clusters.end(); ++it )
{
    cv::Mat filled(cv::Size(w, h), (*it->second).type());
    cv::resize((*it->second), filled, filled.size(), 0,0, CV_INTER_CUBIC);

    cv::threshold( filled, filled, 1, 255, CV_THRESH_BINARY);
    result.push_back(filled);
}

So, can OpenCV help me with the automation of my indexed image (so that I could not create cv::Mat per each cluster for a correct resize)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素手挽清风 2024-12-24 19:32:47

您可以使用 Remap 函数与您自己的混搭来插入值,请

查看本教程(链接)

you can use the Remap function with your own mash to interpolate the values as you'de like

take a look at this tutorial (Link)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文