97 lines
3.1 KiB
C++
Raw Normal View History

2022-11-03 09:23:30 +01:00
#include "lfr.h"
2022-11-10 14:41:27 +01:00
2022-12-14 16:14:00 +01:00
#define right false
#define left true
2022-10-30 20:01:47 +01:00
LFR::LFR(int videoHeight, int videoWidth, int thresholdBinary, int gaussKernelSize)
: iAmLooping(false), input(videoHeight, videoWidth), processing(), controlModule(), interpreter(), intersectionHandler()
2022-11-03 09:23:30 +01:00
{
this->iAmLooping = false;
this->thresholdBinary = thresholdBinary;
this->gaussKernelSize = gaussKernelSize;
this->videoFlag = false;
this->saveOutputFlag = false;
this->outputFileName = "";
2022-11-03 09:23:30 +01:00
}
2022-11-03 09:23:30 +01:00
LFR::~LFR()
2022-10-30 20:01:47 +01:00
{
2022-11-03 11:18:18 +01:00
if(iAmLooping)
{
this->endLoop();
}
2022-11-03 09:23:30 +01:00
}
void LFR::loop()
{
if(this->videoFlag) {namedWindow("Display window");}
2022-11-10 14:41:27 +01:00
while(iAmLooping)
2022-11-03 09:23:30 +01:00
{
Mat originalImage = input.readWebcam();
2022-12-14 12:30:54 +01:00
Point roiOrigin(0, int(originalImage.rows*(3.25/6.0)));
Rect roi(roiOrigin.x, roiOrigin.y, originalImage.cols, originalImage.rows/6);
//Mat processedImage = originalImage(roi);
Mat processedImage = originalImage;
processing.processImage(processedImage, this->thresholdBinary, this->gaussKernelSize);
//processedImage = processedImage(roi);
FrameData data = processing.calculateLineSegments(processedImage, roi);
2022-12-07 22:22:15 +01:00
processing.filterReflections(data);
2022-12-14 16:14:00 +01:00
processing.calcAngles(data, originalImage.cols, originalImage.rows, left);
this->provideOutput(originalImage, processedImage, data, roi);
2022-11-03 09:23:30 +01:00
}
if(this->videoFlag) {destroyWindow("Display window");}
2022-11-10 14:41:27 +01:00
input.freeWebcam();
2022-11-03 09:23:30 +01:00
}
2022-11-03 08:54:35 +01:00
2022-11-10 14:41:27 +01:00
void LFR::startLoop()
2022-11-03 09:23:30 +01:00
{
2022-11-03 11:18:18 +01:00
iAmLooping = true;
2022-11-10 14:41:27 +01:00
this->loopThread=thread(&LFR::loop, this);
2022-11-03 09:23:30 +01:00
}
void LFR::endLoop()
{
2022-11-03 11:18:18 +01:00
iAmLooping = false;
2022-11-10 14:41:27 +01:00
this->loopThread.join();
2022-11-03 11:18:18 +01:00
return;
}
2022-12-14 16:14:00 +01:00
void LFR::provideOutput(Mat originalImage, Mat processedImage, const FrameData& frameData, const Rect& roi)
{
for(int i = 0; i < frameData.contours.size(); i++)
{
drawContours(originalImage, frameData.contours, i, Scalar(0,255,255), 1, 8, noArray(), 0, Point(roi.x, roi.y));
rectangle(originalImage, frameData.boundingBoxes[i], Scalar(0,255,0));
Rect center(Point(frameData.middlePoints[i].x-2, frameData.middlePoints[i].y-2), Point(frameData.middlePoints[i].x+2, frameData.middlePoints[i].y+2));
rectangle(originalImage, center, Scalar(0,0,255));
Rect leftRect(Point(frameData.leftEdges[i].x-2, frameData.leftEdges[i].y-2), Point(frameData.leftEdges[i].x+2, frameData.leftEdges[i].y+2));
rectangle(originalImage, leftRect, Scalar(0,0,255));
2022-12-14 16:14:00 +01:00
}
if(frameData.contours.size() > 0)
{
//Draw the Arrow for the check of the angle
2022-12-14 12:30:54 +01:00
int length = 100;
2022-12-14 16:14:00 +01:00
Point P1 = frameData.middlePoints[frameData.index];
2022-12-14 12:30:54 +01:00
Point P2;
2022-12-14 16:14:00 +01:00
P2.x = (int)round(P1.x + length * cos(frameData.angle * CV_PI / 180.0));
P2.y = (int)round(P1.y + length * sin(frameData.angle * CV_PI / 180.0));
2022-12-14 12:30:54 +01:00
cv::arrowedLine(originalImage, P1, P2, Scalar(0,0,255), 2, 8);
}
if(this->videoFlag)
{
imshow("Display window", originalImage);
imshow("processed:", processedImage);
char c = (char)waitKey(25);
}
if (this->saveOutputFlag && !(this->outputFileName.empty()))
{
imwrite(this->outputFileName, originalImage);
}
}