where is the error?

I am trying to write a code to calculate the angle between a point on line and another moving point, but there is conversion issue between variable types for hand position point, can anyone help me? here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  // ROM.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc.hpp"
#include "opencv2\core\core.hpp"
#include <math.h>
using namespace cv;
using namespace std;


int main(int argc, char** argv)
{
	VideoCapture cap(-1); //capture the video from webcam

	if (!cap.isOpened())  // if not success, exit program
	{
		cout << "Cannot open the web cam" << endl;
		return -1;
	}

	namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"

	int iLowH = 15;
	int iHighH = 45;

	int iLowS = 100;
	int iHighS = 255;

	int iLowV = 100;
	int iHighV = 255;

	//Create trackbars in "Control" window
	createTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)
	createTrackbar("HighH", "Control", &iHighH, 179);

	createTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
	createTrackbar("HighS", "Control", &iHighS, 255);

	createTrackbar("LowV", "Control", &iLowV, 255);//Value (0 - 255)
	createTrackbar("HighV", "Control", &iHighV, 255);

	int iLastX = -1;
	int iLastY = -1;

	//Capture a temporary image from the camera
	Mat imgTmp;
	cap.read(imgTmp);

	//Create a black image with the size as the camera output
	//Mat imgLines = Mat::zeros(imgTmp.size(), CV_8UC3);;


	while (true)
	{
		Mat imgOriginal;

		bool bSuccess = cap.read(imgOriginal); // read a new frame from video



		if (!bSuccess) //if not success, break loop
		{
			cout << "Cannot read a frame from video stream" << endl;
			break;
		}

		Mat imgHSV;

		cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV

		Mat imgThresholded;

		inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image

																									  //morphological opening (removes small objects from the foreground)
		erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(8, 8)));
		dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(8, 8)));

		//morphological closing (removes small holes from the foreground)
		dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(8, 8)));
		erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(8, 8)));

		//Calculate the moments of the thresholded image
		Moments oMoments = moments(imgThresholded);
		double dM01 = oMoments.m01;
		double dM10 = oMoments.m10;
		double dArea = oMoments.m00;

		//if (dArea > 10000)
		//{
			//calculate the position of hand
			double posX = dM10 / dArea;
			double posY = dM01 / dArea;

			//vector<Point2f> mc(dArea);
			//for (int i = 0; i < dArea; i++)
			//{
				//mc[i] = Point2f(dM10 / dArea, dM01 / dArea);
				//circle(imgThresholded, mc[i], 8, Scalar(255), -1, 8, 0);

			//int x1 = 400;
			//int y1 = 200;
			double x2 = posX;
			double y2 = posY;
			double X = (x2 - 200);
			double Y = (y2 - 200);
		

			Mat image = Mat::zeros(400, 400, CV_8UC3);
			line(image, CvPoint(200, 200), CvPoint(400, 200), Scalar(110, 220, 0), 2, 8);
			line(image, CvPoint(200, 200), CvPoint(X, Y), Scalar(110, 220, 0), 2, 8);
			double angle = atan2(Y, X);
			double b = (angle * 180 / 3.14159265);
			double a = abs((angle * 180 / 3.14159265));
			cout << a;
			//if (b < 0) {
				//b = abs(b);
			//}
				//imshow("Image", image);
			imshow("Thresholded Image", imgThresholded); //show the thresholded image

			imgOriginal = imgOriginal + image;
			imshow("Original", imgOriginal);
			
	
			if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
			{
				cout << "esc key is pressed by user" << endl;
				break;
			}
			return 0;
	}
	}
What's the actual error message?
Debug Error!

Program: C:\Users\Maha\source\repos\ROM\x64\Debug\ROM.exe

abort() has been called

The cmd output is:
Assertion failed: (c1 & c2) != 0 || (x1 | y1 | x2 | y2) >= 0, file C:\build\master_winpack-build-win64-vc14\opencv\modules\imgproc\src\drawing.cpp, line 141
Not sure, but

1
2
3
4
double X = ...
double Y = ...

... CvPoint(X, Y) ... 


According to
https://docs.opencv.org/3.2.0/dc/dd1/structCvPoint.html
CvPoint takes two integers as arguments.

Not sure how that gets past the compiler.


Also, you need your X,Y as doubles to get the angle, but in drawing the line surely it should just be
line(image, CvPoint(200, 200), CvPoint((int)posX, (int)posY), Scalar(110, 220, 0), 2, 8);



Last edited on
Is it possible for you to get a full stack trace of the failure (or at least what function it's being called from in main)? I can't tell where function in main is actually causing the downstream assert error.

You also seem to have a lot of function calls that aren't related to the problem. I would whittle down the length of the code until you only have what is absolutely essential in replicating the problem.
use <cmath> not math.h. Its minor, but it can and will bite you one day.

Now, the question. Is this "find the angle between 2 points relative to the X/Y plane" ? Because the angle between a point and a line is 90 degrees if you hit the nearest point on the line. Also being in motion is irrelevant; at the point of the computation, you take its current location and go from that.

Boiling that down, it should just be 1-2 lines to get the angle.

If the points are the same, it is undefined, and you have to live with that and handle it.
Thanks for everyone who replied me, it seems that the problem is in
1
2
Mat image = Mat::zeros(400, 400, CV_8UC3);

as when debugging the program opens a file called "mat.inl.hpp" and the mouse cursor is in the return line , here is the code :
1
2
3
4
5
6
inline
Mat& Mat::operator = (const MatExpr& e)
{
    e.op->assign(e, *this);
    return *this;
}

but can't fix it, any help?
This is the message i get :-
Unhandled exception at 0x00007FFD99459D98 in ROM.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000E82DEFC640. occurred
Last edited on
@jonnin
It is supposed to calculate the angle between a moving point, and another static point, because i wanna make a game that depends on the user's hand motion
Topic archived. No new replies allowed.