第四天:数据结构
声明Point类: PointNX, 例Point2f, Point3f
声明Mat类时, 需要指明数据类型和通道数: CV_{8U,16S,16U,32S,32F,32F,64F}, CV_{1,2,3,(N)}
一大堆函数…
#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include<iostream>
#include<fstream>
using namespace cv;
template <typename T>
void printMatrix(T mat){
for (size_t i = 0; i < mat.cols; i++)
{
for (size_t j = 0; j < mat.rows; j++)
std::cout<<mat.val[i*mat.rows+j]<<"\t";
std::cout<<std::endl;
}
}
int matrixCalc(){
Matx33f matrix = Matx33f::ones();
Vec3f vector = Vec3f(3,1,2);
Vec3f vector2 = matrix*vector;
printMatrix(vector2);
return 0;
}
计算一个3×3 float matrix 和 1×3 float vector 的点积并用泛型函数打印出结果.
第五天: Draw something use matrix
创建三通道二维矩阵,并绘制一个圆
#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include<iostream>
#include<fstream>
using namespace cv;
int main() {
Mat mat = Mat::zeros(100,100,CV_8UC3);
Scalar color = Scalar(200,21,25,15);
circle(mat, Point(50,50), 10, color,2,8,0);
imshow("Example1",mat);
waitKey(0);
return 0 ;
}

Something extra
读取一个图片,返回鼠标处的RGB值:
#include<opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include<iostream>
#include<fstream>
using namespace cv;
using namespace std;
void onMouseMove(int event, int x, int y, int flags, void* src){
if(event == EVENT_FLAG_LBUTTON){
Mat *img = (Mat*)src;
int b = img->at<Vec3b>(y,x)[0];
int g = img->at<Vec3b>(y,x)[1];
int r = img->at<Vec3b>(y,x)[2];
printf("Point(%d,%d): (%d,%d,%d)\n",x,y,r,g,b);
}
}
int main(int argc, char** argv){
if((argc!=2)){
cerr<<"Error: Only take an argument.";
return -1;
}
String picName = argv[1];
Mat img = imread(picName);
if(img.empty()){
cerr<<"Error: No such file.";
return -1;
}
imshow(picName,img);
setMouseCallback(picName,onMouseMove,&img);
waitKey(0);
return 0;
}

An improvement ( and a bug fix) on the side
#include<opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include<iostream>
#include<fstream>
#include <windows.h>
using namespace cv;
using namespace std;
int MouseDown=0;
void onMouseMove(int event, int x, int y, int flags, void* src){
Mat *img = (Mat*)src;
if(x<=0 || y<=0 || x>=img->cols || y>=img->rows)
return;
if(event==EVENT_LBUTTONDOWN)
MouseDown = 1;
if(event == EVENT_LBUTTONUP)
MouseDown = 0;
if((event == EVENT_MOUSEMOVE && MouseDown)||MouseDown){
int b = img->at<Vec3b>(y,x)[0];
int g = img->at<Vec3b>(y,x)[1];
int r = img->at<Vec3b>(y,x)[2];
printf("Point(%d,%d): (%d,%d,%d)\n",x,y,r,g,b);
}
}
int main(int argc, char** argv){
if((argc!=2)){
cerr<<"Error: Only take an argument.";
return -1;
}
String picName =argv[1];
Mat img = imread(picName);
if(img.empty()){
cerr<<"Error: No such file.";
return -1;
}
imshow(picName,img);
setMouseCallback(picName,onMouseMove,&img);
waitKey(0);
return 0;
}
Fix: Invalid coordinates crack.
Update: Press & Move to get continous values of pixels.
We may create a sketch program via this.
Another update: Change the if-condition to make single press avalible.