https://blog.csdn.net/u014337397/article/details/80834689
rgb转成mat
Mat image(IMAGE_HEIGHT, IMAGE_WIDTH, CV_8UC3, buffer);//buffer为转完的rgb数据
实例:
Mat depthout(cv::Size(3072, 2048), CV_8UC3, stConvertParam.pDstBuffer);
原文链接:https://blog.csdn.net/wanggao_1990/article/details/53150926
https://blog.csdn.net/maweifei/article/details/51221259
(1) Mat::Mat()
(2) Mat::Mat(int rows, int cols, int type)
(3) Mat::Mat(Size size, int type)
(4) Mat::Mat(int rows, int cols, int type, constScalar& s)
(5) Mat::Mat(Size size, int type, constScalar& s)
(6) Mat::Mat(const Mat& m)
(7) Mat::Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP)
(8) Mat::Mat(Size size, int type, void* data, size_t step=AUTO_STEP)
(9) Mat::Mat(const Mat& m, const Range& rowRange, const Range& colRange)
(10) Mat::Mat(const Mat& m, const Rect& roi)
(11) Mat::Mat(const CvMat* m, bool copyData=false)
(12) Mat::Mat(const IplImage* img, bool copyData=false)
(13) template
(14) template
(15) template explicit Mat::Mat(const vector& vec, bool copyData=false)
(16) Mat::Mat(const MatExpr& expr)
(17) Mat::Mat(int ndims, const int* sizes, int type)
(18) Mat::Mat(int ndims, const int* sizes, int type, constScalar& s)
(19) Mat::Mat(int ndims, const int* sizes, int type, void* data, const size_t* steps=0)
(20) Mat::Mat(const Mat& m, const Range* ranges)
延伸:OpenCv学习笔记(二)–Mat矩阵(图像容器)的创建及CV_8UC1,CV_8UC2等参数详解
https://blog.csdn.net/shenwanjiang111/article/details/54318958
Mat的一个构造函数 C++: Mat::Mat(int rows, int cols, int type, const Scalar& s) ,其中rows和cols是需要创建的矩阵的行数和列数,type是Mat的数据类型,s是Scalar类型的矩阵初值。
https://blog.csdn.net/weixin_42755384/article/details/88561526
参考代码:
#include
#include
#include "core/core.hpp"
#include "highgui/highgui.hpp"
#include "imgproc/imgproc.hpp"
//连续画矩形框
using namespace cv;
using namespace std;
//去掉以上两行代码注释之后就可以不用在下面的代码中加上cv::和std::
//cv::为OpenCV的函数库
//std::为C++的标准函数库
void OnMouseAction(int event, int x, int y, int flags, void *ustc); //鼠标回调事件函数
Rect rect,temp;
Mat src, src1;
void draw_rec(Mat str, Mat src1);
void draw_rec1(Mat str, Mat src1);
int static times; //记录调用次数
int DrawRect();
int rec_num;
int main(int argc, char*argv[])
{
//以下的方法如果想运行,直接把前头的注释去掉即可
//运行代码的时候请改掉filename里的地址
//我的opencv是安装在C盘目录下
//一些目录地址请自行修改一下
//DrawRect(); //在图像上画四边形
const char* filename = "E:\\projects\\darknet-master\\build\\darknet\\x64\\data\\voc\\VOCdevkit\\VOC2007\\JPEGImages\\img1.bmp";
src = imread(filename);
imshow("title", src);
rec_num = 0;
setMouseCallback("title", OnMouseAction, 0);
waitKey(0);
return 0;
}
//*******************************************************************//
//鼠标回调函数
void OnMouseAction(int event, int x, int y, int flags, void *ustc)
{
times++;
switch (event)
{
//左键按下事件
case EVENT_LBUTTONDOWN:
//左键按下 定义起始点
rect.x = x;
rect.y = y;
rect.width = 1;
rect.height = 1;
cout << "触发左键按下坐标为" << x << "," << y << endl;
break;
//鼠标移动事件
case CV_EVENT_MOUSEMOVE:
//当左键按下时根据左键起始点绘制生成的矩形
if (flags&EVENT_FLAG_LBUTTON)
{
rect = Rect(Point(rect.x, rect.y), Point(x, y));
draw_rec1(src,src1); //鼠标移动过程中显示移动轨迹
}
break;
//左键松开事件
case CV_EVENT_LBUTTONUP:
if (rect.width > 1 && rect.height > 1)
{
draw_rec(src,src1);
rec_num++;
}
cout << "触发左键松开坐标为" << x << "," << y << endl;
cout << "选中区域个数为" << rec_num << endl;
default:
break;
}
//cout << "第 " << times << " 次回调鼠标事件" << endl;
//if (event == CV_EVENT_MOUSEMOVE)
//{
// cout << "触发鼠标移动事件" << endl;
//}
//if (event == CV_EVENT_LBUTTONDOWN)
//{
// cout << "触发左键按下事件" << endl;
//}
//if (event == CV_EVENT_LBUTTONUP)
//{
// cout << "触发左键抬起事件" << endl;
//}
//if (event == CV_EVENT_RBUTTONDOWN)
//{
// cout << "触发右键按下事件" << endl;
// DrawRect(); //在图像上画四边形
//}
//if (event == CV_EVENT_RBUTTONUP)
//{
// cout << "触发右键抬起事件" << endl;
//}
//if (event == CV_EVENT_LBUTTONDBLCLK)
//{
// cout << "触发左键双击事件" << endl;
//}
//if (event == CV_EVENT_RBUTTONDBLCLK)
//{
// cout << "触发右键双击事件" << endl;
//}
}
void draw_rec(Mat src, Mat src1)
{
src.copyTo(src1);
rectangle(src1, rect, Scalar(0, 0, 255));
src1.copyTo(src);
imshow("title", src1);
}
void draw_rec1(Mat src, Mat src1)
{
src.copyTo(src1);
rectangle(src1, rect, Scalar(0, 0, 255));
imshow("title", src1);
}
int DrawRect() {
const char* filename = "E:\\projects\\darknet-master\\build\\darknet\\x64\\data\\voc\\VOCdevkit\\VOC2007\\JPEGImages\\img1.bmp";
cv::Mat mat = cv::imread(filename);
if (mat.empty()) {
throw("Faild open file.");
}
cv::Point p0 = cv::Point(mat.cols / 8, mat.rows / 8);
cv::Point p1 = cv::Point(mat.cols * 7 / 8, mat.rows * 7 / 8);
//设定点的起始和终止坐标
rectangle(mat, p0, p1, cv::Scalar(0, 255, 0), 5, 8);
//画四边形的函数
//第一个参数为画图的目标图像
//第二个参数为画图的起始坐标
//第三个参数为画图的终止坐标
//第四个参数为画图的颜色
cv::Point p2 = cv::Point(mat.cols * 2 / 8, mat.rows * 2 / 8);
cv::Point p3 = cv::Point(mat.cols * 6 / 8, mat.rows * 6 / 8);
rectangle(mat, p2, p3, cv::Scalar(0, 255, 255), 2, 4);
cv::imshow("mat", mat);
//cv::imwrite("C:\\Code\\FirstOpenCVProgramming\\DrawRect.jpg", mat);
cv::waitKey();
return 0;
}
在显示循环中加入代码:
if(!cvGetWindowHandle("窗口名字"))
{
break;
}
https://www.cnblogs.com/ybqjymy/p/13650613.html
计算矩阵的均值和标准偏差。
C++:
void meanStdDev(InputArray src,OutputArray mean, OutputArray stddev, InputArray mask=noArray())
C:
void cvAvgSdv(constCvArr* arr, CvScalar* mean, CvScalar* std_dev, const CvArr* mask=NULL )
Python:
cv.AvgSdv(arr,mask=None) -> (mean, stdDev)
parameter:
src:输入矩阵,这个矩阵应该是1-4通道的,这可以将计算结果存在Scalar_ ‘s中
mean:输出参数,计算均值
stddev:输出参数,计算标准差
mask:可选参数
原文链接:https://blog.csdn.net/weixin_42029090/article/details/80618208
https://blog.csdn.net/qq_29540745/article/details/52562101
void setMousecallback(const string& winname, MouseCallback onMouse, void* userdata=0)
winname:窗口的名字
onMouse:鼠标响应函数,回调函数。指定窗口里每次鼠标时间发生的时候,被调用的函数指针。 这个函数的原型应该为void on_Mouse(int event, int x, int y, int flags, void* param);
userdate:传给回调函数的参数
附常用的event:
#defineCV_EVENT_MOUSEMOVE
#defineCV_EVENT_LBUTTONDOWN
#defineCV_EVENT_RBUTTONDOWN
#defineCV_EVENT_LBUTTONUP
#defineCV_EVENT_RBUTTONUP
和标志位flags有关的:
#defineCV_EVENT_FLAG_LBUTTON
注意: flags & CV_EVENT_FLAG_LBUTTON 的意思是 提取flags的CV_EVENT_FLAG_LBUTTON 标志位,!()的意思是 标志位无效
Opencv中setMouseCallback()创建了一个鼠标回调函数,每次在图像上单击鼠标左键再抬起的过程,都会分3次调用鼠标响应函数,并且响应顺序是:
1.左键单击按下;
2.左键单击抬起;
3.鼠标指针位置移动(即使原地单击,鼠标位置并没有移动);
原因:内存溢出,将路径中斜杠改为“\”