Skip to content

Instantly share code, notes, and snippets.

@diogon01
Created August 20, 2021 19:28
Show Gist options
  • Save diogon01/4bf1f9942cee2eed60f3add9b61f19d0 to your computer and use it in GitHub Desktop.
Save diogon01/4bf1f9942cee2eed60f3add9b61f19d0 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
// OpenCV includes
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
int main(int argc, char** argv) {
// Read images
Mat color = imread("lena.jpg");
Mat gray = imread("lena.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if (!color.data) {
cout << "Could not open or find the image" << std::endl;
return -1;
}
// Write images
imwrite("lenaGray.jpg", gray);
// Get same pixel with opencv function
int myRow = color.cols - 1;
int myCol = color.rows - 1;
Vec3b pixel = color.at<Vec3b>(myRow, myCol);
cout << "Pixel value (B,G,R): (" << (int)pixel[0] << "," << (int)pixel[1] << "," << (int)pixel[2] << ")" << endl;
// show images
imshow("Lena BGR", color);
imshow("Lena Gray", gray);
// wait for any key press
waitKey(0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment