https://stackoverflow.com/questions/15956124/minarearect-angles-unsure-about-the-angle-returned
上のURLにあるプログラムでは0度から90度の範囲で10度ずつ与えながら、長方形が回転していきます。これを下のコメントにあるように0度から180度の範囲で10度ずつ与えるようにしたいと考え、コメントにある使用するコードを入れたのですが、結果が変わらなかったり、エラーを吐いてしまいます。そのため、使用するコードをどこに入れたら良いのか教えてほしいです。
0~90度の範囲のプログラムのもの↓
#define _CRT_SECURE_NO_WARNINGS
/-------------- ヘッダファイル ----------------/
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
//CP2110/4 HID USB-to-UART インターフェースライブラリのインクルードファイル
#include <SLABCP2110.h>
#include <SLABHIDtoUART.h>
#include <CP2114_Common.h>
//CP2110/4 HID USB-to-UART インターフェースライブラリ へのリンク
#pragma comment(lib,"SLABHIDtoUART.LIB")
#pragma comment(lib,"winmm.LIB")
/OpenCV用------------------------------------------------------------------------/
#include<math.h>
#include<stdio.h>
#include<vector>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<iostream>
// OpenCV includes
#include <opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv/cv.h>
using namespace cv;
using namespace std;
#pragma warning(disable:4996)
/*#include <stdio.h>
#include <opencv\cv.h>
#include <opencv\highgui.h>
using namespace cv;*/
int main() {
float angle = 0;
Mat image(200, 400, CV_8UC3, Scalar(0));
RotatedRect originalRect;
Point2f vertices[4];
vector<Point2f> vertVect;
RotatedRect calculatedRect;
while (waitKey(5000) != 27) { // 長方形を作成し、毎回10度ずつ回転させます。 originalRect = RotatedRect(Point2f(100, 100), Size2f(100, 50), angle); // minAreaRectが使用できるように、長方形を点のベクトルに変換します。 // 同じ場所で また、ポイントを右に移動して、2つの長方形が動かないようにします。 originalRect.points(vertices); for (int i = 0; i < 4; i++) { vertVect.push_back(vertices[i] + Point2f(200, 0)); } // Get minAreaRect to find a rectangle that encloses the points. minAreaRectを取得して、ポイントを囲む長方形を見つけます。 // This should have the exact same orientation as our original rectangle.これは、元の長方形とまったく同じ向きである必要があります。 calculatedRect = minAreaRect(vertVect); // Draw the original rectangle, and the one given by minAreaRect.元の長方形と、minAreaRectで指定された長方形を描画します。 for (int i = 0; i < 4; i++) { line(image, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 0)); line(image, vertVect[i], vertVect[(i + 1) % 4], Scalar(255, 0, 0)); } imshow("rectangles", image); // Print the angle values. 角度の値を出力します。 printf("---\n"); printf("Original angle: %7.2f\n", angle); printf("Angle given by minAreaRect: %7.2f\n", calculatedRect.angle); printf("---\n"); // Reset everything for the next frame.//次のフレームのためにすべてをリセットします。 image = Mat(200, 400, CV_8UC3, Scalar(0)); vertVect.clear(); angle += 10; } return 0;
}
使用したいコード
一つ目
void printAngle(RotatedRect calculatedRect){
if(calculatedRect.size.width < calculatedRect.size.height){
printf("Angle along longer side: %7.2f\n", calculatedRect.angle+180);
}else{
printf("Angle along longer side: %7.2f\n", calculatedRect.angle+90);
}
}
二つ目
printf("Angle given by minAreaRect: %7.2f\n", calculatedRect.angle);
printAngle(calculatedRect);
printf("---\n");

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。