OpenCV官方文档学习记录(1)
2014-12-02 23:26
459 查看
图像显示并转化为黑白输出到新文件
code:
结果:
Mat数据结构:
结果:
以上显示的是浅拷贝,就是除了headers不同之外,其余的数据都是从一块数据块中取得的。
要实现深拷贝:
或是:
构建Mat对象:
构造器参数,行+列+图像类型+填充数据。
模仿MatLab的函数:
因为默认重载了流运算符,因此可以直接输出;
对于小矩阵使用逗号初始化:
可以按行列复制图像的色值:
随机生成矩阵方式:
randu后两个参数是取值范围;
矩阵值的输出格式:
其他可以直接用流输出的数据类型是
计算算法运行时间:
下篇待续。。。
以上。
code:
#include <opencv2\opencv.hpp> #include <iostream> #include <string> #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) using namespace std; using namespace cv; int main() { Mat img = imread("test.jpg",1); if (img.empty()) { return -1; } Mat gray_img; cvtColor(img, gray_img, CV_BGR2GRAY); imwrite("./black.jpg", gray_img); namedWindow("Source", CV_WINDOW_AUTOSIZE); imshow("Source", img); namedWindow("Black", CV_WINDOW_AUTOSIZE); imshow("Black", gray_img); waitKey(); return 0; }
结果:
Mat数据结构:
#include <opencv2\opencv.hpp> #include <iostream> #include <string> #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) using namespace std; using namespace cv; void Show(std::string name,Mat img) { namedWindow(name, CV_WINDOW_AUTOSIZE); imshow(name, img); } int main() { Mat A, C; A = imread("test.jpg", 1); Mat B(A); C = A; Mat D(A, Rect(10, 10, 100, 100)); Mat E = A(Range::all(), Range(1, 3)); Show("A", A); Show("B", B); Show("C", C); Show("D", D); Show("E", E); waitKey(); return 0; }
结果:
以上显示的是浅拷贝,就是除了headers不同之外,其余的数据都是从一块数据块中取得的。
要实现深拷贝:
#include <opencv2\opencv.hpp> #include <iostream> #include <string> #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) using namespace std; using namespace cv; void Show(std::string name,Mat img) { namedWindow(name, CV_WINDOW_AUTOSIZE); imshow(name, img); } int main() { Mat img = imread("test.jpg", 1); Show("Source", img); Mat dest = img.clone(); Show("Dest", dest); waitKey(); return 0; }
或是:
#include <opencv2\opencv.hpp> #include <iostream> #include <string> #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) using namespace std; using namespace cv; void Show(std::string name,Mat img) { namedWindow(name, CV_WINDOW_AUTOSIZE); imshow(name, img); } int main() { Mat img = imread("test.jpg", 1); Show("Source", img); Mat dest; img.copyTo(dest); Show("Dest", dest); waitKey(); return 0; }
构建Mat对象:
#include <opencv2\opencv.hpp> #include <iostream> #include <string> //#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) using namespace std; using namespace cv; void Show(std::string name,Mat img) { namedWindow(name, CV_WINDOW_AUTOSIZE); imshow(name, img); } int main() { Mat M(2, 2, CV_8UC3, Scalar(0, 0, 255)); cout << "M=" << endl << "" << M << endl << endl; system("pause"); return 0; }
构造器参数,行+列+图像类型+填充数据。
模仿MatLab的函数:
#include <opencv2\opencv.hpp> #include <iostream> #include <string> //#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) using namespace std; using namespace cv; void Show(std::string name,Mat img) { namedWindow(name, CV_WINDOW_AUTOSIZE); imshow(name, img); } int main() { Mat E = Mat::eye(4, 4, CV_64F); cout << E << endl; Mat O = Mat::ones(2, 2, CV_32F); cout << O << endl; Mat Z = Mat::zeros(3, 3, CV_8UC1); cout << Z << endl; system("pause"); return 0; }
因为默认重载了流运算符,因此可以直接输出;
对于小矩阵使用逗号初始化:
#include <opencv2\opencv.hpp> #include <iostream> #include <string> //#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) using namespace std; using namespace cv; void Show(std::string name,Mat img) { namedWindow(name, CV_WINDOW_AUTOSIZE); imshow(name, img); } int main() { Mat C = (Mat_<double>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0); cout << C << endl; system("pause"); return 0; }
可以按行列复制图像的色值:
#include <opencv2\opencv.hpp> #include <iostream> #include <string> //#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) using namespace std; using namespace cv; void Show(std::string name,Mat img) { namedWindow(name, CV_WINDOW_AUTOSIZE); imshow(name, img); } int main() { Mat C = (Mat_<double>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0); Mat Row = C.col(1).clone(); cout << Row << endl; system("pause"); return 0; }
随机生成矩阵方式:
#include <opencv2\opencv.hpp> #include <iostream> #include <string> #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) using namespace std; using namespace cv; void Show(std::string name,Mat img) { namedWindow(name, CV_WINDOW_AUTOSIZE); imshow(name, img); } int main() { Mat img; img = Mat(400, 400, CV_8UC3); randu(img, Scalar::all(0), Scalar::all(255)); Show("Img", img); waitKey(); return 0; }
randu后两个参数是取值范围;
矩阵值的输出格式:
#include <opencv2\opencv.hpp> #include <iostream> #include <string> //#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) using namespace std; using namespace cv; void Show(std::string name,Mat img) { namedWindow(name, CV_WINDOW_AUTOSIZE); imshow(name, img); } int main() { Mat img; img = Mat(4, 4, CV_8UC3,Scalar::all(25)); cout << img << endl; cout << format(img, "python") << endl; cout << format(img, "csv") << endl; cout << format(img, "numpy") << endl; cout << format(img, "C") << endl; system("pause"); return 0; }
其他可以直接用流输出的数据类型是
#include <opencv2\opencv.hpp> #include <iostream> #include <string> //#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) using namespace std; using namespace cv; void Show(std::string name,Mat img) { namedWindow(name, CV_WINDOW_AUTOSIZE); imshow(name, img); } int main() { Point2f p2(5, 1); cout << p2 << endl; Point3f p3(2, 6, 7); cout << p3 << endl; vector<float> v; v.push_back((float)CV_PI); v.push_back(2); v.push_back(3.01f); cout << Mat(v) << endl; vector<Point2f> vPoints(20); for (size_t i = 0; i < vPoints.size(); ++i) vPoints[i] = Point2f((float)(i * 5), (float)(i % 7)); cout << vPoints << endl; system("pause"); return 0; }
计算算法运行时间:
#include <opencv2\opencv.hpp> #include <iostream> #include <string> #include <sstream> //#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) using namespace std; using namespace cv; void Show(std::string name,Mat img) { namedWindow(name, CV_WINDOW_AUTOSIZE); imshow(name, img); } int main() { double t = getTickCount(); //do somethings t = ((double)getTickCount() - t) / getTickFrequency(); cout << t << endl; system("pause"); return 0; }
下篇待续。。。
以上。
相关文章推荐
- OpenCV官方文档学习记录(3)
- OpenCV官方文档学习记录(8)
- OpenCV官方文档学习记录(20)
- OpenCV官方文档学习记录(4)
- OpenCV官方文档学习记录(15)
- OpenCV官方文档学习记录(17)
- OpenCV官方文档学习记录(6)
- OpenCV官方文档学习记录(14)
- OpenCV官方文档学习记录(10)
- OpenCV官方文档学习记录(7)
- OpenCV官方文档学习记录(12)
- OpenCV官方文档学习记录(13)
- OpenCV官方文档学习记录(19)
- OpenCV官方文档学习记录(16)
- OpenCV官方文档学习记录(11)
- OpenCV官方文档学习记录(18)
- OpenCV官方文档学习记录(9)
- OpenCV官方文档学习记录(5)
- OpenCV官方文档学习记录(2)
- OpenCV基础入门 基于官方文档解读(4)--core模块学习