Functions:
preprocess 預處理圖片,resize, [0, 1], normalize, pass to float arraycvtArray2Mat 將 float array 存放的數據再存為 cv::MatchannelArgMax 取每個 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.resizecv::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_stdsvector<cv::Mat> channels;cv::split(src, channels);cv::Scalar means, stds;for (int i = 0; i < 3; ++i) {cv::Mat a = channels[i]; // bcv::meanStdDev(a, means, stds);a = a / stds.val[0] * norm_stds[i]; // change std, mean also changemeans = cv::mean(a); // recompute mean!a = a - means.val[0] + norm_means[i];channels[i] = a;}// R,G,B. split channel testprintf("%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,Bfor (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->Bindex++;}}}// R,G,B. float array testint 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) {// reshapecv::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,Bindex++; // update STEP times}}// R,G,B. recover Mat testcout << 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 pointout.at<uchar>(h, w) = (uchar) argmax(p, p + 3);}}return out;
}int main() {// preprocess input and pass to datacv::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 Matcv::Mat out = cvtArray2Mat(input);// channel argmaxout = channelArgMax(out);// resize to print predicted resultcv::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
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
總結
以上是生活随笔為你收集整理的cv::Mat 与 float 互换,实现 argmax 得到像素点分类的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。