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

读入的原图:

luffy.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,053评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,527评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,779评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,685评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,699评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,609评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,989评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,654评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,890评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,634评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,716评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,394评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,976评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,950评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,191评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,849评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,458评论 2 342