Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3658387
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2022-03-04 17:31:34

#include

#include

#include

#include

Mat cv::dnn::blobFromImage(

InputArray image,

double scalefactor = 1.0,

const Size & size = Size(),

const Scalar & mean = Scalar(),

bool swapRB = false,

bool crop = false,

int ddepth = CV_32F

)

Mat cv::dnn::blobFromImages(

InputArrayOfArrays images,

double scalefactor = 1.0,

Size size = Size(),

const Scalar & mean = Scalar(),

bool swapRB = false,

bool crop = false,

int ddepth = CV_32F

)

参数解释

Images表示多张图像,image表示单张图像

Scalefactor表示放缩

Size表示图像大小

Mean表示均值

swapRB是否交换通道

crop是否剪切

ddepth 输出的类型,默认是浮点数格式

using namespace cv;

using namespace cv::dnn;

using namespace std;

// 图像处理  标准化处理

void PreProcess(const Mat& image, Mat& image_blob)

{

Mat input;

image.copyTo(input);

//数据处理 标准化

std::vector channels, channel_p;

split(input, channels);

Mat R, G, B;

B = channels.at(0);

G = channels.at(1);

R = channels.at(2);

B = (B / 255. - 0.406) / 0.225;

G = (G / 255. - 0.456) / 0.224;

R = (R / 255. - 0.485) / 0.229;

channel_p.push_back(R);

channel_p.push_back(G);

channel_p.push_back(B);

Mat outt;

merge(channel_p, outt);

image_blob = outt;

}

String bin_model = "F:\\Pycharm\\PyCharm_Study\\Others\\c++_learning\\C++_Master\\Onnx\\classification\\vgg16.onnx";

String labels_txt_file = "F:\\Pycharm\\PyCharm_Study\\Others\\c++_learning\\C++_Master\\Onnx\\classification\\classification_classes_ILSVRC2012.txt";

vector readClassNames();                  // string对象作为vector对象

int main(int argc, char** argv) {

Mat image1 = imread("F:\\Pycharm\\PyCharm_Study\\Others\\c++_learning\\C++_Master\\Onnx\\classification\\dog.jpg");

Mat image2 = imread("F:\\Pycharm\\PyCharm_Study\\Others\\c++_learning\\C++_Master\\Onnx\\classification\\rabbit.jpg");

//用于显示

vectorShowimages;

Showimages.push_back(image1);

Showimages.push_back(image2);

//处理image1

resize(image1, image1, Size(256, 256), INTER_AREA);

image1.convertTo(image1, CV_32FC3);

PreProcess(image1, image1);         //标准化处理

//处理image2

resize(image2, image2, Size(256, 256), INTER_AREA);

image2.convertTo(image2, CV_32FC3);

PreProcess(image2, image2);         //标准化处理

//image1image2合并到images

vector images;

images.push_back(image1);

images.push_back(image2);

vector labels = readClassNames();

int w = 224;

int h = 224;

// 加载网络

cv::dnn::Net net = cv::dnn::readNetFromONNX(bin_model);  // 加载训练好的识别模型

if (net.empty()) {

printf("read onnx model data failure...\n");

return -1;

}

Mat inputBlob = blobFromImages(images, 1.0, Size(w, h), Scalar(0, 0, 0), false, true);

// 执行图像分类

net.setInput(inputBlob);

cv::Mat prob = net.forward();     // 推理出结果

cout << prob.cols<< endl;

vector times;

double time = net.getPerfProfile(times);

float ms = (time * 1000) / getTickFrequency();

printf("current inference time : %.2f ms \n", ms);

// 得到最可能分类输出

for (int n = 0; n < prob.rows; n++) {

Point classNumber;

double classProb;

Mat probMat = prob(Rect(0, n, 1000, 1)).clone();

Mat result 外汇跟单gendan5.com= probMat.reshape(1, 1);

minMaxLoc(result, NULL, &classProb, NULL, &classNumber);

int classidx = classNumber.x;

printf("\n current image classification : %s, possible : %.2f\n", labels.at(classidx).c_str(), classProb);

// 显示文本

putText(Showimages[n], labels.at(classidx), Point(10, 20), FONT_HERSHEY_SIMPLEX, 0.6, Scalar(0, 0, 255), 1, 1);

imshow("Image Classification", Showimages[n]);

waitKey(0);

}

return 0;

}

std::vector readClassNames()

{

std::vector classNames;

std::ifstream fp(labels_txt_file);

if (!fp.is_open())

{

printf("could not open file...\n");

exit(-1);

}

std::string name;

while (!fp.eof())

{

std::getline(fp, name);

if (name.length())

classNames.push_back(name);

}

fp.close();

return classNames;

}

阅读(7053) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~