cv::Mat 与 float 互换,实现 argmax 得到像素点分类

Functions:

  1. preprocess 预处理图片,resize, [0, 1], normalize, pass to float array
  2. cvtArray2Matfloat array 存放的数据再存为 cv::Mat
  3. channelArgMax 取每个 channel 数组中最大值下标作为预测种类,use argmax

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace std;
using namespace cv;

// define an array
static const float norm_means[] = {0.406, 0.456, 0.485}; // src
static const float norm_stds[] = {0.225, 0.224, 0.229};

static const int INPUT_H = 256;
static const int INPUT_W = 320;
static const int INPUT_C = 3;
static const int STEP = INPUT_H * INPUT_W;

// test sample test_pt
const Point2i test_pt(0, 0); // x,y

// 数组长度
template<class T>
int getArrayLen(T &array) {
    return (sizeof(array) / sizeof(array[0]));
}

// 数组最大值下标
template<class ForwardIterator>
inline int argmax(ForwardIterator first, ForwardIterator last) {
    return std::distance(first, std::max_element(first, last));
}

void preprocess(cv::Mat src, float *data) {
    // 1.resize
    cv::resize(src, src, cv::Size(INPUT_W, INPUT_H), cv::INTER_NEAREST);

    // 2.uchar->CV_32F, scale to [0,1]
    src.convertTo(src, CV_32F);
    src /= 255.0;

    // 3.split R,G,B and normal each channel using norm_means,norm_stds
    vector<cv::Mat> channels;
    cv::split(src, channels);
    cv::Scalar means, stds;
    for (int i = 0; i < 3; ++i) {
        cv::Mat a = channels[i]; // b
        cv::meanStdDev(a, means, stds);
        a = a / stds.val[0] * norm_stds[i]; // change std, mean also change
        means = cv::mean(a); // recompute mean!
        a = a - means.val[0] + norm_means[i];
        channels[i] = a;
    }

    // R,G,B. split channel test
    printf("%f, %f, %f\n", channels[2].at<float>(test_pt),
           channels[1].at<float>(test_pt), channels[0].at<float>(test_pt));

    // 4.pass to data, ravel()
    int index = 0;
    for (int c = 2; c >= 0; --c) { // R,G,B
        for (int h = 0; h < INPUT_H; ++h) {
            for (int w = 0; w < INPUT_W; ++w) {
                data[index] = channels[c].at<float>(h, w); // R->G->B
                index++;
            }
        }
    }

    // R,G,B. float array test
    int idx = INPUT_W * test_pt.y + test_pt.x;
    printf("%f, %f, %f\n", data[idx], data[idx + STEP], data[idx + STEP * 2]);

}

cv::Mat cvtArray2Mat(const float *data) {
    // reshape
    cv::Mat out = cv::Mat::zeros(INPUT_H, INPUT_W, CV_32FC3);
    int index = 0;
    for (int h = 0; h < INPUT_H; ++h) {
        for (int w = 0; w < INPUT_W; ++w) {
            out.at<Vec3f>(h, w) = {data[index], data[index + STEP], data[index + STEP * 2]}; // R,G,B
            index++; // update STEP times
        }
    }
    // R,G,B. recover Mat test
    cout << out.at<Vec3f>(test_pt) << endl;
    return out;
}


cv::Mat channelArgMax(cv::Mat src) {
    cv::Mat out = cv::Mat::zeros(INPUT_H, INPUT_W, CV_8U);
    for (int h = 0; h < INPUT_H; ++h) {
        for (int w = 0; w < INPUT_W; ++w) {
            uchar *p = src.ptr(h, w); // prob of a point
            out.at<uchar>(h, w) = (uchar) argmax(p, p + 3);
        }
    }
    return out;
}


int main() {

    // preprocess input and pass to data
    cv::Mat src = cv::imread("/Users/shuai/CLionProjects/CV/CVTest/luffy.jpg");

    float input[INPUT_C * INPUT_H * INPUT_W]; // C,H,W;
    preprocess(src, input); // pass src -> input

    // float array -> float Mat
    cv::Mat out = cvtArray2Mat(input);

    // channel argmax
    out = channelArgMax(out);
    
    // resize to print predicted result
    cv::resize(out, out, cv::Size(100, 40), cv::INTER_NEAREST);
    for (int h = 0; h < 40; ++h) {
        for (int w = 0; w < 100; ++w) {
            cout << (int) out.at<uchar>(h, w);
        }
        cout << endl;
    }

    return 0;
}

0.699532, 0.703471, 0.686600
0.699532, 0.703471, 0.686600
[0.699532, 0.703471, 0.6866]
0000000000000000000000000000001012111022111111110111122212121022111120110000000000000000000000000000
0000000000000000000000000000111011010021120110111220212202001202111222210000000000000000000000000000
0000000000000000000000000011000011001111211100111222211112200110222212222201000000000000000000000000
0000000000000000000000000020202100022000100002020001122000220101022011022221010000000000000000000000
0000000000000000000000001011010110110011211211121122111212022221222110121121211100000000000000000000
0000000000000000000000110210001022221112111110211211111102221112210202222112010110000000000000000000
0000000000000000000010111012222202022112001020101121222112221221222212022202211111000000000000000000
0000000000000000011110222122212211221221111110012212112211122222222222121221222222110000000000000000
0000000000000000101222112021220211211221112112112221211012222021221122121210102221211200000000000000
0000000000000011011120212112112012221112100020200021122101100121211221212111222221212211100000000000
0000000000001101121112211111210121101211100001100000112111111211022122211111101121122211110000000000
0000000000011102021200211022210211101010100000110000012210211112221112211101011201212221222000000000
0000000000102212212121011111120112111111101000111101010211111111101222222111112201111222121110000000
0000000011012222112010101121122122200000122221010000011111221100201020212220202201112022222000000000
0000000010211110111011211122111120110111210212200100112101112121121112221122211110101211121111000000
0000000111012210121110112201101010010110100111010001120001010111111022010221111120110121121011000000
0000000012211122210021122221100000000010000010101001211000001021100011212002101221020021121120000000
0000000012001121112111120112111011110101012110111011121011210011112122111022022101101111021210000000
0000000002211001110102022201221101111011000000122020201111001211211100010001022221112011111000000000
0000000111221000121011101111021111121110110010111101111102212111110121011112110111110000010111100000
0001021000010111221210210211111000011011100000000010000000001110000000100120111000111111000000110100
1101000010121222121221222100000000000000000000000000000000001100000000000210110101210201211000010012
0111011222222111112111112101001100000000000000000010000000000000000100012221121112221122101010212221
1101011121011222211201110010110200000011011001111120111111101000000010112111211122021211100101012112
1111001222211102221222200221021111110111100000000101000000001110000102202112012211111101101011022222
0010112222001102222211002210021110000100000000000010000000000100000010112112121211221210010000101112
0011121122100101222001012212111112100010000000001010000000000110011001121221210010210110000101010012
1011111212111221222201000111102211000011011101111111101111110110012221111100000111000122211111122222
0000001010011222211100000000000001212212221222100000000012202222211000000000000011221110100000022211
1011221121212222111000000000000000000000111102101211111101110000000000000000000001110000001122222212
2121111111111211200000000000000000000000000210121211112111110000000000000000000000111112111001011011
0000000000000000000000000000000000000000000122111020021111110000000000000000000000001000000111010011
0121110111111111111111110000010000010000002110221211120111110000010000102101221221221101111101100111
0100000111101110010000100002221212210210101110000221120022210101201001101122111222001111111111111101
1111101111111101001100010012110101122110122110011021221102221111110010111122222001100011101110100000
1000000101100111100000000211121110022111120010111010112221011112101111101011001110110010010001101111
2022211010100000001000110222222222122102000101100010000202210011102101001211010001110010211011012111
1111011121112112210001112111212211122111111121100011111211111111111011111011121122221222211010000000
0000000000100220212222220212112012022222011020201021022210002220101111011111222222100110000000000000
0000000000000000111121211212121212121102211101111122121000112011112112221111121111000000000000000000

读入的原图:


作者:谢小帅
链接:https://www.jianshu.com/p/82199c4f7b65
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值