输出yolo的测试结果,根据坐标裁剪原图并保存

本文介绍如何根据Yolo的测试结果坐标,裁剪原图并保存。主要修改了`src/image.c`中的`draw_detections`函数,添加了保存文件的功能。在`darknet.h`中更新函数定义,并调整`examples/detector.c`中的`test_detector`函数以处理批量图片。通过这些修改,实现了将检测到的前9个框保存到单独图像文件的目标。
摘要由CSDN通过智能技术生成

因为项目中的需要,本篇博文实现输出(保存)yolo的测试结果,并测试结果的坐标位置切割原图,并不需要知道每个框的类别,保存了top9。
主要对src/image.c文件中的draw_detections函数做了修改。

//添加了 char *filename ,为了得到当前的图片名。
void draw_detections(image im, int num, float thresh, box *boxes, float **probs, float **masks, char **names, image **alphabet, int classes, char *filename)
{
    printf("num %d\n", num);  
    float rawmax[num];
    int i,j;
    int params[3];
    char savePath[100] = "";
    //为了得到top19的框,将每个框按照最大概率值进行了排序,取了top19. b,c矩阵都是为了得到top9对应的原框的下标,好得到坐标点。
    for(i =0; i<num; i++){
    rawmax[i] = probs[i][0];
    for(j =0;j<classes;j++){
         if(probs[i][j] > rawmax[i]){
          rawmax[i] = probs[i][j];                                     }
    }
    }
    for( i =0;i<num;i++){
      if(rawmax[i] > 0)
       printf("rawmax[ %d]:%f\n",i,rawmax[i]);
    }
   float b[num];
   int c[num];

   for(i =0; i<num; c[i]= 1+ i++){
      b[i] = rawmax[i];
   }
   int x;
   for(i =0;i<num;i++){
      for(x = i,j = x+1; j<num;j++)
       if(b[x]<b[j]) x = j;
       if(x!=j){
        j= b[i];
        b[i] = b[x];
        b[x] = j;
        j = c[i];
        c[i] = c[x];
        c[x] = j;
     }
      }
  //输出top9的坐标,后面的代码注释掉了,因为我只需要根据坐标切割出子
  • 4
    点赞
  • 74
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
以下是基于Python的代码实现获取YOLO输出的目标中心坐标的方法: ```python import numpy as np def decode_output(output, anchors, num_classes, input_shape, score_threshold): output = np.array(output) anchors = np.array(anchors) num_anchors = len(anchors) grid_size = output.shape[1:3] num_classes = int(num_classes) # 对YOLO输出进行解码,得到边界框坐标、类别概率和置信度 output = np.reshape(output, [-1, num_anchors * (5 + num_classes)]) xy_offset = np.sigmoid(output[..., :2]) # 边界框中心坐标偏移量 wh_scale = np.exp(output[..., 2:4]) * anchors # 边界框宽高缩放比例 obj_score = np.sigmoid(output[..., 4]) # 边界框置信度 class_prob = np.sigmoid(output[..., 5:]) # 类别概率 # 对边界框坐标进行还原,得到实际图像中的坐标 grid_y = np.tile(np.arange(grid_size[0])[:, np.newaxis], [1, grid_size[1]]) grid_x = np.tile(np.arange(grid_size[1])[np.newaxis, :], [grid_size[0], 1]) xy_offset = np.stack([grid_x, grid_y], axis=-1) + xy_offset xy_offset = xy_offset / np.array(grid_size)[::-1] # 归一化 wh_scale = wh_scale / np.array(input_shape)[::-1] # 归一化 xy_min = xy_offset - wh_scale / 2 xy_max = xy_offset + wh_scale / 2 boxes = np.concatenate([xy_min, xy_max], axis=-1) # 对边界框置信度和类别概率进行筛选和排序 mask = obj_score >= score_threshold boxes = boxes[mask] class_prob = class_prob[mask] scores = obj_score[mask] * class_prob.max(axis=-1) # 对边界框按照置信度进行排序,选择置信度最高的边界框作为最终的检测结果 order = scores.argsort()[::-1] boxes = boxes[order] scores = scores[order] classes = class_prob.argmax(axis=-1)[order] # 计算每个目标的中心坐标 centers = np.stack([(boxes[:, 0] + boxes[:, 2]) / 2, (boxes[:, 1] + boxes[:, 3]) / 2], axis=-1) return boxes, scores, classes, centers ``` 其中,`output`是YOLO输出,`anchors`是预定义的锚框,`num_classes`是类别数量,`input_shape`是输入图像的大小,`score_threshold`是置信度阈值。函数返回值包括边界框坐标、置信度、类别和中心坐标。可以通过调用该函数来获取YOLO输出的目标中心坐标
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值