neuer_ansatz_bildverarbeitung #1

Manually merged
zeunerti74726 merged 8 commits from neuer_ansatz_bildverarbeitung into master 2022-12-01 08:17:31 +00:00
5 changed files with 67 additions and 30 deletions
Showing only changes of commit 8c53f49935 - Show all commits

View File

@ -24,19 +24,37 @@ void Processing::processImage(Mat& inputPicture, int thresholdBinary, int gaussK
// And one (the other one) to segment the lines. // And one (the other one) to segment the lines.
// No return value here as the input is passed by reference -> directly modified. // No return value here as the input is passed by reference -> directly modified.
cvtColor(inputPicture, inputPicture, COLOR_BGR2GRAY); cvtColor(inputPicture, inputPicture, COLOR_BGR2GRAY);
threshold(inputPicture, inputPicture, thresholdBinary, 255, THRESH_BINARY);
GaussianBlur(inputPicture, inputPicture, Size(gaussKernelSize, gaussKernelSize), 0); GaussianBlur(inputPicture, inputPicture, Size(gaussKernelSize, gaussKernelSize), 0);
Canny(inputPicture, inputPicture, thresholdCanny1, thresholdCanny2, apertureSizeCanny); threshold(inputPicture, inputPicture, thresholdBinary, 255, THRESH_BINARY);
//Perform a opening
Mat kernel(5,5, CV_8UC1,1);
morphologyEx(inputPicture, inputPicture, 2, kernel);
} }
std::vector<Vec4i> Processing::calculateLineSegments(const Mat& inputPicture) FrameData Processing::calculateLineSegments(const Mat& inputPicture, const cv::Rect& roi)
{ {
//See following link FrameData data;
//https://stackoverflow.com/questions/45322630/how-to-detect-lines-in-opencv cv::findContours(inputPicture, data.contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
vector<Vec4i> lines;
VectorOfLines linesInVectors; //Delete the areas that are too small
HoughLinesP(inputPicture, lines, 1, CV_PI/360, 150, 0, 250); auto iterator = data.contours.begin();
//lines = linesInVectors.findMiddleLine(lines); while(iterator != data.contours.end())
{
return lines; if (contourArea(*iterator) < 3500)
{
iterator = data.contours.erase(iterator);
}
else
{
Rect boundingBox = boundingRect(*iterator);
boundingBox.x += roi.x;
boundingBox.y += roi.y;
data.boundingBoxes.push_back(boundingBox);
data.middlePoints.push_back(Point(boundingBox.x+boundingBox.width/2, boundingBox.y+boundingBox.height/2));
data.leftEdges.push_back(Point(boundingBox.x, boundingBox.y+boundingBox.height/2));
++iterator;
}
}
return data;
} }

View File

@ -21,5 +21,5 @@ public:
void processImage(Mat& inputPicture, int thresholdBinary, int gaussKernelSize, int thresholdCanny1, int thresholdCanny2, int apertureSizeCanny); void processImage(Mat& inputPicture, int thresholdBinary, int gaussKernelSize, int thresholdCanny1, int thresholdCanny2, int apertureSizeCanny);
std::vector<Vec4i> calculateLineSegments(const Mat& inputPicture); FrameData calculateLineSegments(const Mat& inputPicture, const cv::Rect& roi);
}; };

View File

@ -60,4 +60,15 @@ class VectorOfLines{
static double calcDistance(Point p0, Point p1); static double calcDistance(Point p0, Point p1);
vector<Vec4i> findMiddleLine(vector<Vec4i> &lines); vector<Vec4i> findMiddleLine(vector<Vec4i> &lines);
};
class FrameData
{
public:
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Rect> boundingBoxes;
std::vector<cv::Point> leftEdges;
std::vector<cv::Point> middlePoints;
FrameData(): contours(), boundingBoxes(), leftEdges(), middlePoints() {}
}; };

View File

@ -2,7 +2,7 @@
LFR::LFR(int videoHeight, int videoWidth, int thresholdBinary, int gaussKernelSize, int thresholdCanny1, int thresholdCanny2, int apertureSizeCanny) LFR::LFR(int videoHeight, int videoWidth, int thresholdBinary, int gaussKernelSize, int thresholdCanny1, int thresholdCanny2, int apertureSizeCanny)
: iAmLooping(false), input(videoHeight, videoWidth), processing(), controlModule(), interpreter(), intersectionHandler() : iAmLooping(false), input(videoHeight, videoWidth), processing(), controlModule(), interpreter(), intersectionHandler(), roi()
{ {
this->iAmLooping = false; this->iAmLooping = false;
this->thresholdBinary = thresholdBinary; this->thresholdBinary = thresholdBinary;
@ -14,6 +14,10 @@ LFR::LFR(int videoHeight, int videoWidth, int thresholdBinary, int gaussKernelSi
this->videoFlag = false; this->videoFlag = false;
this->saveOutputFlag = false; this->saveOutputFlag = false;
this->outputFileName = ""; this->outputFileName = "";
cv::Point roiOrigin(0, videoHeight*(7.5/12.0));
roi = Rect(roiOrigin.x, roiOrigin.y, videoWidth, videoHeight/12);
} }
LFR::~LFR() LFR::~LFR()
@ -29,23 +33,11 @@ void LFR::loop()
if(this->videoFlag) {namedWindow("Display window");} if(this->videoFlag) {namedWindow("Display window");}
while(iAmLooping) while(iAmLooping)
{ {
Mat image = input.readWebcam(); Mat originalImage = input.readWebcam();
processing.processImage(image, this->thresholdBinary, this->gaussKernelSize, this->thresholdCanny1, thresholdCanny2, this->apertureSizeCanny); Mat processedImage = originalImage;
std::vector<Vec4i> lines = processing.calculateLineSegments(image); processing.processImage(processedImage, this->thresholdBinary, this->gaussKernelSize, this->thresholdCanny1, thresholdCanny2, this->apertureSizeCanny);
for( size_t i = 0; i < lines.size(); i++ ) FrameData data = processing.calculateLineSegments(processedImage, this->roi);
{ this->provideOutput(processedImage);
line( image, Point(lines[i][0], lines[i][1]),
Point( lines[i][2], lines[i][3]), (0,0,255), 1, 8 );
}
if(this->videoFlag)
{
imshow("Display window", image);
char c = (char)waitKey(1);
}
if (this->saveOutputFlag && !(this->outputFileName.empty()))
{
imwrite(this->outputFileName, image);
}
} }
if(this->videoFlag) {destroyWindow("Display window");} if(this->videoFlag) {destroyWindow("Display window");}
input.freeWebcam(); input.freeWebcam();
@ -63,3 +55,16 @@ void LFR::endLoop()
this->loopThread.join(); this->loopThread.join();
return; return;
} }
void LFR::provideOutput(const Mat& image)
{
if(this->videoFlag)
{
imshow("Display window", image);
char c = (char)waitKey(1);
}
if (this->saveOutputFlag && !(this->outputFileName.empty()))
{
imwrite(this->outputFileName, image);
}
}

View File

@ -30,6 +30,9 @@ class LFR
int thresholdCanny1; int thresholdCanny1;
int thresholdCanny2; int thresholdCanny2;
int apertureSizeCanny; int apertureSizeCanny;
cv::Rect roi;
void provideOutput(const Mat& image);
public: public: