如何用OpenCV将图像分成四个象限,并将象限1和象限4旋转180度

How to divide an image in four quadrants and rotate 180 degrees the quadrant 1 and 4 with OpenCV?

本文关键字:180度 旋转 四个 图像 OpenCV 何用      更新时间:2023-10-16

所以,当我必须拍摄图像时,我有这个活动,将其分为四个象限,然后在同一图像中旋转象限1和4,而不创建另一个窗口。我已经有了这段代码,但我只需要在不同的窗口中进行旋转,这是我的主要问题。如果这有帮助,我在Visual Studio中使用CLR项目编写代码。谢谢你。

#include "stdafx.h"
#include <iostream>
#include <opencvcv.h>
#include <opencvcxcore.h>
#include <opencvhighgui.h>
#include <cmath>
using namespace System;
using namespace std;
using namespace cv;
int main( int argc, char** argv ){ 
    CvPoint pt1, pt2;
    int width; 
    int height; 
    IplImage* img = cvLoadImage("C:/Users/Munii/Documents/Visual Studio 2010/Projects/Proyecto/Proyecto/Imagen.jpg"); 
    pt1.x=0; 
    pt1.y=0; 
    pt2.x = (img->width)/2; 
    pt2.y = (img->height)/2; 
    width = img->width; 
    height = img->height; 
    IplImage* rotated=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,img->nChannels);
    CvPoint2D32f center;
    float angle=180;
    CvMat* M = cvCreateMat(2,3,CV_32FC1);
    center.x = (img->width)/2.0;
    center.y = (img->height)/2.0;
    cv2DRotationMatrix(center,angle,1.0,M);
    cvWarpAffine(img,rotated,M,CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, cvScalarAll(0)); 
    cvSetImageROI(rotated, cvRect( pt1.x, pt1.y, pt2.x, pt2.y)); 
    pt1.x = (img->width)/2;
    pt1.y = (img->height)/2;  
    pt2.x = img->width;
    pt2.y = img->height;  
    IplImage* rot=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,img->nChannels);
    center.x=img->width/2.0;
    center.y=img->height/2.0;
    cv2DRotationMatrix(center,angle,1.0,M);
    cvWarpAffine(img,rot,M,CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, cvScalarAll(0)); 
    cvSetImageROI(rot, cvRect( pt1.x, pt1.y, pt1.x, pt2.y)); 
    cvNamedWindow("Ejemplo 3", CV_WINDOW_AUTOSIZE); //creamos una ventana con el nombre Ejemplo 3
    cvNamedWindow("ROI", CV_WINDOW_AUTOSIZE); 
    cvNamedWindow("RO", CV_WINDOW_AUTOSIZE); //creamos una ventana con el nombre ROI donde estará la región de interes
    cvShowImage("Ejemplo 3", img);
    cvShowImage("ROI",rotated);
    cvShowImage("RO",rot); //mostramos la imagen en la ventana anteriormente creada
    cvSaveImage("C:/Users/Munii/Documents/Visual Studio 2010/Projects/Proyecto/Proyecto/RotacionI.jpg", rotated);
    cvSaveImage("C:/Users/Munii/Documents/Visual Studio 2010/Projects/Proyecto/Proyecto/RotacionIV.jpg", rot);
    cvWaitKey(0);
    cvDestroyWindow("ROI" );
    cvDestroyWindow("RO" );
    cvReleaseImage( &img );
    cvDestroyWindow("Ejemplo3" );    
    cvReleaseImage(&rotated);   
    cvReleaseMat(&M);
}

所以,谢谢你帮助我。我已经可以解决这个问题,一个朋友告诉我,越factible得到我想要的结果是把图像分成四个象限使用矩形函数,然后创建函数旋转象限我要和getRotationMatrix2D()和warpAffine()和最后我使用函数hconcat()和vconcat()加入四个图片我从原始图像到之前创建一个新窗口并显示合成图像。

下面是c++代码,不多不少:

const int rot180 {-1};
cv::Mat img = cv::imread("path/to/your/image");
cv::imshow("Original", img);
cv::Mat upLeft = img({0, img.rows/2}, {0, img.cols/2});
cv::flip(upLeft, upLeft, rot180);
cv::Mat downRight = img({img.rows/2, img.rows}, {img.cols/2, img.cols});
cv::flip(downRight, downRight, rot180);
cv::imshow("Rotated", img);
cv::imwrite("path/to/your/new/image", img);
cv::waitKey();
cv::destroyAllWindows();

upLeftdownRight都是直接作用于img的roi。
请随意在:

cv::Mat src = img.clone();