| 1 | #include "net.h" |
| 2 | #if defined(USE_NCNN_SIMPLEOCV) |
| 3 | #include "simpleocv.h" |
| 4 | #else |
| 5 | #include <opencv2/core/core.hpp> |
| 6 | #include <opencv2/highgui/highgui.hpp> |
| 7 | #include <opencv2/imgproc/imgproc.hpp> |
| 8 | #endif |
| 9 | #include <stdlib.h> |
| 10 | #include <float.h> |
| 11 | #include <stdio.h> |
| 12 | #include <vector> |
| 13 | |
| 14 | static void draw_objects(const cv::Mat& bgr, const cv::Mat& fgr, const cv::Mat& pha) |
| 15 | { |
| 16 | cv::Mat fgr8U; |
| 17 | fgr.convertTo(fgr8U, CV_8UC3, 255.0, 0); |
| 18 | cv::Mat pha8U; |
| 19 | pha.convertTo(pha8U, CV_8UC1, 255.0, 0); |
| 20 | |
| 21 | cv::Mat comp; |
| 22 | cv::resize(bgr, comp, pha.size(), 0, 0, 1); |
| 23 | for (int i = 0; i < pha8U.rows; i++) |
| 24 | { |
| 25 | for (int j = 0; j < pha8U.cols; j++) |
| 26 | { |
| 27 | uchar data = pha8U.at<uchar>(i, j); |
| 28 | float alpha = (float)data / 255; |
| 29 | comp.at<cv::Vec3b>(i, j)[0] = fgr8U.at<cv::Vec3b>(i, j)[0] * alpha + (1 - alpha) * 155; |
| 30 | comp.at<cv::Vec3b>(i, j)[1] = fgr8U.at<cv::Vec3b>(i, j)[1] * alpha + (1 - alpha) * 255; |
| 31 | comp.at<cv::Vec3b>(i, j)[2] = fgr8U.at<cv::Vec3b>(i, j)[2] * alpha + (1 - alpha) * 120; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | cv::imshow("pha", pha8U); |
| 36 | cv::imshow("fgr", fgr8U); |
| 37 | cv::imshow("comp", comp); |
| 38 | cv::waitKey(0); |
| 39 | } |
| 40 | static int detect_rvm(const cv::Mat& bgr, cv::Mat& pha, cv::Mat& fgr) |
| 41 | { |
| 42 | const float downsample_ratio = 0.5f; |
| 43 | const int target_width = 512; |
| 44 | const int target_height = 512; |
| 45 | |
| 46 | ncnn::Net net; |
| 47 | net.opt.use_vulkan_compute = false; |
| 48 | //original pretrained model from https://github.com/PeterL1n/RobustVideoMatting |
| 49 | //ncnn model https://pan.baidu.com/s/11iEY2RGfzWFtce8ue7T3JQ password: d9t6 |
| 50 | if (net.load_param("rvm_512.param")) |
| 51 | exit(-1); |
| 52 | if (net.load_model("rvm_512.bin")) |
| 53 | exit(-1); |
| 54 | |
| 55 | //if you use another input size,pleaze change input shape |
| 56 | ncnn::Mat r1i = ncnn::Mat(128, 128, 16); |
| 57 | ncnn::Mat r2i = ncnn::Mat(64, 64, 20); |
| 58 | ncnn::Mat r3i = ncnn::Mat(32, 32, 40); |
| 59 | ncnn::Mat r4i = ncnn::Mat(16, 16, 64); |
| 60 | r1i.fill(0.0f); |
| 61 | r2i.fill(0.0f); |
| 62 | r3i.fill(0.0f); |
| 63 | r4i.fill(0.0f); |
| 64 | |
| 65 | ncnn::Extractor ex = net.create_extractor(); |
| 66 | const float mean_vals1[3] = {123.675f, 116.28f, 103.53f}; |
| 67 | const float norm_vals1[3] = {0.01712475f, 0.0175f, 0.01742919f}; |
| 68 | const float mean_vals2[3] = {0, 0, 0}; |
| 69 | const float norm_vals2[3] = {1 / 255.0, 1 / 255.0, 1 / 255.0}; |
| 70 | ncnn::Mat ncnn_in2 = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR2RGB, bgr.cols, bgr.rows, target_width, target_height); |
| 71 | ncnn::Mat ncnn_in1 = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR2RGB, bgr.cols, bgr.rows, target_width * downsample_ratio, target_height * downsample_ratio); |
| 72 | |
| 73 | ncnn_in1.substract_mean_normalize(mean_vals1, norm_vals1); |
| 74 | ncnn_in2.substract_mean_normalize(mean_vals2, norm_vals2); |
| 75 | |
| 76 | ex.input("src1", ncnn_in1); |
| 77 | ex.input("src2", ncnn_in2); |
| 78 | ex.input("r1i", r1i); |
| 79 | ex.input("r2i", r2i); |
| 80 | ex.input("r3i", r3i); |
| 81 | ex.input("r4i", r4i); |
| 82 | |
| 83 | //if use video matting,these output will be input of next infer |
| 84 | ex.extract("r4o", r4i); |
| 85 | ex.extract("r3o", r3i); |
| 86 | ex.extract("r2o", r2i); |
| 87 | ex.extract("r1o", r1i); |
| 88 | |
| 89 | ncnn::Mat pha_; |
| 90 | ex.extract("pha", pha_); |
| 91 | ncnn::Mat fgr_; |
| 92 | ex.extract("fgr", fgr_); |
| 93 | |
| 94 | cv::Mat cv_pha = cv::Mat(pha_.h, pha_.w, CV_32FC1, (float*)pha_.data); |
| 95 | cv::Mat cv_fgr = cv::Mat(fgr_.h, fgr_.w, CV_32FC3); |
| 96 | float* fgr_data = (float*)fgr_.data; |
| 97 | for (int i = 0; i < fgr_.h; i++) |
| 98 | { |
| 99 | for (int j = 0; j < fgr_.w; j++) |
| 100 | { |
| 101 | cv_fgr.at<cv::Vec3f>(i, j)[2] = fgr_data[0 * fgr_.h * fgr_.w + i * fgr_.w + j]; |
| 102 | cv_fgr.at<cv::Vec3f>(i, j)[1] = fgr_data[1 * fgr_.h * fgr_.w + i * fgr_.w + j]; |
| 103 | cv_fgr.at<cv::Vec3f>(i, j)[0] = fgr_data[2 * fgr_.h * fgr_.w + i * fgr_.w + j]; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | cv_pha.copyTo(pha); |
| 108 | cv_fgr.copyTo(fgr); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | int main(int argc, char** argv) |
| 113 | { |
| 114 | if (argc != 2) |
| 115 | { |
| 116 | fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]); |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | const char* imagepath = argv[1]; |
| 121 | |
| 122 | cv::Mat m = cv::imread(imagepath, 1); |
| 123 | if (m.empty()) |
| 124 | { |
| 125 | fprintf(stderr, "cv::imread %s failed\n", imagepath); |
| 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | cv::Mat fgr, pha; |
| 130 | detect_rvm(m, pha, fgr); |
| 131 | draw_objects(m, fgr, pha); |
| 132 | |
| 133 | return 0; |
| 134 | } |