息肉识别算法

//
// Created by lhc on 8/15/22.
//


#include "net.h"
#include "mat.h"

#include 
#include 
#include 
#include 
#include "opencv2/imgproc/types_c.h"
#include 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using std::string;
using std::vector;
using cv::Mat;

void pretty_print(const ncnn::Mat& m)
{

    for (int q=0; q(h, w)[c];
                std::cout << pix << " ";
            }
            std::cout << std::endl;
        }
        std::cout << ">>>>>>>>>>>>"<> get_color_map();
void inference();


int main(int argc, char** argv) {
    inference();
    return 0;
}


void inference() {
    int nthreads = 5;
    string mod_param = "./model/seg.param";
    string mod_model = "./model/seg.bin";


    int oH{144}, oW{144}, n_classes{2};
    float mean[3] = {0.485f, 0.456f, 0.406f};
    float var[3] = {0.229f, 0.224f, 0.225f};
 //   float mean[3] = {0.485, 0.456, 0.406};
//    float var[3] = {0.229, 0.224, 0.225};

    string impth = "./a.jpg";
    string savepth = "out.png";

    // load model
    ncnn::Net mod;
#if NCNN_VULKAN
    int gpu_count = ncnn::get_gpu_count();
    if (gpu_count <= 0) {
        fprintf(stderr, "we do not have gpu device\n");
        return;
    }
    mod.opt.use_vulkan_compute = 1;
    mod.set_vulkan_device(1);
#endif
    mod.load_param(mod_param.c_str());
    mod.load_model(mod_model.c_str());



    // ncnn enable fp16 by default, so we do not need these options
    // int8 depends on the model itself, so we do not set here
    // bool use_fp16 = false;
    // mod.opt.use_fp16_packed = use_fp16;
    // mod.opt.use_fp16_storage = use_fp16;
    // mod.opt.use_fp16_arithmetic = use_fp16;

    // load image, and copy to ncnn mat
    cv::Mat im = cv::imread(impth);
    cv::Mat matOrg=im.clone();
    int iOrigH=im.rows;
    int iOrigW=im.cols;

//    if (im.empty()) {
//        fprintf(stderr, "cv::imread failed\n");
//        return;
//    }
    cv::cvtColor(im,im,CV_BGR2RGB);

    cv::resize(im,im,cv::Size(oW,oH));



  //  cv::Mat im()
    mat_print(im);
 //   ncnn::Mat inp = ncnn::Mat::from_pixels_resize(im.data, ncnn::Mat::PIXEL_BGR, im.cols, im.rows, oW, oH);
    ncnn::Mat inp = ncnn::Mat::from_pixels_resize(im.data, ncnn::Mat::PIXEL_BGR, im.cols, im.rows, oW, oH);

 //   pretty_print(inp);
//    for (float &el : mean)
//    {
//        mean[=el*255;
//    }
    for(int i=0;i<3;i++)
    {
         mean[i]=mean[i]*255;
         std::cout<(std::chrono::system_clock::now() - t0).count() << std::endl;
    std::cout<> color_map = get_color_map();
  //  Mat pred(cv::Size(oW, oH), CV_8UC3);
    Mat pred(cv::Size(oW, oH), CV_8UC1);
    int offset = oH * oW;
 //   omp_set_num_threads(omp_get_max_threads());


#pragma omp parallel for
    color_map[0]={0,0,0};
    color_map[1]={255,255,255};
    for (int i=0; i < oH; ++i) {
//     uint8_t *ptr = pred.ptr(i);
        for (int j=0; j < oW; ++j) {
            // compute argmax
            int idx{0}, argmax{0};
            float max=-10000;
            idx = i * oW + j;
            max = out[idx];

            pred.at(i,j)=max*255;
        }
    }
  //  cv::imshow("coutour",pred);
    cv::resize(pred,pred,cv::Size(iOrigW,iOrigH));


 //   cvtColor(pred, pred, cv::COLOR_BGR2GRAY);

    threshold(pred, pred, 100, 255, cv::THRESH_OTSU);


  //  imshow("bin", binMat);
    //通过findContours函数寻找连通域
    vector> contours;
    vector hierarchy;
    findContours(pred, contours, cv::RETR_LIST,cv::CHAIN_APPROX_NONE);

    //绘制轮廓,内填充
    for (int i = 0; i < contours.size(); i++) {
        cv::RotatedRect rbox = minAreaRect(contours[i]);
       // if (fabs(rbox.size.width * 1.0 / rbox.size.height - 1) < 0.1 && rbox.size.width > 10)
            drawContours(matOrg, contours, i, cv::Scalar(0, 255, 255), 4, 8);
    }

    cv::imshow("x",matOrg);
    cv::waitKey(0);
   cv::imwrite(savepth, matOrg);

    ex.clear(); // must have this, or error
    mod.clear();

}


vector> get_color_map() {
    vector> color_map(256, vector(3));
    std::minstd_rand rand_eng(123);
    std::uniform_int_distribution u(0, 255);
    for (int i{0}; i < 256; ++i) {
        for (int j{0}; j < 3; ++j) {
            color_map[i][j] = u(rand_eng);
        }
    }
    return color_map;
}

你可能感兴趣的:(算法,c++,开发语言)