opencv, cv2, rotate image & point

 

refer to code.

sample point is 100,100

firstly, I rotated image as 90 degree and point.

And compare origin image & pt where rotated point is placed in right position.

..

import numpy as np
import cv2
from matplotlib import pyplot as plt

def rotate_image(angle, width, height):
image_center = (width/2, height/2)
rotation_mat = cv2.getRotationMatrix2D(image_center, angle, 1.)
abs_cos = abs(rotation_mat[0,0])
abs_sin = abs(rotation_mat[0,1])
bound_w = int(height * abs_sin + width * abs_cos)
bound_h = int(height * abs_cos + width * abs_sin)
rotation_mat[0, 2] += bound_w/2 - image_center[0]
rotation_mat[1, 2] += bound_h/2 - image_center[1]
return rotation_mat, (bound_w, bound_h)

#open image
image = cv2.imread('img.png')
sample_pt =(100,100)

#get rotate mat, bound
height, width = image.shape[:2]
rotation_mat, (bound_w, bound_h) = rotate_image(90, width, height)

#rotate image
image_90 = cv2.warpAffine(image, rotation_mat, (bound_w, bound_h))
#rotate pt
sample = np.array([100, 100, 1])
sample_90 = np.matmul(rotation_mat, sample)


#origin image & pt
cv2.circle(image, sample_pt, 1, (0, 0, 255), 2, cv2.LINE_AA)

#rotated image & pt
cv2.circle(image_90, (int(sample_90[0]), int(sample_90[1])), 1, (0, 0, 255), 2, cv2.LINE_AA)


image = image[:,:,::-1]
plt.figure(figsize=(10, 10), dpi=100)
plt.imshow(image)

image_90 = image_90[:,:,::-1]
plt.figure(figsize=(10, 10), dpi=100)
plt.imshow(image_90)

..


origin image with point

90 rotated image and point

Thank you.

www.marearts.com


OpenCV Drawing Example, (line, circle, rectangle, ellipse, polyline, fillConvexPoly, putText, drawContours)

line, circle, rectangle, ellipse, polyline, fillConvexPoly, putText, drawContours example source code!!.















...
//http://study.marearts.com/2016/06/opencv-mat-copyto-clone-roi-example-code.html

#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
#include <vector>

#ifdef _DEBUG               
#pragma comment(lib, "opencv_core331d.lib")       
#pragma comment(lib, "opencv_highgui331d.lib")    
#pragma comment(lib, "opencv_imgcodecs331d.lib")  
#pragma comment(lib, "opencv_objdetect331d.lib")  
#pragma comment(lib, "opencv_imgproc331d.lib")  
#pragma comment(lib, "opencv_videoio331d.lib")    
#else       
#pragma comment(lib, "opencv_core331.lib")       
#pragma comment(lib, "opencv_highgui331.lib")    
#pragma comment(lib, "opencv_imgcodecs331.lib")    
#pragma comment(lib, "opencv_objdetect331.lib")  
#pragma comment(lib, "opencv_imgproc331.lib")  
#pragma comment(lib, "opencv_videoio331.lib")    
#endif        


using namespace std;
using namespace cv;

int main()
{
    Mat img(500, 500, CV_8UC3);
    img.setTo(255);

    
    ///////////////////////////////////////////////////
    //line example
    cv::Point pt(300, 300);
    line(img, Point(10, 10), pt, CV_RGB(255, 0, 0), 2);
    line(img, Point(300, 10), Point(30, 300), Scalar(255, 0, 0), 2);
    ///////////////////////////////////////////////////


    ///////////////////////////////////////////////////
    //Circle example
    circle(img, Point(250, 250), 100, CV_RGB(0, 255, 0), 3);
    cv::Point center(400, 400);
    circle(img, center, 300, Scalar(255, 255, 0), 10);
    circle(img, Point(40, 40), 10, Scalar(255, 0, 0), -1);
    ///////////////////////////////////////////////////


    ///////////////////////////////////////////////////
    //rectangle example
    rectangle(img, Rect(10, 10, 200, 200), CV_RGB(255, 0, 0), 2);
    rectangle(img, Rect(Point(40, 40), Point(400, 400)), Scalar(255, 0, 0), 10);
    ///////////////////////////////////////////////////


    /////////////////////////////////////////////////// 
    //ellipse example 1
    ellipse(img, Point(100, 100), Size(100, 50), 0, 0, 360, CV_RGB(255, 0, 0));
    ellipse(img, Point(100, 100), Size(100, 50), 30, 0, 360, CV_RGB(0, 255, 0));
    ellipse(img, Point(100, 100), Size(100, 50), 60, 0, 360, CV_RGB(0, 0, 255));

    ellipse(img, Point(300, 300), Size(100, 50), 0, 0, 180, CV_RGB(255, 0, 0));
    ellipse(img, Point(300, 300), Size(100, 50), 30, 0, 270, CV_RGB(0, 255, 0));
    ellipse(img, Point(300, 300), Size(100, 50), 60, 0, 360, CV_RGB(0, 0, 255));
    ///////////////////////////////////////////////////

    /////////////////////////////////////////////////// 
    //ellipse example 2
    RotatedRect rRect = RotatedRect(Point2f(300, 300), Size2f(300, 100), 30);
    ellipse(img, rRect, CV_RGB(255, 0, 0));

    //draw rect and inside rect in RotatedRect
    Point2f vertices[4];
    rRect.points(vertices);
    for (int i = 0; i < 4; i++)
        line(img, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 0));

    Rect brect = rRect.boundingRect();
    rectangle(img, brect, Scalar(255, 0, 0));
    ///////////////////////////////////////////////////
    

    /*
    ///////////////////////////////////////////////////
    //polylines example 1 
    vector< Point> contour;
    contour.push_back(Point(50, 50));
    contour.push_back(Point(300, 50));
    contour.push_back(Point(350, 200));
    contour.push_back(Point(300, 150));
    contour.push_back(Point(150, 350));
    contour.push_back(Point(100, 100));

    const Point *pts = (const cv::Point*) Mat(contour).data;
    int npts = Mat(contour).rows;

    std::cout << "Number of polygon vertices: " << npts << std::endl;
    // draw the polygon 
    polylines(img, &pts, &npts, 1, false, Scalar(0, 255, 0));

    //polylines example 2 
    contour.clear();
    contour.push_back(Point(400, 400));
    contour.push_back(Point(250, 250));
    contour.push_back(Point(50, 300));

    pts = (const cv::Point*) Mat(contour).data;
    npts = Mat(contour).rows;
    polylines(img, &pts, &npts, 1, true, Scalar(255, 0, 0));
    ///////////////////////////////////////////////////
    */


    /*
    ///////////////////////////////////////////////////
    //fillConvexPoly example 1 
    cv::Point ptss[4];
    ptss[0] = cv::Point(100, 100);
    ptss[1] = cv::Point(150, 200);
    ptss[2] = cv::Point(300, 300);
    ptss[3] = cv::Point(400, 100);

    cv::fillConvexPoly(img, ptss, 4, cv::Scalar(0, 0, 200));
    ///////////////////////////////////////////////////
    */

    /*
    ///////////////////////////////////////////////////
    //textout example 1
    char TestStr[100];
    sprintf_s(TestStr, "total time : %lf sec", 0.001);
    putText(img, TestStr, Point(10, 250), CV_FONT_NORMAL, 1, Scalar(0, 0, 0), 1, 1); //OutImg is Mat class;
    ///////////////////////////////////////////////////
    */

    
    imshow("show0", img);
    waitKey(0);

    return 0;
}
...