OpenCV中Mat数据结构使用举例
生活随笔
收集整理的這篇文章主要介紹了
OpenCV中Mat数据结构使用举例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include "stdafx.h"
#include <string>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
//創建一個用1+3j填充的 7 x 7 復矩陣-----1
Mat M(7, 7, CV_32FC2, Scalar(1,3));
//現在將 M轉換為100 x 60的CV_8UC(15)的矩陣,舊內容將會被釋放
M.create(100, 60, CV_8UC(15));//不能為矩陣設置初值
//第 5行,乘以 3,加到第 3 行,
M.row(3) = M.row(3) + M.row (5) * 3;
//現在將第7列復制到第1列, M.col(1) = M.col(7);//這個不能實現,對列操作時要新建一個Mat
Mat M1 = M.col(1);
M.col(7).copyTo(M1);
//創建一種新的 320 x 240 圖像-----2
Mat img(Size(320,240), CV_8UC3, Scalar::all(255));
string strWindowName = "ShowImage";
namedWindow(strWindowName, WINDOW_AUTOSIZE);
imshow(strWindowName, img);
waitKey(0);
//選擇ROI(region of interest)
Mat roi(img, Rect(10, 10, 100, 100));
//填充 (0,255,0) 的ROI (這是RGB 空間中的綠色),320 x 240 原始圖像將被修改
roi = Scalar(0, 255, 0) ;
imshow(strWindowName, img);
waitKey(0);
//獲取數組中的子塊-----3
Mat A = Mat::eye(10, 10, CV_32S);
//提取 A 的1 (含)到 3 (不包含)列
Mat B = A(Range::all(), Range(1, 3));
//提取 B 的5 (含)到 9 (不包含)行,即 C ~ A(Range(5,9),Range (1,3))
Mat C = B(Range(5, 9), Range::all());
Size size;
Point ofs;
C.locateROI(size, ofs);//使用locateROI() 計算子數組在主容器數組中的相對的位置
cout<<size.width<<" ?"<<size.height<<" ?"<<ofs.x<<" ?"<<ofs.y<<endl;
//快速初始化小矩陣-----4
double m[3][3] = {{1, 2, 3}, {1, 2, 5}, {3, 4, 6}};
Mat M2 = Mat(3, 3, CV_64F, m);//.inv();
Mat E = Mat::eye(4, 4, CV_64F); ? ?
cout<<"E = "<<endl<<" "<<E<<endl;
Mat O = Mat::ones(2, 2, CV_32F); ? ?
cout<<"O = "<<endl<<" "<<O<<endl;
Mat Z = Mat::zeros(3,3, CV_8UC1);
cout<<"Z = "<<endl<<" "<<Z<<endl;
//IplImage、Mat、CvMat互轉-----5
IplImage *img1 = cvLoadImage("aa.jpg", 2 | 4);
Mat mtx(img1);//IplImage *-> Mat,新的Mat類型與原來的IplImage類型共享圖像數據,轉換只是創建一個Mat矩陣頭// or : Mat mtx = img1;
CvMat oldmat = mtx;//Mat-> CvMat //只是創建矩陣頭,而沒有復制數據,oldmat不用手動釋放
CV_Assert((oldmat.cols == img1->width) && (oldmat.rows == img1->height) && (oldmat.data.ptr == (uchar *)img1->imageData) && (oldmat.step == img1->widthStep));
imshow(strWindowName, mtx);
waitKey(0);
cvNamedWindow(strWindowName.c_str(), 0);
cvShowImage(strWindowName.c_str(), &oldmat);
cvWaitKey(0);
IplImage img2 = mtx;//Mat->IplImage //只是創建圖像頭,而沒有復制數據,img2不用手動釋放
cvShowImage(strWindowName.c_str(), &img2);
cvWaitKey(0);
Mat mat3(&oldmat);//CvMat->Mat
imshow(strWindowName, mat3);
waitKey(0);
cvDestroyWindow(strWindowName.c_str());
cvReleaseImage(&img1);
//創建 3 x 3 雙精度恒等矩陣-----6
Mat M3 = (Mat_ <double>(3,3) <<1,0,0, 0,1,0, 0,0,1);
//訪問數組元素-----7
M2.at<double>(0, 0) += 10.f;
double sum = 0;//計算元素和,方法一
for (int i=0; i<M2.rows; i++)
{
const double *Mi = M2.ptr<double>(i) ;
for (int j=0; j<M2.cols; j++)
{
sum += std::max(Mi[j], 0.);
}
}
cout<<sum<<endl;
sum = 0;//計算元素和,方法二
int cols =M2.cols, rows = M2.rows ;
if (M2.isContinuous())
{
cols *= rows;
rows = 1 ;
}
for (int i=0; i<rows; i++)
{
const double *Mi = M2.ptr <double>(i);
for (int j=0; j<cols; j++)
{
sum += std::max(Mi[j], 0.);
}
}
cout<<sum<<endl;
sum = 0;//計算元素和,方法三
MatConstIterator_<double> ?it = M2.begin<double>(), it_end = M2.end<double>();
for(; it != it_end; ++it)
{
sum += std::max(*it, 0.);
}
cout<<sum<<endl;
return 0;
#include <string>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
//創建一個用1+3j填充的 7 x 7 復矩陣-----1
Mat M(7, 7, CV_32FC2, Scalar(1,3));
//現在將 M轉換為100 x 60的CV_8UC(15)的矩陣,舊內容將會被釋放
M.create(100, 60, CV_8UC(15));//不能為矩陣設置初值
//第 5行,乘以 3,加到第 3 行,
M.row(3) = M.row(3) + M.row (5) * 3;
//現在將第7列復制到第1列, M.col(1) = M.col(7);//這個不能實現,對列操作時要新建一個Mat
Mat M1 = M.col(1);
M.col(7).copyTo(M1);
//創建一種新的 320 x 240 圖像-----2
Mat img(Size(320,240), CV_8UC3, Scalar::all(255));
string strWindowName = "ShowImage";
namedWindow(strWindowName, WINDOW_AUTOSIZE);
imshow(strWindowName, img);
waitKey(0);
//選擇ROI(region of interest)
Mat roi(img, Rect(10, 10, 100, 100));
//填充 (0,255,0) 的ROI (這是RGB 空間中的綠色),320 x 240 原始圖像將被修改
roi = Scalar(0, 255, 0) ;
imshow(strWindowName, img);
waitKey(0);
//獲取數組中的子塊-----3
Mat A = Mat::eye(10, 10, CV_32S);
//提取 A 的1 (含)到 3 (不包含)列
Mat B = A(Range::all(), Range(1, 3));
//提取 B 的5 (含)到 9 (不包含)行,即 C ~ A(Range(5,9),Range (1,3))
Mat C = B(Range(5, 9), Range::all());
Size size;
Point ofs;
C.locateROI(size, ofs);//使用locateROI() 計算子數組在主容器數組中的相對的位置
cout<<size.width<<" ?"<<size.height<<" ?"<<ofs.x<<" ?"<<ofs.y<<endl;
//快速初始化小矩陣-----4
double m[3][3] = {{1, 2, 3}, {1, 2, 5}, {3, 4, 6}};
Mat M2 = Mat(3, 3, CV_64F, m);//.inv();
Mat E = Mat::eye(4, 4, CV_64F); ? ?
cout<<"E = "<<endl<<" "<<E<<endl;
Mat O = Mat::ones(2, 2, CV_32F); ? ?
cout<<"O = "<<endl<<" "<<O<<endl;
Mat Z = Mat::zeros(3,3, CV_8UC1);
cout<<"Z = "<<endl<<" "<<Z<<endl;
//IplImage、Mat、CvMat互轉-----5
IplImage *img1 = cvLoadImage("aa.jpg", 2 | 4);
Mat mtx(img1);//IplImage *-> Mat,新的Mat類型與原來的IplImage類型共享圖像數據,轉換只是創建一個Mat矩陣頭// or : Mat mtx = img1;
CvMat oldmat = mtx;//Mat-> CvMat //只是創建矩陣頭,而沒有復制數據,oldmat不用手動釋放
CV_Assert((oldmat.cols == img1->width) && (oldmat.rows == img1->height) && (oldmat.data.ptr == (uchar *)img1->imageData) && (oldmat.step == img1->widthStep));
imshow(strWindowName, mtx);
waitKey(0);
cvNamedWindow(strWindowName.c_str(), 0);
cvShowImage(strWindowName.c_str(), &oldmat);
cvWaitKey(0);
IplImage img2 = mtx;//Mat->IplImage //只是創建圖像頭,而沒有復制數據,img2不用手動釋放
cvShowImage(strWindowName.c_str(), &img2);
cvWaitKey(0);
Mat mat3(&oldmat);//CvMat->Mat
imshow(strWindowName, mat3);
waitKey(0);
cvDestroyWindow(strWindowName.c_str());
cvReleaseImage(&img1);
//創建 3 x 3 雙精度恒等矩陣-----6
Mat M3 = (Mat_ <double>(3,3) <<1,0,0, 0,1,0, 0,0,1);
//訪問數組元素-----7
M2.at<double>(0, 0) += 10.f;
double sum = 0;//計算元素和,方法一
for (int i=0; i<M2.rows; i++)
{
const double *Mi = M2.ptr<double>(i) ;
for (int j=0; j<M2.cols; j++)
{
sum += std::max(Mi[j], 0.);
}
}
cout<<sum<<endl;
sum = 0;//計算元素和,方法二
int cols =M2.cols, rows = M2.rows ;
if (M2.isContinuous())
{
cols *= rows;
rows = 1 ;
}
for (int i=0; i<rows; i++)
{
const double *Mi = M2.ptr <double>(i);
for (int j=0; j<cols; j++)
{
sum += std::max(Mi[j], 0.);
}
}
cout<<sum<<endl;
sum = 0;//計算元素和,方法三
MatConstIterator_<double> ?it = M2.begin<double>(), it_end = M2.end<double>();
for(; it != it_end; ++it)
{
sum += std::max(*it, 0.);
}
cout<<sum<<endl;
return 0;
}
參考文獻:
1、?http://blog.csdn.net/giantchen547792075/article/details/7061391
2、?http://blog.csdn.net/giantchen547792075/article/details/7169255
3、?http://blog.csdn.net/giantchen547792075/article/details/7318415
4、?http://blog.sina.com.cn/s/blog_534497fd01015k7z.html
5、?http://blog.csdn.net/wobuaishangdiao/article/details/7754580
6、?http://blog.csdn.net/yang_xian521/article/details/7107786
7、?http://blog.csdn.net/yang_xian521/article/details/7161335
8、 http://blog.csdn.net/caiye917015406/article/details/7791815總結
以上是生活随笔為你收集整理的OpenCV中Mat数据结构使用举例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV实现在图像中写入汉字
- 下一篇: Windows7在Notepad++中配