- void MainWindow::on_pushButton_clicked()
- {
- QFile::copy("assets:/dnn/lena.jpg", "lena.jpg");
- QFile::copy("assets:/dnn/colorization_deploy_v2.prototxt", "colorization_deploy_v2.prototxt");
- QFile::copy("assets:/dnn/colorization_release_v2.caffemodel", "colorization_release_v2.caffemodel");
- Mat src = imread("lena.jpg");
- cvtColor(src,tmp,COLOR_BGR2GRAY);
- cvtColor(tmp,src,COLOR_GRAY2BGR);
- string modelTxt = "colorization_deploy_v2.prototxt";
- string modelBin = "colorization_release_v2.caffemodel";
- bool useOpenCL = true;
- // fixed input size for the pretrained network
- const int W_in = 224;
- const int H_in = 224;
- Net.NET = dnn::readNetFromCaffe(modelTxt, modelBin);
- if (useOpenCL)
- net.setPreferableTarget(DNN_TARGET_OPENCL);
- // setup additional layers:
- int sz[] = {
- 2, 313, 1, 1
- };
- const Mat pts_in_hull(4, sz, CV_32F, hull_pts);
- Ptr<dnn::Layer> class8_ab = net.getLayer("class8_ab");
- class8_ab->blobs.push_back(pts_in_hull);
- Ptr<dnn::Layer> conv8_313_rh = net.getLayer("conv8_313_rh");
- conv8_313_rh->blobs.push_back(Mat(1, 313, CV_32F, Scalar(2.606)));
- // extract L channel and subtract mean
- Mat lab, L, input;
- src.convertTo(tmp, CV_32F, 1.0 / 255);
- cvtColor(tmp, lab, COLOR_BGR2Lab);
- extractChannel(lab, L, 0);
- cv::resize(L, input, Size(W_in, H_in));
- input -= 50;
- // run the L channel through the network
- Mat inputBlob = blobFromImage(input);
- net.setInput(inputBlob);
- Mat result = net.forward();
- // retrieve the calculated a,b channels from the network output
- Size siz(result.size[2], result.size[3]);
- Mat a = Mat(siz, CV_32F, result.ptr(0, 0));
- Mat b = Mat(siz, CV_32F, result.ptr(0, 1));
- cv::resize(a, a, src.size());
- cv::resize(b, b, src.size());
- // merge, and convert back to BGR
- Mat color, chn[] = {
- L, a, b
- };
- merge(chn, 3, lab);
- cvtColor(lab, color, COLOR_Lab2BGR);
- color.convertTo(tmp,CV_8UC3,255);
- QPixmap qpixmap = Mat2QImage(tmp);
- // 将图片显示到 label 上
- ui->label->setPixmap(qpixmap);
- }
来源: https://www.cnblogs.com/jsxyhelu/p/11587046.html