解讀opencv->tensorrt的數(shù)據(jù)排列方式
opencv->tensorrt的數(shù)據(jù)排列
在使用tensorrt的時(shí)候,將opencv的Mat格式處理成tensorrt輸入方式。
中間還有圖像預(yù)處理,包括通道的變換、圖像尺寸變形,最重要的是如何將Mat格式處理成tensorrt 可接受的輸入形式。
opencv的Mat中BGR形式的數(shù)據(jù)排列是這樣的:
b(0,0), g(0,0), r(0,0), b(0,1), g(0,1), r(0,1),......, b(h-1,w-1), g(h-1,w-1), r(h-1,w-1)
tensorrt所需要的數(shù)據(jù)排列是這樣的:
b(0,0), b(0,1),..., b(h-1,w-1), g(0,0), g(0,1)..., g(h-1,w-1), r(0,0),r(0,1), ..., r(h-1,w-1)
可見(jiàn),需要將Mat的數(shù)據(jù)通道分離,重新排列,才能為tensorrt所用。
下面摘錄兩種常見(jiàn)的處理方法。
1.使用split函數(shù)分離通道
std::vector<float> prepareImage(std::vector<cv::Mat> &vec_img) {
std::vector<float> result(BATCH_SIZE * IMAGE_WIDTH * IMAGE_HEIGHT * INPUT_CHANNEL);
float *data = result.data();
int index = 0;
for (const cv::Mat &src_img : vec_img)
{
if (!src_img.data)
continue;
float ratio = float(IMAGE_WIDTH) / float(src_img.cols) < float(IMAGE_HEIGHT) / float(src_img.rows) ? float(IMAGE_WIDTH) / float(src_img.cols) : float(IMAGE_HEIGHT) / float(src_img.rows);
cv::Mat flt_img = cv::Mat::zeros(cv::Size(IMAGE_WIDTH, IMAGE_HEIGHT), CV_8UC3);
cv::Mat rsz_img;
cv::resize(src_img, rsz_img, cv::Size(), ratio, ratio);
rsz_img.copyTo(flt_img(cv::Rect(0, 0, rsz_img.cols, rsz_img.rows)));
flt_img.convertTo(flt_img, CV_32FC3, 1.0 / 255);
//HWC TO CHW
int channelLength = IMAGE_WIDTH * IMAGE_HEIGHT;
std::vector<cv::Mat> split_img = {
cv::Mat(IMAGE_HEIGHT, IMAGE_WIDTH, CV_32FC1, data + channelLength * (index + 2)), // Mat與result數(shù)據(jù)區(qū)域共享
cv::Mat(IMAGE_HEIGHT, IMAGE_WIDTH, CV_32FC1, data + channelLength * (index + 1)),
cv::Mat(IMAGE_HEIGHT, IMAGE_WIDTH, CV_32FC1, data + channelLength * index)
};
index += 3;
cv::split(flt_img, split_img); // 通道分離
}
return result; // result與split_img的數(shù)據(jù)共享
}
2.手撕通道,暴力直接
std::vector<float> prepareImage(std::vector<cv::Mat> &vec_img) {
std::vector<float> result(BATCH_SIZE * INPUT_CHANNEL * IMAGE_HEIGHT * IMAGE_WIDTH);
float* data = result.data();
int index = 0;
int offset = IMAGE_WIDTH * IMAGE_HEIGHT;
// 數(shù)據(jù)預(yù)處理
for(const auto &img:vec_img){
float ratio = IMAGE_WIDTH/float(img.cols) < IMAGE_HEIGHT/float(img.rows) ? IMAGE_WIDTH/float(img.cols) : IMAGE_HEIGHT/float(img.rows);
cv::Mat rsz_img;
cv::resize(img, rsz_img, cv::Size(), ratio, ratio, 0);
cv::Mat flt_img(IMAGE_HEIGHT, IMAGE_WIDTH, CV_8UC3, cv::Scalar(128, 128, 128));
rsz_img.copyTo(flt_img(cv::Rect(0, 0, rsz_img.cols, rsz_img.rows)));
// 手撕三通道,填入data
int i = 0;
for (int row = 0; row < IMAGE_HEIGHT; ++row) {
uchar* uc_pixel = flt_img.data + row * flt_img.step;
for (int col = 0; col < IMAGE_WIDTH; ++col) {
data[index * 3 * IMAGE_HEIGHT * IMAGE_WIDTH + i] = (float)uc_pixel[2]/255.;
data[index * 3 * IMAGE_HEIGHT * IMAGE_WIDTH + i + IMAGE_HEIGHT * IMAGE_WIDTH] = (float)uc_pixel[1]/255.;
data[index * 3 * IMAGE_HEIGHT * IMAGE_WIDTH+ i + 2 * IMAGE_HEIGHT * IMAGE_WIDTH] = (float)uc_pixel[0]/255.;
uc_pixel += 3;
++i;
}
}
index++;
}
return result;
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
django實(shí)現(xiàn)將后臺(tái)model對(duì)象轉(zhuǎn)換成json對(duì)象并傳遞給前端jquery
這篇文章主要介紹了django實(shí)現(xiàn)將后臺(tái)model對(duì)象轉(zhuǎn)換成json對(duì)象并傳遞給前端jquery,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Python實(shí)現(xiàn)提取和去除數(shù)據(jù)中包含關(guān)鍵詞的行
這篇文章主要介紹了Python如何提取數(shù)據(jù)中包含關(guān)鍵詞的行已經(jīng)如何去除數(shù)據(jù)中包含關(guān)鍵詞的行,文中的示例代碼講解詳細(xì),需要的可以參考一下2023-08-08
Python中ValueError報(bào)錯(cuò)的原因和解決辦法
在Python編程中,ValueError是一種非常常見(jiàn)的異常類(lèi)型,它通常發(fā)生在函數(shù)接收到一個(gè)有效類(lèi)型但不適合該函數(shù)操作的值時(shí),本文將深入探討ValueError的報(bào)錯(cuò)原因、提供詳細(xì)的解決辦法,并通過(guò)豐富的代碼示例來(lái)加深理解,需要的朋友可以參考下2024-07-07
python自定義時(shí)鐘類(lèi)、定時(shí)任務(wù)類(lèi)
這篇文章主要為大家詳細(xì)介紹了Python自定義時(shí)鐘類(lèi)、定時(shí)任務(wù)類(lèi),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
將python運(yùn)行結(jié)果保存至本地文件中的示例講解
今天小編就為大家分享一篇將python運(yùn)行結(jié)果保存至本地文件中的示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07

