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)