O'Reilly logo

Learning Image Processing with OpenCV by Gloria Bueno García, Oscar Deniz Suarez, José Luis Espinosa Aranda, Jesus Salido Tercero, Ismael Serrano Gracia, Noelia Vállez Enano

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

Common operations with images

The following table summarizes the most typical operations with images:

Operation

Code examples

Set matrix values

img.setTo(0); // for 1-channel img
img.setTo(Scalar(B,G,R); // 3-channel img

MATLAB-style matrix initialization

Mat m1 = Mat::eye(100, 100, CV_64F);
Mat m3 = Mat::zeros(100, 100, CV_8UC1);
Mat m2 = Mat::ones(100, 100, CV_8UC1)*255;

Random initialization

Mat m1 = Mat(100, 100, CV_8UC1);
randu(m1, 0, 255);

Create a copy of the matrix

Mat img1 = img.clone();

Create a copy of the matrix (with the mask)

img.copy(img1, mask);

Reference a submatrix (the data is not copied)

Mat img1 = img (Range(r1,r2),Range(c1,c2));

Image crop

Rect roi(r1,c2, width, height); Mat img1 = img(roi).clone(); // data ...

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