Class 3
This class introduces basic class and functions in OpenCV library.
In OpenCV 3.0 and later, images are stored as matrix in Mat
object. Mat
class supports all kinds of matrix operation and can be easily converted back and forth to ordinary C++ vector and array.
Manipulation with Mat:
//Constructor -> create new Mat object based on parameters given
Mat::Mat(): flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0) {.....}
//Copy Constructor -> create a copy of original Mat object
Mat::Mat(const Mat& m): ...
Member function of Mat object (Mat img
):
– Reture size of image (in Size type) and width
img.size();
img.size().width;
– Return coordinate of top left corner
img.x; //x coordinate
img.y; //y coordinate
– Check if the image is empty
img.empty();
– Replacing image pixels
img.copyTo(targetMat, mask); //mask specify which pixel to copy
img.copyTo(targetMat); //mask set to matrix of 1 by default
//targetMat and mask are both Mat object, if img is larger than targetMat, will overwrite
General Functions
Besides member functions that we use to manipulate our Mat
object, there some important general functions provided by the library that we are going to use in our project.
– Convert color image to gray
cvtColor( frame, frame_gray, COLOR_BGR2GRAY );
– Resize image
Size size(width, height); //specify the size we want
resize(proc_img, origin_img,size); //proc_img and origin_img are Mat object, can store the processed back to the same object
– Select region of interest (ROI)
//Setect a rectangular region of interest in the image
Rect roi(image.x,image.y,image.size().width, image.size().height); //top x, top y, width, height
//Extract pixel from the region and assign to another Mat object, basically create a sub-image of the original
Mat image_roi = image(roi);
//By manipulating the sub-image, we can change pixels of the original image in the ROI
decoration.copyTo(image_roi);
For our face filter, we will need to place decorations on people’s faces. To insert our decoration image, we need to first select the face location as our region of interest, create a sub-image, then resize our decoration to fit the region, and finally replace pixels in the sub-image with our decoration.
Here is an example code to insert decoration in to an image:
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("./src/img.jpg); //read in image
Mat logoin = imread("./src/decoration.jpg); //read in decoration
//Set width and height of ROI
int w = 100;
int h = 100;
Size size(w,h);
Rect roi(image.x,image.y,w,h); //select the top left coordinate of the ROI in the image, set the width and height
Mat image_roi = image(roi); //extract sub-image
resize(decoration,decoration,size); //resize decoration to fit the ROI
decoration.copyTo(image_roi); //insert the image
imshow("result",img); //show the result image
waitKey(0);
return 0;
}