From 61030ccffe1f388946f223d8190df9aa2c84a91f Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Thu, 2 Nov 2023 18:26:45 -0500 Subject: [PATCH] Add Canny Edge Detection Algorithm. --- OpenCVVideoParser/main.cpp | 41 ++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/OpenCVVideoParser/main.cpp b/OpenCVVideoParser/main.cpp index 9f39cf6..767c6a7 100644 --- a/OpenCVVideoParser/main.cpp +++ b/OpenCVVideoParser/main.cpp @@ -79,9 +79,12 @@ public: } public: - std::vectorframes; + cv::Mat edges; float frameView=0; cv::VideoCapture video; // open the video file + bool edgeDetect=true; + float edgeLowerThreshold=150; + float edgeUpperThreshold=255; bool OnUserCreate() override { // Called once at the start, so create things here @@ -92,10 +95,20 @@ public: void UpdateScreen(){ Clear(BLACK); cv::Mat targetFrame=get_frame(video,std::clamp(int(frameView),0,int(video.get(cv::CAP_PROP_FRAME_COUNT)))); - for(int y=0;y(y,x); - Draw(x,y,{col[2],col[1],col[0]}); + if(edgeDetect){ + cv::Canny(targetFrame,edges,edgeLowerThreshold,edgeUpperThreshold); + for(int y=0;y(y,x); + Draw(x,y,{col,col,col}); + } + } + }else{ + for(int y=0;y(y,x); + Draw(x,y,{col[2],col[1],col[0]}); + } } } } @@ -124,8 +137,9 @@ public: } if(!cabDetected){ DrawString({4,ScreenHeight()-40},"Drag over the region where the cab's screen is located.\n (Make sure the entire cabinet screen is covered)",WHITE,2); - SetPixelMode(Pixel::ALPHA); FillRectDecal(upperLeft,lowerRight-upperLeft,{255,255,255,128}); + DrawString({4,ScreenHeight()-40-16},"Upper Threshold: "+std::to_string(edgeUpperThreshold),WHITE,2); + DrawString({4,ScreenHeight()-40-32},"Lower Threshold: "+std::to_string(edgeLowerThreshold),WHITE,2); } if(GetMouse(Mouse::LEFT).bPressed){ if(!dragging){ @@ -139,6 +153,21 @@ public: if(GetMouse(Mouse::LEFT).bReleased){ dragging=false; } + if(GetKey(HOME).bPressed){ + edgeDetect=!edgeDetect; + } + if(GetKey(INS).bHeld){ + edgeLowerThreshold=std::clamp(edgeLowerThreshold+fElapsedTime*60,0.f,255.f); + } + if(GetKey(DEL).bHeld){ + edgeLowerThreshold=std::clamp(edgeLowerThreshold-fElapsedTime*60,0.f,255.f); + } + if(GetKey(PGUP).bHeld){ + edgeUpperThreshold=std::clamp(edgeUpperThreshold+fElapsedTime*60,0.f,255.f); + } + if(GetKey(PGDN).bHeld){ + edgeUpperThreshold=std::clamp(edgeUpperThreshold-fElapsedTime*60,0.f,255.f); + } return true; } };