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      = 1332
		int dst_w = int(src_w / 2) + 16;              // 1658/2 +16 = 845
		cv::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 cv2

def cv_imread(file_path):
    #imdedcode读取的是RGB图像
    cv_img = cv2.imdecode(np.fromfile(file_path,dtype=np.uint8),-1)
    return cv_img


src_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_name
    print(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 = shape0
        img_width = shape1
        image_save_path = dst_path_root + img_name
        if img_height > img_width:
            # 使用zeros()方法创建图像对象
            src_h, src_w, c = img.shape
            dst_h = int(src_h/2)+16
            dst_w = src_w * 2
            blank = 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.shape
            dst_w = int(src_w/2)+16
            dst_h = src_h*2
            blank = 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 附一张效果图

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值