Skip to content

Instantly share code, notes, and snippets.

@atoz-programming-tutorials
Last active November 4, 2018 13:10
Show Gist options
  • Save atoz-programming-tutorials/1b1265940e9e7a89c024b7688596cd52 to your computer and use it in GitHub Desktop.
Save atoz-programming-tutorials/1b1265940e9e7a89c024b7688596cd52 to your computer and use it in GitHub Desktop.
// OpenCV 3 demo - Convert a color image to gray
// See YouTube tutorial https://www.youtube.com/watch?v=9lra7lTKpbs
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
int main() {
const std::string img_file{"water.png"};
// Check if we can open the file
cv::Mat input = cv::imread(img_file, 1);
if(!input.data) {
std::cout << "Can't open file " << img_file << '\n';
exit(1);
}
// Convert to gray
cv::Mat output;
cvtColor(input, output, cv::COLOR_BGR2GRAY);
// Show the original and the result
cv::namedWindow("Original image", cv::WINDOW_AUTOSIZE);
cv::imshow("Original image", input);
cv::namedWindow("Gray image", cv::WINDOW_AUTOSIZE);
cv::imshow("Gray image", output);
// Wait until the presses any key
cv::waitKey(0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment