O'Reilly logo

Learn OpenCV 4 by Building Projects - Second Edition by Prateek Joshi, Vinicius G. Mendonca, David Millan Escriva

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Reading/writing images

Following the introduction to matrices, we are going to start with the OpenCV code basics. The first thing that we have to learn is how to read and write images:

#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, const char** argv) 
{ 
   // Read images 
   Mat color= imread("../lena.jpg"); 
   Mat gray= imread("../lena.jpg",CV_LOAD_IMAGE_GRAYSCALE); 
  if(! color.data ) // Check for invalid input { 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; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required