영상처리

이미지 합성 - 1 (OpenCV, C++)

park__ 2024. 12. 12. 17:17

OpenCV를 사용하여 한 영상의 특정 사각형 영역(Rect)을 다른 영상의 사각형 영역으로 복사하는 방법

왼쪽 빨강 영역을 오른쪽 초록색 영역으로 Copy

 

	cv::Mat img1 = cv::imread(file_path1, -1);
	cv::Mat img2 = cv::imread(file_path2, -1);

	cv::Rect rect1(400, 580, 170, 170);
	cv::Rect rect2(100, 100, 170, 170);

	int start_x1 = rect1.x;
	int start_x2 = rect2.x;	
	int start_y1 = rect1.y;
	int start_y2 = rect2.y;
	
	int copy_size = rect1.width * sizeof(uint8_t) * img1.channels();

	uint8_t* p_img1 = nullptr;
	uint8_t* p_img2 = nullptr;

	for (int y = 0; y < rect1.height; y++, start_y1++, start_y2++)
	{
		p_img1 = img1.ptr(start_y1) + start_x1 * img1.channels();
		p_img2 = img2.ptr(start_y2) + start_x2 * img2.channels();
		std::memcpy(p_img2, p_img1, copy_size);
	}

	cv::imshow("img2", img2);

 

결과 이미지

반응형

'영상처리' 카테고리의 다른 글

Opencv Histogram C++  (1) 2024.12.13
OpenCV filter2d Source Code (C++)  (2) 2024.12.13
OpenCV rectangle(Python)  (2) 2024.12.12
AutoThreshold (Yen) C++  (1) 2024.12.11
AutoThreshold (Triangle) C++  (0) 2024.12.11