Mat類的介紹與圖像的讀寫
1. Mat類的介紹
opencv2.x採用全行的圖像數據結構Mat。Mat結構不需要我們爲其手動開闢空間,也不需要立即釋放存儲空間,Mat類能夠自動管理內存。Mat類由矩陣頭和指向存儲所有像素值的矩陣真的指針構成。Mat類表示一個n維的密集數值單通道或多通道數組,它可用於存儲實數或負數值的向量和矩陣/灰度或彩色圖像/體素/向量場/點雲/張量/直方圖等。
Mat類代碼如下:
class CV_EXPORTS Mat
{
public:
// 標誌位
int flags;
// 數組的維數
int dims;
// 行數和列數
int rows, cols;
// 指向實際存儲數據的指針
uchar* data;
// 指針的引用計數器,當陣列指向用戶分配的數據時,指針爲零
int* refcount;
}
矩陣的頭是一個固定大小的結構,而它所指向的數據存儲於data指針指向的內存空間。
2. Mat類的構造函數與使用
Mat::Mat() | 無參構造函數 |
Mat::Mat(int rows, cols) | 創建行數爲rows,列數爲cols,類型爲type的圖像 |
Mat::Mat(Size size, int type) | 創建大小爲size,類型爲type的圖像 |
Mat::Mat(int rows, int cols, int type, const Scaler& s) | 創建行數爲rows,列數爲cols,類型爲type的圖像,並將圖像所有元素初始化爲s |
Mat::Mat(Size size, int type, const Scaler& s) | 創建大小爲size,類型爲type的圖像,並將所有元素初始化爲s |
Mat::Mat(const Mat& m) | 將m複製給新創建的對象,此處並不會對象相數據進行復制,二者公用圖像數據,即二者的data指針指向同一個地方 |
這裏的type是用來指定圖像的數據類型和同道數,常用的type由下表列出
type類型 | 含義 |
---|---|
CV_8UC1 | 單通道圖像,每個像素爲8位無符號數 |
CV_16SC1 | 單通道圖像,每個像素點爲16位有符號數 |
CV_64FC3 | 3通道圖像,每個像素點數據類型爲64位浮點數 |
Mat使用舉例
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
int main()
{
cv::Mat image1;
cv::Mat image2(6, 6, CV_8UC1);
cv::Mat image3(cv::Size(7, 7), CV_8UC3);
cv::Mat image4(8, 8, CV_32FC2, cv::Scalar(1, 3));
cv::Mat image5(cv::Size(9, 9), CV_8UC3, cv::Scalar(1,2,3));
cv::Mat image6(image2);
std::cout << "image1" << endl;
std::cout << image1 << std::endl;
std::cout << "image2" << endl;
std::cout << image2 << std::endl;
std::cout << "image3" << endl;
std::cout << image3 << std::endl;
std::cout << "image4" << endl;
std::cout << image4 << std::endl;
std::cout << "image5" << endl;
std::cout << image5 << std::endl;
std::cout << "image6" << endl;
std::cout << image6 << std::endl;
return 0;
}
3. Mat的一些常用成員函數
函數 | 功能 |
---|---|
Mat::row | 創建一個具有指定了矩陣頭中函數的參數的矩陣 |
Mat::col | 創建一個具有指定了矩陣頭中列數的參數的矩陣 |
Mat::rowRange | 爲指定的行span創建了一個新的矩陣頭,可取指定行區間元素 |
Mat::colRange | 爲指定的列span創建一個矩陣頭,可取指定的列區間元素 |
Mat::clone | 創建一個數組及其基礎數據的完整副本 |
Mat::copyTo | 把矩陣複製到另一個矩陣中 |
Mat::convertTo | 在縮放或不縮放的情況下轉換成另一種數據類型 |
Mat::channels | 返回矩陣的通道數 |
Mat::empty | 如果數組中沒有元素,則返回真 |
Mat::at | 返回對指定數據元素的引用 |
舉例:
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat Image1( 10, 8, CV_8UC1, Scalar(5) );
cout << "Image1 row: " << Image1.rows << endl;
cout << "Image1 col: " << Image1.cols << endl;
cout << Image1.rowRange(1, 3) << endl;
cout << Image1.colRange(2, 4) << endl;
Mat Image2( 8, 8, CV_32FC2, Scalar(1, 5) );
Image2.create( 10, 10, CV_8UC3 );
cout << "Image2 channels: " << Image2.channels() << endl;
Image2.convertTo( Image2, CV_32F );
cout << "Image2 depth: " << Image2.depth() << endl;
Mat Image3 = Mat::zeros( Image2.rows, Image2.cols, CV_8UC1 );
Image1.row(4) = Image1.row(5) * 2;
cout << Image1 << endl;
Mat Image4 = Image1.col(4);
cout << Image4 << endl;
Image1.col(1).copyTo(Image4);
cout << Image4 << endl;
return 0;
}
4. 圖像的讀寫與顯示
opencv提供了類似MATLAB中相類似的imread/imwrite/imshow等函數,具體情況如下
Mat imread( const string& filename, int flags=1 )
imread用於讀取圖像
filename: 圖像名
flags:表示讀取圖像的顏色類型,默認參數爲1,函數返回3通道圖像。參數flags有如下幾種常用設置方式:
flags | 含義 |
---|---|
CV_LOAD_IMAGE_ANYDEPTY | 返回圖像真正的位寬(16bit或者32bit),否則返回8bit |
CV_LOAD_IMAGE_COLOR | 返回單色圖像 |
CV_LOAD_IMAGE_GRAYSCALE | 返回通道圖像 |
<0 | 不對圖像進行通道轉換 |
0 | 返回單通道圖像 |
>0 | 強制返回3通道圖像 |
bool imwrite( const string& filename, InputArray img, const vector<int>& params=vector<int>() )
filename:寫入文件的格式及文件擴展名
img:表示待寫入的圖像數據
params:表示文件格式的一些信息
讀寫圖像舉例
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{
// 讀取圖像數據
Mat src_image = imread("./android.jpg");
// 判斷是否讀取成功
if ( src_image.empty() )
{
return -1;
}
Mat src_gray;
// rgb2gray
cvtColor(src_image, src_gray, COLOR_RGB2GRAY);
imwrite("src_gray.png", src_gray);
imshow("src_gray", src_gray);
Mat blur_dst_image;
// 圖像均值濾波
blur( src_gray, blur_dst_image, Size(5, 5), Point(-1, -1) );
imshow("blur_dst_image", blur_dst_image);
// 均值濾波後的圖像寫如到本地硬盤
imwrite("blur_dst_image.png", blur_dst_image);
waitKey(0);
return 0;
}