yolov3 裁剪識別的目標,並將剪裁的目標圖片保存到本地

1 image.h中加入函數聲明

void save_cut_image(int px, int py, int ph, int pw, int no, image m_img, char **names, float cut_pro, int the_class);

2 image.c中加入如下函數定義

void save_cut_image(int px, int py, int ph, int pw, int no, image m_img, char **names, float cut_pro, int the_class)
{
	image copy = copy_image(m_img);
	if (m_img.c == 3) rgbgr_image(copy);
	int x, y, k;
	char buff[256];
	sprintf(buff, "results//%s%.0f%%%d.jpg", names[the_class], cut_pro, no);
	printf(" cut_class  :%s  ...........cut_class:%.0f ", names[the_class], cut_pro);
	printf(names[the_class]);
	printf("%f",cut_pro);	
	IplImage *disp = cvCreateImage(cvSize(m_img.w, m_img.h), IPL_DEPTH_8U, m_img.c);
	int step = disp->widthStep;
	for (y = 0; y < m_img.h; ++y) {
		for (x = 0; x < m_img.w; ++x) {
			for (k = 0; k < m_img.c; ++k) {
				disp->imageData[y*step + x*m_img.c + k] = (unsigned char)(get_pixel(copy, x, y, k) * 255);
			}
		}
	}
	CvMat *pMat = cvCreateMatHeader(m_img.w, m_img.h, IPL_DEPTH_8U);
	CvRect rect = cvRect(px, py, pw, ph);
	cvGetSubRect(disp, pMat, rect);
	IplImage *pSubImg = cvCreateImage(cvSize(pw, ph), IPL_DEPTH_8U, m_img.c);
	cvGetImage(pMat, pSubImg);
	cvSaveImage(buff, pSubImg, 0);	
	free_image(copy);
}

3 在image.c 的 draw_detection_v3()中插入一下代碼

 插在 draw_box_width(im, left, top, right, bot, width, red, green, blue);前一行

int the_class = selected_detections[i].best_class;
float cut_pro = selected_detections[i].det.prob[the_class] * 100;
printf("cut_class  :%s  ...........class_pro:%.0f \n", names[the_class], cut_pro);
int pre_x = left;
int pre_y = top;
int pre_h = bot - top;
int pre_w = right - left;
save_cut_image(pre_x, pre_y, pre_h, pre_w, i, im, names, cut_pro, the_class);
printf("/************  cut and save over *****************/ \n");

 

重編譯,運行測試圖片可自動裁剪圖片中的目標到  目錄下的results文件夾,圖片命名格式爲:類名+概率+計數.jpg

 

4  結果展示

 

 

5、問題:

問題:save_cut_image重定義:不同的基類型

解決:在image.h頭文件中複製一下定義就好。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章