【C++】【Opencv】多分类语义分割结果覆盖在原图上

如题,对于多分类的输出结果想要可视化出来的话,需要覆盖在原图上,具体的C++代码实现如下:


    cv::Mat image = cv::imread("your_img_path",1);
    int height_ori = image.rows;
    int width_ori = image.cols;
    std::string save_result_path = "your_path\\";
    std::string name = "your_name";
    int num_classes = 3;
    for (int k = 0; k < num_classes; k++)
    {
        int integer_number = k / 3;
        cv::Mat ImageResult = cv::Mat(height_ori, width_ori, CV_8UC1);
        memcpy(ImageResult.data, result+ k*height_ori* width_ori, height_ori * width_ori * sizeof(unsigned char));
        cv::cvtColor(ImageResult, ImageResult, CV_GRAY2BGR);
        for (int h = 0; h < height_ori; h++)
        {
            for (int w = 0; w < width_ori; w++)
            {
                ImageResult.at<Vec3b>(h, w)[k%3] /= (integer_number + 1);
                ImageResult.at<Vec3b>(h, w)[((k % 3) + 1) % 3] /= 255;
                ImageResult.at<Vec3b>(h, w)[((k % 3) + 1) % 3] *=(255 - 255 / (integer_number + 1));
                ImageResult.at<Vec3b>(h, w)[((k % 3) + 2) % 3] = 0;
            }
        }
        addWeighted(image, 1, ImageResult, 0.2, 0, image, -1);

    }
    cv::imwrite(save_result_path + name + "_result.jpg", image);

假如说本人有9分类的话,而且每个分类结果图最大像素值是255,则输出的9张结果图的像素最大值分别是
第1张:(255,0,0);
第2张:(0,255,0);
第3张:(0,0,255);
第4张:(127,128,0);
第5张:(0,127,128);
第6张:(128,0,127);
第7张:(85,170,0);
第8张:(0,85,170);
第9张:(170,0,85);

然后在循环里面不断使用addweighted()函数来在原图上叠加图像就行。

Original: https://blog.csdn.net/qq_42944019/article/details/126280546
Author: 少有人走的路_心智旅程
Title: 【C++】【Opencv】多分类语义分割结果覆盖在原图上

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/702042/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球