neuer_ansatz_bildverarbeitung #1

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

View File

@ -15,8 +15,8 @@ Mat Input::readFile(String filePath)
{ {
std::srand(std::time(0)); std::srand(std::time(0));
// Read all .jpg files from the specified folder // Read all .jpg files from the specified folder
std::string folder = filePath; cv::String folder = filePath;
std::vector<std::string> filenames; std::vector<cv::String> filenames;
cv::glob(folder, filenames); cv::glob(folder, filenames);
// Random shuffle // Random shuffle

View File

@ -1,4 +1,4 @@
#include <opencv2/core/utils/logger.hpp> //#include <opencv2/core/utils/logger.hpp>
#include <opencv2/opencv.hpp> #include <opencv2/opencv.hpp>
#include <iostream> #include <iostream>
@ -37,15 +37,15 @@ void in_depth_processing_chain(int thresholdBinary, int videoHeight, int videoWi
{ {
Input input(videoHeight, videoWidth); Input input(videoHeight, videoWidth);
std::string outputFolder = "C:\\Users\\User\\Desktop\\temp"; cv::String outputFolder = "C:\\Users\\tim-z\\Desktop\\temp";
std::string inputFolder = "C:\\Users\\User\\Desktop\\Studium\\02_Master_MSY\\2. Semester Winter 22 23\\Projekt\\Line-Following-Robot\\AutonomousMode\\Test_data"; cv::String inputFolder = "C:\\Users\\tim-z\\Desktop\\Studium\\02_Master MSY\\2. Semester Winter 2022 2023\\Projekt\\Repo\\Line-Following-Robot\\AutonomousMode\\Test_data";
std::vector<std::string> filenames; std::vector<cv::String> filenames;
cv::glob(inputFolder, filenames); cv::glob(inputFolder, filenames);
//filenames.begin(), filenames.end() //filenames.begin(), filenames.end()
int i = 0; int i = 0;
for(std::vector<std::string>::iterator it = filenames.begin(); it != filenames.end(); it++) for(std::vector<cv::String>::iterator it = filenames.begin(); it != filenames.end(); it++)
{ {
std::string current_output = outputFolder + "\\" + to_string(i); std::string current_output = outputFolder + "\\" + to_string(i);
std::cout << current_output << std::endl; std::cout << current_output << std::endl;
@ -56,7 +56,8 @@ void in_depth_processing_chain(int thresholdBinary, int videoHeight, int videoWi
Mat original_image = input.readFile(inputFile); Mat original_image = input.readFile(inputFile);
imwrite(current_output + "\\00_input.jpg", original_image); imwrite(current_output + "\\00_input.jpg", original_image);
Rect roi = Rect(0, original_image.rows*(7.5/12.0), original_image.cols, original_image.rows/12); Point roiOrigin(0, original_image.rows*(7.5/12.0));
Rect roi = Rect(roiOrigin.x, roiOrigin.y, original_image.cols, original_image.rows/12);
Mat image = original_image(roi); Mat image = original_image(roi);
@ -78,15 +79,41 @@ void in_depth_processing_chain(int thresholdBinary, int videoHeight, int videoWi
vector<vector<Point> > contours; vector<vector<Point> > contours;
vector<Vec4i> hierarchy; vector<Vec4i> hierarchy;
vector<Rect> vectorOfRects;
vector<Point> vectorOfLeftEdges;
findContours(image,contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE); findContours(image,contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE);
int amountOfValidRects = 0;
for( int i = 0; i< contours.size(); i++ ) // iterate through each contour. for( int i = 0; i< contours.size(); i++ ) // iterate through each contour.
{ {
double a = contourArea( contours[i],false); // Find the area of contour double a = contourArea( contours[i],false); // Find the area of contour
if(a > 3500) if(a > 3500)
{ {
drawContours(original_image, contours, i, Scalar(0,255,255), 1, 8, hierarchy, 0, Point(0,original_image.rows*(7.5/12.0))); drawContours(original_image, contours, i, Scalar(0,255,255), 1, 8, hierarchy, 0, roiOrigin);
Rect currentBoundingRect = boundingRect(contours[i]);
//Handle roi offset:
currentBoundingRect.x += roiOrigin.x;
currentBoundingRect.y += roiOrigin.y;
vectorOfRects.push_back(currentBoundingRect);
rectangle(original_image, currentBoundingRect, Scalar(0,255,0));
// get mid-point of rect
Point midRect = Point(currentBoundingRect.x+currentBoundingRect.width/2, currentBoundingRect.y+currentBoundingRect.height/2);
// Draw middle as small rect instead of circle because for whatever reasons drawing a circle doesnt work.
Rect testRect(Point(midRect.x-2, midRect.y-2), Point(midRect.x+2, midRect.y+2));
rectangle(original_image, testRect, Scalar(0,0,255));
// get the left edge of rect
// used as offset as raspicam is not
// mounted on mid of regbot
Point leftEdge(currentBoundingRect.x, currentBoundingRect.y+currentBoundingRect.height/2);
vectorOfLeftEdges.push_back(leftEdge);
testRect = Rect(Point(leftEdge.x-2, leftEdge.y-2), Point(leftEdge.x+2, leftEdge.y+2));
rectangle(original_image, testRect, Scalar(0,0,255));
amountOfValidRects++;
} }
} }
imwrite(current_output + "\\06_contours.jpg", original_image); imwrite(current_output + "\\06_contours.jpg", original_image);
@ -98,7 +125,7 @@ void in_depth_processing_chain(int thresholdBinary, int videoHeight, int videoWi
int main(void) int main(void)
{ {
//Disable opencv logging messages //Disable opencv logging messages
cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_WARNING); //cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_WARNING);
const int thresholdBinary = 140; const int thresholdBinary = 140;
const int videoHeight = 720; const int videoHeight = 720;