0

So, I have this activity when I have to take an image, divide it into four quadrants and then rotate the quadrants 1 nd four in the same imagen, without creating another window. I have already this code but I just got to make the rotations in differents windows and that's my main issue. If this helps, I made the code in Visual Studio, with a CLR Project. Thank you.

#include "stdafx.h"
#include <iostream>
#include <opencv\cv.h>
#include <opencv\cxcore.h>
#include <opencv\highgui.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);
}
Monica S
  • 1
  • 1
  • 1
    first stop using depreciated C API and use c++. Next read some docs regarding how to crop a image to a particular ROI. Then apply your transformation – Balaji R Nov 07 '16 at 05:44
  • With an angle multiple of 90°, `cv::flip` is a better choice. – O'Neil Nov 07 '16 at 15:13

2 Answers2

0

So, thank you to who helped me with this. I already could fix the problem, a friend tell me that the more factible to get the result I wanted was to divide the image into four quadrants using the function Rect, then create the function for rotate the quadrants I want with getRotationMatrix2D() and warpAffine() and finally I used the functions hconcat() and vconcat() to join the four images I've created before from the original image into a new window and show the resultant image.

Monica S
  • 1
  • 1
0

Here is what you could have with C++ code, no more, no less:

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();

upLeft and downRight are both ROIs acting directly upon img.
Feel free to make a copy before:

cv::Mat src = img.clone();
O'Neil
  • 3,790
  • 4
  • 16
  • 30