C++ 图像切分与拼接

头文件

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;


struct bbox_t {
    
    
	unsigned int x, y, w, h;       // (x,y) - top-left corner, (w, h) - width & height of bounded box
	float prob;                    // confidence - probability that the object was found correctly
	unsigned int obj_id;           // class of object - from range [0, classes-1]
	unsigned int track_id;         // tracking id for video (0 - untracked, 1 - inf - tracked object)
	unsigned int frames_counter;   // counter of frames on which the object was detected
	float x_3d, y_3d, z_3d;        // center of object (in Meters) if ZED 3D Camera is used
};

主程序

#include "my.h"




void  ImgBlock(Mat& src, int sub, vector<Mat> & subImages, vector<Rect2i> & location)
{
    
    
	
	int height = src.rows;
	int width = src.cols;

	int stride = int(sub * 0.9);  // 374

	for (int i = 0; i < height; i+=stride)
	{
    
    
		for (int j = 0; j < width; j+=stride)
		{
    
    
			Rect sub_rect;
			sub_rect.x = j + 416 < width ? j : width - 416 ;
			sub_rect.y = i + 416 < height ? i : height - 416;
			sub_rect.width = 416;//j + 416 < width ? 416 : width - j;
			sub_rect.height = 416;//i + 416 < height ? 416 : height - i;

			Mat sub_img = src(sub_rect);
			string sub_name = to_string(i) + "_" + to_string(j) + ".jpg"; // ���±걣��
			imwrite(sub_name, sub_img);
			subImages.push_back(sub_img);
			location.push_back(sub_rect);


		}
	}

	
		


}


void ImgStitch(vector<Mat> subImages, vector<Rect2i> locations, Mat res)
{
    
    

	vector<bbox_t> result;
	for (int i = 0; i < subImages.size(); i++)
	{
    
    
		
		Mat subImg = subImages[i];




		subImg =5*subImg;
		Rect2i loc = locations[i];
		vector<bbox_t> yolo_box;
		for (int k = 0; k < yolo_box.size(); k++)
		{
    
    
			bbox_t n_box;
			n_box.x = yolo_box[i].x + loc.x;
			n_box.y = yolo_box[i].y + loc.y;
			n_box.obj_id = yolo_box[i].obj_id;
			n_box.prob = yolo_box[i].prob;

			result.push_back(n_box);
		}

		// yolo���
		
		Mat ROI = res(loc);

		subImg.copyTo(res(loc));


		



	}


}






int main()
{
    
    

	Mat img = imread("1.jpg");

	int height = img.rows;
	int width = img.cols;
	imshow("1", img);
	int sub = 416;
	vector<Mat> subImages;
	vector<Rect2i> locations;

	ImgBlock(img, sub, subImages, locations);
	Mat res = Mat::zeros(Size(width, height), img.type());
	ImgStitch(subImages, locations, res);
	imshow("2", res);
	waitKey(0);
	system("pause");
	return 0;

}

猜你喜欢

转载自blog.csdn.net/shuaijieer/article/details/126047049