Skip to content

Instantly share code, notes, and snippets.

@drabaioli
Created November 25, 2020 10:46
Show Gist options
  • Save drabaioli/02ce16a65c87f3f21cc3f3ff08af1f69 to your computer and use it in GitHub Desktop.
Save drabaioli/02ce16a65c87f3f21cc3f3ff08af1f69 to your computer and use it in GitHub Desktop.
Visualize image after kernel filtering
#include <opencv2/opencv.hpp>
#include <opencv2/core/ocl.hpp>
int main( int argc, char ** argv )
{
cv::VideoCapture cap;
cv::Mat frame;
constexpr int FILTER_SIZE = 3;
const std::string windowName{ "Filter image" };
std::array<float, FILTER_SIZE * FILTER_SIZE> filter_mem{{ 1.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f,
-1.0f, -1.0f, -1.0f }};
const cv::Mat filter = cv::Mat( FILTER_SIZE, FILTER_SIZE, CV_32F, filter_mem.data() ) / static_cast<float>( 1 );
cv::Point anchor = cv::Point( -1, -1 );
cap.open( 0 );
for( cap >> frame; !frame.empty(); cap >> frame )
{
cv::cvtColor( frame, frame, cv::COLOR_BGR2GRAY );
cv::filter2D( frame, frame, -1 , filter, anchor, 0, cv::BORDER_DEFAULT );
cv::imshow( windowName, frame );
char key = cv::waitKey( 1 );
if( key == 'q' || key == 27 )
break;
}
cv::destroyWindow( windowName );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment