- OpenCV 3.0 Computer Vision with Java
- Daniel Lélis Baggio
- 802字
- 2021-07-16 13:17:32
Basic matrix manipulation
From a computer vision background, we can see an image as a matrix of numerical values, which represents its pixels. For a gray-level image, we usually assign values ranging from 0 (black) to 255 (white) and the numbers in between show a mixture of both. These are generally 8-bit images. So, each element of the matrix refers to each pixel on the gray-level image, the number of columns refers to the image width, as well as the number of rows refers to the image's height. In order to represent a color image, we usually adopt each pixel as a combination of three basic colors: red, green, and blue. So, each pixel in the matrix is represented by a triplet of colors.
Note
It is important to observe that with 8 bits, we get 2 to the power of eight (28), which is 256. So, we can represent the range from 0 to 255, which includes, respectively the values used for black and white levels in 8-bit grayscale images. Besides this, we can also represent these levels as floating points and use 0.0 for black and 1.0 for white.
OpenCV has a variety of ways to represent images, so you are able to customize the intensity level through the number of bits considering whether one wants signed, unsigned, or floating point data types, as well as the number of channels. OpenCV's convention is seen through the following expression:
CV_<bit_depth>{U|S|F}C(<number_of_channels>)
Here, U
stands for unsigned, S for signed, and F stands for floating point. For instance, if an 8-bit unsigned single-channel image is required, the data type representation would be CV_8UC1
, while a colored image represented by 32-bit floating point numbers would have the data type defined as CV_32FC3
. If the number of channels is omitted, it evaluates to 1. We can see the ranges according to each bit depth and data type in the following list:
CV_8U
: These are the 8-bit unsigned integers that range from 0 to 255CV_8S
: These are the 8-bit signed integers that range from -128 to 127CV_16U
: These are the 16-bit unsigned integers that range from 0 to 65,535CV_16S
: These are the 16-bit signed integers that range from -32,768 to 32,767CV_32S
: These are the 32-bit signed integers that range from -2,147,483,648 to 2,147,483,647CV_32F
: These are the 32-bit floating-point numbers that range from-FLT_MAX
toFLT_MAX
and includeINF
andNAN
valuesCV_64F
: These are the 64-bit floating-point numbers that range from-DBL_MAX
toDBL_MAX
and includeINF
andNAN
values
You will generally start the project from loading an image, but it is important to know how to deal with these values. Make sure you import org.opencv.core.CvType
and org.opencv.core.Mat
. Several constructors are available for matrices as well, for instance:
Mat image2 = new Mat(480,640,CvType.CV_8UC3); Mat image3 = new Mat(new Size(640,480), CvType.CV_8UC3);
Both of the preceding constructors will construct a matrix suitable to fit an image with 640 pixels of width and 480 pixels of height. Note that width is to columns as height is to rows. Also pay attention to the constructor with the Size
parameter, which expects the width and height order. In case you want to check some of the matrix properties, the methods rows()
, cols()
, and elemSize()
are available:
System.out.println(image2 + "rows " + image2.rows() + " cols " + image2.cols() + " elementsize " + image2.elemSize());
The output of the preceding line is:
Mat [ 480*640*CV_8UC3, isCont=true, isSubmat=false, nativeObj=0xceeec70, dataAddr=0xeb50090 ]rows 480 cols 640 elementsize 3
The isCont
property tells us whether this matrix uses extra padding when representing the image, so that it can be hardware-accelerated in some platforms; however, we won't cover it in detail right now. The isSubmat
property refers to fact whether this matrix was created from another matrix and also whether it refers to the data from another matrix. The nativeObj
object refers to the native object address, which is a Java Native Interface (JNI) detail, while dataAddr
points to an internal data address. The element size is measured in the number of bytes.
Another matrix constructor is the one that passes a scalar to be filled as one of its elements. The syntax for this looks like the following:
Mat image = new Mat(new Size(3,3), CvType.CV_8UC3, new Scalar(new double[]{128,3,4}));
This constructor will initialize each element of the matrix with the triple {128, 3, 4}
. A very useful way to print a matrix's contents is using the auxiliary method dump()
from Mat
. Its output will look similar to the following:
[128, 3, 4, 128, 3, 4, 128, 3, 4; 128, 3, 4, 128, 3, 4, 128, 3, 4; 128, 3, 4, 128, 3, 4, 128, 3, 4]
It is important to note that while creating the matrix with a specified size and type, it will also immediately allocate memory for its contents.
- C++程序設計教程
- Instant Testing with CasperJS
- Mastering JavaScript Object-Oriented Programming
- JIRA 7 Administration Cookbook(Second Edition)
- 青少年軟件編程基礎與實戰(圖形化編程三級)
- C# 從入門到項目實踐(超值版)
- Python網絡爬蟲從入門到實踐(第2版)
- TypeScript實戰指南
- Extending Puppet(Second Edition)
- 單片機C語言程序設計實訓100例
- C++從入門到精通(第5版)
- Canvas Cookbook
- Sails.js Essentials
- AutoCAD基礎教程
- Python Automation Cookbook