38 lines
1.8 KiB
C

#pragma once
#include "yolo_runtime.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Optional in-memory RGB example. No bitmap/file is required. The result set
stays alive while borrowed binary views are used; only rgb belongs to us. */
static int example_rgb_mask(int argc, char** argv, const yolo_result_set_t* results,
yolo_task_type_t task, int width, int height) {
int enabled=0;
for(int i=1;i<argc;++i) if(strcmp(argv[i],"--rgb-mask")==0) enabled=1;
if(!enabled) return 0;
if(task!=YOLO_TASK_SEG) {fprintf(stderr,"--rgb-mask requires a Seg model\n");return -1;}
uint32_t count=0;
if(yolo_result_set_get_count(results,&count)!=YOLO_STATUS_OK || count>255) return -1;
yolo_mask_view_v1_t masks[255];
memset(masks,0,sizeof(masks));
for(uint32_t i=0;i<count;++i) {
masks[i].struct_size=sizeof(masks[i]);masks[i].version=YOLO_MASK_VIEW_VERSION;
if(yolo_result_set_get_seg_mask(results,i,&masks[i])!=YOLO_STATUS_OK) return -1;
}
const uint64_t bytes=(uint64_t)width*(uint64_t)height*3;
unsigned char* rgb=(unsigned char*)malloc((size_t)bytes);
if(!rgb) return -1;
yolo_error_info_v1_t error;
memset(&error,0,sizeof(error));error.struct_size=sizeof(error);error.version=YOLO_ERROR_INFO_VERSION;
const yolo_status_t status=yolo_masks_compose_rgb_ids(masks,count,width,height,rgb,width*3,bytes,&error);
if(status==YOLO_STATUS_OK) {
uint64_t foreground=0;
for(uint64_t i=0;i<bytes;i+=3) if(rgb[i]) ++foreground;
printf("RGB_ID_MASK_OK width=%d height=%d instances=%u foreground=%llu bytes=%llu\n",
width,height,count,(unsigned long long)foreground,(unsigned long long)bytes);
} else fprintf(stderr,"RGB mask failed: %s\n",error.message);
free(rgb);
return status==YOLO_STATUS_OK ? 0 : -1;
}