Hello everyone, I have created a program using opencv and c++ that mimics planets rotating around the sun. I started by using a blank image and some dots, but I am trying to replace them with actual images. When I simply try to make the background a picture of space, however, I keep getting an exception unhandled error. When I try to simply just display the image in it’s own program it works, but not for this one. Could anyone help me troubleshoot this? The code is below.
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
using namespace std;
using namespace cv;
Mat img;
double theta = 0.0;
double theta2 = 0.0;
int radius = 150;
int radius2 = 50;
bool play_mode = false;
int main(int argc, const char* argv[])
{
Mat universe = imread("universe_space.png");
Mat universe_space;
cv::namedWindow("universe");
Point Q = Point(400, 250); //The rotation about point (red color)
Point P = Point(Q.x + radius, Q.y); //The target point (green color)
//Create another point P2, which will rotate about P
Point P2; //Let p2 rotate about P in counter-clock-wise; initially you can let P2 line up with P horizontally
//You can just theta as the reference
P2 = Point(P.x + radius2, P.y);
double pi = 3.14159265359;
//Create a matrix version for point P
double P_mat_data[3] = { P.x, P.y, 1 };
Mat P_mat = Mat(3, 1, CV_64FC1, P_mat_data); //The matrix version for point P
//Create a matrix version for point P2
double P2_mat_data[3] = { P2.x, P2.y, 1 };
Mat P2_mat = Mat(3, 1, CV_64FC1, P2_mat_data);
while (true)
{
//Refresh the image
if (play_mode)
{
theta += 1;
theta2 += 5;
}
double radian = theta * (pi / 180); //convert Degree to Radian (only for window user)
double radian2 = theta2 * (pi / 180); //convert Degree to Radian (only for window user)
/* Earth around sun: The code below is to generate the 2 matrices for the 3 steps for Q-about rotation */
double T1_data[3][3] = { { 1, 0, -Q.x }, { 0, 1, -Q.y }, { 0, 0, 1 } };
double T2_data[3][3] = { { 1, 0, Q.x }, { 0, 1, Q.y }, { 0, 0, 1 } };
Mat T1 = Mat(3, 3, CV_64FC1, T1_data); //First translation matrix -- Step 1
Mat T2 = Mat(3, 3, CV_64FC1, T2_data); //Second translation matrix -- Step 3
//Update Earth Pos
double R_data[3][3] = { { cos(radian), -sin(radian), 0 }, { sin(radian), cos(radian), 0 }, { 0, 0, 1 } };
Mat R = Mat(3, 3, CV_64FC1, R_data); //Rotation matrix -- Step 2
Mat M = T2 * R * T1; //The arbitrary point rotation
Mat P_mat_new = M * P_mat;
P.x = P_mat_new.ptr<double>(0)[0];
P.y = P_mat_new.ptr<double>(1)[0];
/* Moon around earth: The code below is to generate the 2 matrices for the 3 steps for P-about rotation */
double t1_data[3][3] = { { 1, 0, -P.x }, { 0, 1, -P.y }, { 0, 0, 1 } };
double t2_data[3][3] = { { 1, 0, P.x }, { 0, 1, P.y }, { 0, 0, 1 } };
Mat t1 = Mat(3, 3, CV_64FC1, t1_data); //First translation matrix -- Step 1
Mat t2 = Mat(3, 3, CV_64FC1, t2_data); //Second translation matrix -- Step 3
//Update Moon Pos
double r_data[3][3] = { { cos(radian2), -sin(radian2), 0 }, { sin(radian2), cos(radian2), 0 }, { 0, 0, 1 } };
Mat r = Mat(3, 3, CV_64FC1, r_data); //Rotation matrix -- Step 2
Mat m = t2 * r * t1; //The arbitrary point rotation
Mat P2_mat_new = m * M * P2_mat;
P2.x = P2_mat_new.ptr<double>(0)[0];
P2.y = P2_mat_new.ptr<double>(1)[0];
/* Create the Rotatiom matrix for P-rotation */
//double r_data[3][3]; //implement this part
//Mat r = Mat(3, 3, CV_64FC1, r_data); //Rotation matrix -- Step 2
//Mat m = t2 * r * t1; //The arbitrary point rotation
//Draw the new position
circle(img, P, 1, Scalar(0, 155, 0), 12);
circle(img, Q, 1, Scalar(0, 0, 255), 5);
circle(img, P2, 1, Scalar(255, 0, 0), 7);
universe.copyTo(universe_space);
imshow("Universe", universe_space);
char c = waitKey(1);
if (c == 27)
break;
else if (c == 'p' || c == 'P')
{
if (play_mode)
play_mode = false;
else
play_mode = true;
}
//Key board to change the rotation radius: "i" - increase the radius "d" - decrease the radius
else if (c == 'i' || c == 'I')
{
//increase the radius
// radius++; //increase by 1 pixel, which is so hard to see the difference
radius += 5;
P.x = Q.x + radius;
P2.x = P.x + radius2;
P_mat_data[0] = P.x;
}
else if (c == 'd' || c == 'D')
{
//decrease the radius
if (radius > 5)
radius -= 5;
P.x = Q.x + radius;
P2.x = P.x + radius2;
P_mat_data[0] = P.x;
}
}
return 1;
}