[OpenCV] Samples 02: [ML] kmeans
前言
一、简介
Mat是opencv2.0推出的处理图像的新的数据结构,现在越来越有趋势取代之前的cvMat和lplImage。
相比之下Mat最大的好处就是能够更加方便的进行内存管理,不再需要程序员手动管理内存的释放。
opencv2.3中提到Mat是一个多维的密集数据数组,可以用来处理向量和矩阵、图像、直方图等等常见的多维数据。
/* implement */
基本操作
操作Mat元素时:I.at<double>(1,1) = CV_PI;
本博客原内容
/*
*
* cvout_sample just demonstrates the serial out capabilities of cv::Mat
* That is, cv::Mat M(...); cout << M; Now works.
*
*/ #include "opencv2/core/core.hpp"
#include <iostream> using namespace std;
using namespace cv; static void help()
{
cout
<< "\n------------------------------------------------------------------\n"
<< " This program shows the serial out capabilities of cv::Mat\n"
<< "That is, cv::Mat M(...); cout << M; Now works.\n"
<< "Output can be formated to OpenCV, matlab, python, numpy, csv and \n"
<< "C styles Usage:\n"
<< "./cvout_sample\n"
<< "------------------------------------------------------------------\n\n"
<< endl;
} int main(int argc, char** argv)
{
cv::CommandLineParser parser(argc, argv, "{help h||}");
if (parser.has("help"))
{
help();
return ;
} /**************************************************************************/
// Jeff --> Define Diagnal Mat.
Mat I = Mat::eye(, , CV_64F);
I.at<double>(,) = CV_PI;
cout << "I = \n" << I << ";" << endl << endl; /*------------------------------------------------------------------------*/
Mat r = Mat(, , CV_8UC3);
randu(r, Scalar::all(), Scalar::all()); // Jeff --> Matrix Format transform.
cout << "r (default) = \n" << r << ";" << endl << endl;
cout << "r (matlab) = \n" << format(r, Formatter::FMT_MATLAB) << ";" << endl << endl;
cout << "r (python) = \n" << format(r, Formatter::FMT_PYTHON) << ";" << endl << endl;
cout << "r (numpy) = \n" << format(r, Formatter::FMT_NUMPY) << ";" << endl << endl;
cout << "r (csv) = \n" << format(r, Formatter::FMT_CSV) << ";" << endl << endl;
cout << "r (c) = \n" << format(r, Formatter::FMT_C) << ";" << endl << endl; /**************************************************************************/
Point2f p(, );
cout << "p = " << p << ";" << endl; /*------------------------------------------------------------------------*/
Point3f p3f(, , );
cout << "p3f = " << p3f << ";" << endl; /**************************************************************************/
// Jeff --> vector.
vector<float> v;
v.push_back(1.1);
v.push_back(2.2);
v.push_back(3.3); cout << "shortvec = " << Mat(v) << endl; /*------------------------------------------------------------------------*/
vector<Point2f> points();
for (size_t i = ; i < points.size(); ++i)
points[i] = Point2f((float)(i * ), (float)(i % )); cout << "points = " << points << ";" << endl;
return ;
}
Result: Matrix Format for不同的工具。
r (default) =
[ 91, 2, 79, 179, 52, 205, 236, 8, 181;
239, 26, 248, 207, 218, 45, 183, 158, 101;
102, 18, 118, 68, 210, 139, 198, 207, 211;
181, 162, 197, 191, 196, 40, 7, 243, 230;
45, 6, 48, 173, 242, 125, 175, 90, 63;
90, 22, 112, 221, 167, 224, 113, 208, 123;
214, 35, 229, 6, 143, 138, 98, 81, 118;
187, 167, 140, 218, 178, 23, 43, 133, 154;
150, 76, 101, 8, 38, 238, 84, 47, 7;
117, 246, 163, 237, 69, 129, 60, 101, 41]; r (matlab) =
(:, :, 1) =
91, 179, 236;
239, 207, 183;
102, 68, 198;
181, 191, 7;
45, 173, 175;
90, 221, 113;
214, 6, 98;
187, 218, 43;
150, 8, 84;
117, 237, 60
(:, :, 2) =
2, 52, 8;
26, 218, 158;
18, 210, 207;
162, 196, 243;
6, 242, 90;
22, 167, 208;
35, 143, 81;
167, 178, 133;
76, 38, 47;
246, 69, 101
(:, :, 3) =
79, 205, 181;
248, 45, 101;
118, 139, 211;
197, 40, 230;
48, 125, 63;
112, 224, 123;
229, 138, 118;
140, 23, 154;
101, 238, 7;
163, 129, 41; r (python) =
[[[ 91, 2, 79], [179, 52, 205], [236, 8, 181]],
[[239, 26, 248], [207, 218, 45], [183, 158, 101]],
[[102, 18, 118], [ 68, 210, 139], [198, 207, 211]],
[[181, 162, 197], [191, 196, 40], [ 7, 243, 230]],
[[ 45, 6, 48], [173, 242, 125], [175, 90, 63]],
[[ 90, 22, 112], [221, 167, 224], [113, 208, 123]],
[[214, 35, 229], [ 6, 143, 138], [ 98, 81, 118]],
[[187, 167, 140], [218, 178, 23], [ 43, 133, 154]],
[[150, 76, 101], [ 8, 38, 238], [ 84, 47, 7]],
[[117, 246, 163], [237, 69, 129], [ 60, 101, 41]]]; r (numpy) =
array([[[ 91, 2, 79], [179, 52, 205], [236, 8, 181]],
[[239, 26, 248], [207, 218, 45], [183, 158, 101]],
[[102, 18, 118], [ 68, 210, 139], [198, 207, 211]],
[[181, 162, 197], [191, 196, 40], [ 7, 243, 230]],
[[ 45, 6, 48], [173, 242, 125], [175, 90, 63]],
[[ 90, 22, 112], [221, 167, 224], [113, 208, 123]],
[[214, 35, 229], [ 6, 143, 138], [ 98, 81, 118]],
[[187, 167, 140], [218, 178, 23], [ 43, 133, 154]],
[[150, 76, 101], [ 8, 38, 238], [ 84, 47, 7]],
[[117, 246, 163], [237, 69, 129], [ 60, 101, 41]]], dtype='uint8'); r (csv) =
91, 2, 79, 179, 52, 205, 236, 8, 181
239, 26, 248, 207, 218, 45, 183, 158, 101
102, 18, 118, 68, 210, 139, 198, 207, 211
181, 162, 197, 191, 196, 40, 7, 243, 230
45, 6, 48, 173, 242, 125, 175, 90, 63
90, 22, 112, 221, 167, 224, 113, 208, 123
214, 35, 229, 6, 143, 138, 98, 81, 118
187, 167, 140, 218, 178, 23, 43, 133, 154
150, 76, 101, 8, 38, 238, 84, 47, 7
117, 246, 163, 237, 69, 129, 60, 101, 41
; r (c) =
{ 91, 2, 79, 179, 52, 205, 236, 8, 181,
239, 26, 248, 207, 218, 45, 183, 158, 101,
102, 18, 118, 68, 210, 139, 198, 207, 211,
181, 162, 197, 191, 196, 40, 7, 243, 230,
45, 6, 48, 173, 242, 125, 175, 90, 63,
90, 22, 112, 221, 167, 224, 113, 208, 123,
214, 35, 229, 6, 143, 138, 98, 81, 118,
187, 167, 140, 218, 178, 23, 43, 133, 154,
150, 76, 101, 8, 38, 238, 84, 47, 7,
117, 246, 163, 237, 69, 129, 60, 101, 41};
[OpenCV] Samples 02: [ML] kmeans的更多相关文章
- [OpenCV] Samples 06: [ML] logistic regression
logistic regression,这个算法只能解决简单的线性二分类,在众多的机器学习分类算法中并不出众,但它能被改进为多分类,并换了另外一个名字softmax, 这可是深度学习中响当当的分类算法 ...
- [OpenCV] Samples 02: Mat - 图像矩阵
前言 一.简介 Ref:IplImage, CvMat, Mat 的关系 Mat是opencv2.0推出的处理图像的新的数据结构,现在越来越有趋势取代之前的cvMat和lplImage. 相比之下Ma ...
- [OpenCV] Samples 10: imagelist_creator
yaml写法的简单例子.将 $ ./ 1 2 3 4 5 命令的参数(代表图片地址)写入yaml中. 写yaml文件. 参考:[OpenCV] Samples 06: [ML] logistic re ...
- [OpenCV] Samples 16: Decompose and Analyse RGB channels
物体的颜色特征决定了灰度处理不是万能,对RGB分别处理具有相当的意义. #include <iostream> #include <stdio.h> #include &quo ...
- [OpenCV] Samples 03: kmeans
注意Mat作为kmeans的参数的含义. 扩展:高维向量的聚类. 一.像素聚类 #include "opencv2/highgui.hpp" #include "open ...
- [OpenCV] Samples 06: logistic regression
logistic regression,这个算法只能解决简单的线性二分类,在众多的机器学习分类算法中并不出众,但它能被改进为多分类,并换了另外一个名字softmax, 这可是深度学习中响当当的分类算法 ...
- [OpenCV] Samples 03: cout_mat
操作Mat元素时:I.at<double>(1,1) = CV_PI; /* * * cvout_sample just demonstrates the serial out capab ...
- OpenCV学习(23) 使用kmeans算法实现图像分割
本章我们用kmeans算法实现一个简单图像的分割.如下面的图像,我们知道图像分3个簇,背景.白色的任务,红色的丝带以及帽子. Mat img = cv::imread(&quo ...
- [OpenCV] Samples 13: opencv_version
cv::CommandLineParser的使用. I suppose CommandLineParser::has("something") should be true whe ...
随机推荐
- PADSPCB权威指南-第一章 PADS软件系统(部分)(原创)
PADSPCB权威指南-第一章(部分)豆丁地址:http://www.docin.com/p-707128286.html
- Hibernate快照
l当session加载了某个对象后,会为该对象的值类型的属性复制一份快照.当刷出缓存时,通过比较对象的当前属性和快照,来判断对象的哪些属性发生了变化. 代码如下: /** * 修改学生信息 */ @T ...
- day9---多线程,线程锁,队列
进程.线程 http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html 使用threading模块实现多线程编程[综述] Pyt ...
- 【MySQL】事务没有提交导致 锁等待Lock wait timeout exceeded异常
异常:Lock wait timeout exceeded; try restarting transaction 解决办法:(需要数据库最高权限) 执行select * from informati ...
- android简单登陆和注册功能实现+SQLite数据库学习
最近初学android,做了实验室老师给的基本任务,就是简单的登陆和注册,并能通过SQLite实现登陆,SQlLite是嵌入在安卓设备中的 好了下面是主要代码: 数据库的建立: 这里我只是建立了一个用 ...
- 搜索栏css代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- delphi 中如何查看网页POSTDATA
procedure TForm1.WebBrowser1NewWindow2(Sender: TObject; var ppDisp: IDispatch; var Cancel: WordBool ...
- java jinfo命令详解
jinfo (configuration info): 功能:输出Java进程的系统信息与jvm参数. 摘要: jinfo [ option ] pid jinfo [ option ] execut ...
- Swift 对比学习 (二)
书接上回,可以作为参数和返回值的函数数型,以及嵌套函数,绝对继承了动态语言的优良传统: 函数嵌套了,那必然少不了闭包问题,Swift的闭包表达式语法也蛮有趣的. { (paraeeters) -> ...
- SWT: 发起事件 post event
有很多学习SWT的同志遇到过一类需求,为某些控件添加了诸如MouseListener.KeyListener之类的监听,然后呢,希望使用代码模拟鼠标.键盘来执行点击.按键等操作. 首先说明一点,这是可 ...