当前位置: 首页 > news >正文

yolo V8训练 长条状目标

1、说明

目标数据集合中有很多长条状图片,如果直接Resize 会严重拉伸,因此采用把长条图像裁剪成2段,然后将裁剪后的2段图片拼接在一起。

2、代码

2.1 C++ 代码 (部署,模型推理时C++ )

#include <stdio.h>
#include<string>
#include <vector>
#include <sys/stat.h>
#include<opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>  
#include <opencv2/imgproc/imgproc.hpp>  
#include <opencv2/core/core.hpp>  
using namespace std;
using namespace cv;
int main()
{cv::Mat img = cv::imread("_1.jpg", 1);cv::Mat dst;int src_h = img.rows;  int	src_w = img.cols;std::cout << "src_h " << img.rows << std::endl;std::cout << "src_w " << img.cols << std::endl;if (src_h > src_w && src_h > 600){int dst_h = int(src_h / 2) + 16;int dst_w = src_w * 2;cv::Mat blank(dst_h, dst_w, CV_8UC3, cv::Scalar(0, 0, 0));cv::Mat blank_1 = blank(cv::Rect(0, 0, src_w, dst_h));cv::Mat blank_2 = blank(cv::Rect(src_w - 1, 0, src_w, dst_h - 16));blank_1 += img(cv::Rect(0, 0, src_w, dst_h));blank_2 += img(cv::Rect(0, dst_h - 16 - 1, src_w, dst_h - 16));dst = blank.clone();}else if (src_w > 600 && src_h > 100){int dst_h = src_h * 2;                    // 566*2      = 1332int dst_w = int(src_w / 2) + 16;              // 1658/2 +16 = 845cv::Mat blank(dst_h, dst_w, CV_8UC3, cv::Scalar(0, 0, 0));cv::Mat blank_1 = blank(cv::Rect(0, 0, dst_w, src_h));cv::Mat blank_2 = blank(cv::Rect(0, src_h - 1, dst_w - 16, src_h));blank_1 += img(cv::Rect(0, 0, dst_w, src_h));blank_2 += img(cv::Rect(dst_w - 16 - 1, 0, dst_w - 16, src_h));dst = blank.clone();}std::cout << "dst_h " << img.rows << std::endl;std::cout << "dst_w " << img.cols << std::endl;cv::imshow("src", img);cv::imshow("dst", dst);cv::waitKey(0);
}

2.2 python 代码(训练时处理数据)

import os
import numpy as np
import copy
import shutil
import cv2def cv_imread(file_path):#imdedcode读取的是RGB图像cv_img = cv2.imdecode(np.fromfile(file_path,dtype=np.uint8),-1)return cv_imgsrc_path_root = "E:/trip/"
dst_path_root = "E:/strip_combine/"
images_names=os.listdir(src_path_root)
print(images_names)
print("图片张数:",len(images_names))
for img_name in images_names:image_path = src_path_root+img_nameprint(image_path)print(img_name[-4:])if img_name[-4:] != "json":img = cv_imread(image_path)cv2.imshow("img",img)print(img.shape)shape0 = img.shape[0]shape1 = img.shape[1]img_height = shape0img_width = shape1image_save_path = dst_path_root + img_nameif img_height > img_width:# 使用zeros()方法创建图像对象src_h, src_w, c = img.shapedst_h = int(src_h/2)+16dst_w = src_w * 2blank = np.zeros((dst_h, dst_w, c), dtype=np.uint8)blank[0:dst_h, 0:src_w, :] = img[0:dst_h, 0:src_w, :]blank[0:dst_h-16:, src_w:2*src_w, :] = img[dst_h-16:2*dst_h-32, 0:src_w, :]cv2.imshow('blank', blank)cv2.imwrite(image_save_path, blank)cv2.waitKey(10)if img_height < img_width:# 使用zeros()方法创建图像对象src_h, src_w, c = img.shapedst_w = int(src_w/2)+16dst_h = src_h*2blank = np.zeros((dst_h, dst_w, c), dtype=np.uint8)blank[0:src_h, 0:dst_w, :] = img[0:src_h, 0:dst_w, :]blank[src_h:2*src_h, 0:dst_w-16:, :] = img[0:src_h, dst_w-16:2*dst_w-32, :]cv2.imshow('blank', blank)cv2.imwrite(image_save_path, blank)cv2.waitKey(10)

3 附一张效果图

在这里插入图片描述

http://www.lryc.cn/news/428533.html

相关文章:

  • 数据结构与算法 - 设计
  • 62 网络设备的暗藏的操控者SNMP
  • 华硕飞行堡垒键盘全部失灵【除电源键】
  • 前端字符串将其分割成长度为 32 的子字符串数组
  • 小学二年级数学精选试题
  • 练习题 - 探索正则表达式re功能
  • 【Oracle 11G 配置使用教程1】
  • 【ubuntu24.04】docker pull 配置
  • 《机器学习》—— 通过下采样方法实现银行贷款分类问题
  • Synchronized重量级锁原理和实战(五)
  • linux常用网络工具汇总三
  • Linux中nano编辑器详解
  • 26-vector arraylist和linkedlist的区别
  • 20-redis穿透击穿雪崩
  • Docker使用教程
  • poi-tl循环放图片+文字说明
  • 数据结构之树的存储结构
  • Zotero 常用插件介绍
  • WebSocket协议解析
  • ES6 (一)——ES6 简介及环境搭建
  • HarmonyOS开发案例:列表场景实例-TaskPool
  • 谷歌浏览器如何隐藏书签
  • SQL - 视图
  • centos7环境升级默认的gcc 4.8.5到gcc 8.2.0, 并且升级glibc到glibc 2.28
  • FastHTML:使用 Python 彻底改变 Web 开发
  • 快速排序的深入优化探讨
  • c语言杂谈系列:模拟虚函数
  • 短视频推广App不再难!Xinstall来帮忙
  • 打靶记录13——doubletrouble
  • awk文本处理工具