Goal
- Learn how to access image properties
- Learn how to construct Mat
- Learn how to copy Mat
- Learn how to convert the type of Mat
- Learn how to use MatVector
- Learn how to access pixel values and modify them
- Learn how to set Region of Interest (ROI)
- Learn how to split and merge images
Accessing Image Properties
Image properties include number of rows, columns and size, depth, channels, type of image data.
- Note
- src.type() is very important while debugging because a large number of errors in OpenCV.js code are caused by invalid data type.
How to construct Mat
There are 4 basic constructors:
There are 3 static functions:
There are 2 factory functions:
- Note
- Don't forget to delete cv.Mat when you don't want to use it any more.
How to copy Mat
There are 2 ways to copy a Mat:
How to convert the type of Mat
We use the function: convertTo(m, rtype, alpha = 1, beta = 0)
- Parameters
-
m output matrix; if it does not have a proper size or type before the operation, it is reallocated. rtype desired output matrix type or, rather, the depth since the number of channels are the same as the input has; if rtype is negative, the output matrix will have the same type as the input. alpha optional scale factor. beta optional delta added to the scaled values.
How use MatVector
- Note
- Don't forget to delete cv.Mat, cv.MatVector and cnt(the Mat you get from MatVector) when you don't want to use them any more.
Accessing and Modifying pixel values
Firstly, you should know the following type relationship:
Data Properties | C++ Type | JavaScript Typed Array | Mat Type |
---|---|---|---|
data | uchar | Uint8Array | CV_8U |
data8S | char | Int8Array | CV_8S |
data16U | ushort | Uint16Array | CV_16U |
data16S | short | Int16Array | CV_16S |
data32S | int | Int32Array | CV_32S |
data32F | float | Float32Array | CV_32F |
data64F | double | Float64Array | CV_64F |
1. data
- Note
- Data manipulation is only valid for continuous Mat. You should use isContinuous() to check first.
2. at
Mat Type | At Manipulation |
---|---|
CV_8U | ucharAt |
CV_8S | charAt |
CV_16U | ushortAt |
CV_16S | shortAt |
CV_32S | intAt |
CV_32F | floatAt |
CV_64F | doubleAt |
- Note
- At manipulation is only for single channel access and the value can't be modified.
3. ptr
Mat Type | Ptr Manipulation | JavaScript Typed Array |
---|---|---|
CV_8U | ucharPtr | Uint8Array |
CV_8S | charPtr | Int8Array |
CV_16U | ushortPtr | Uint16Array |
CV_16S | shortPtr | Int16Array |
CV_32S | intPtr | Int32Array |
CV_32F | floatPtr | Float32Array |
CV_64F | doublePtr | Float64Array |
mat.ucharPtr(k) get the k th row of the mat. mat.ucharPtr(i, j) get the i th row and the j th column of the mat.
Image ROI
Sometimes, you will have to play with certain region of images. For eye detection in images, first face detection is done all over the image and when face is obtained, we select the face region alone and search for eyes inside it instead of searching whole image. It improves accuracy (because eyes are always on faces) and performance (because we search for a small area)
We use the function: roi (rect)
- Parameters
-
rect rectangle Region of Interest.
Try it
Splitting and Merging Image Channels
Sometimes you will need to work separately on R,G,B channels of image. Then you need to split the RGB images to single planes. Or another time, you may need to join these individual channels to RGB image.
- Note
- Don't forget to delete cv.Mat, cv.MatVector and R(the Mat you get from MatVector) when you don't want to use them any more.
Making Borders for Images (Padding)
If you want to create a border around the image, something like a photo frame, you can use cv.copyMakeBorder() function. But it has more applications for convolution operation, zero padding etc. This function takes following arguments:
- src - input image
- top, bottom, left, right - border width in number of pixels in corresponding directions
- borderType - Flag defining what kind of border to be added. It can be following types:
- cv.BORDER_CONSTANT - Adds a constant colored border. The value should be given as next argument.
- cv.BORDER_REFLECT - Border will be mirror reflection of the border elements, like this : fedcba|abcdefgh|hgfedcb
- cv.BORDER_REFLECT_101 or cv.BORDER_DEFAULT - Same as above, but with a slight change, like this : gfedcb|abcdefgh|gfedcba
- cv.BORDER_REPLICATE - Last element is replicated throughout, like this: aaaaaa|abcdefgh|hhhhhhh
- cv.BORDER_WRAP - Can't explain, it will look like this : cdefgh|abcdefgh|abcdefg
- cv.BORDER_CONSTANT - Adds a constant colored border. The value should be given as next argument.
- value - Color of border if border type is cv.BORDER_CONSTANT
Try it