知识点:
getRotationMatrix2D
(Point(w / 2, h / 2), 45, 1.0);//第一个参数表示center:旋转中心坐标,是一个元组参数(col, row);第二个参数表示angle:旋转角度,旋转方向,负号为逆时针,正号为顺时针;第三个参数表示scale:旋转后图像相比原来的缩放比例,1为等比例缩放。warpAffine
(image, dst, M, Size(nw, nh), INTER_LINEAR, 0, Scalar(0, 0, 255));//image表示输入图像;dst为输出图像;M表示矩阵变换关系,通常是平移或者旋转;Size(nw, nh)输出图像的尺寸;INTER_LINEAR表示线性插值;0表示边界像素模式;Scalar(0, 0, 255)表示颜色。#include
using namespace cv;
class QuickDemo //QuickDemo对象
{
public:
void rotate_demo(Mat &image);
};
#include
#include
void QuickDemo::rotate_demo(Mat &image)
{
Mat dst, M;
int h = image.rows;//定义图片的高度
int w = image.cols;//定义图片的宽度
M = getRotationMatrix2D(Point(w / 2, h / 2), 45, 1.0);//中心的,角度,缩放比例
double cos = abs(M.at<double>(0, 0));
double sin = abs(M.at<double>(0, 1));
int nw = cos * w + sin * h;
int nh = sin * w + cos * h;
M.at<double>(0, 2) += (nw / 2 - w / 2);
M.at<double>(1, 2) += (nh / 2 - h / 2);
//输入图像,输出图像,变换矩阵,输出尺寸;插值符号,边界像素模式,颜色
warpAffine(image, dst, M, Size(nw, nh), INTER_LINEAR, 0, Scalar(0, 0, 255));
imshow("旋转演示", dst);
}
#include
#include
#include
using namespace std;
using namespace cv;
int main(int argc,char **argv)
{
Mat src = imread("D:/opencv/pictures/3 (3).jpeg", IMREAD_ANYCOLOR);//B,G,R蓝绿红通道图像
if (src.empty())
{
printf("could not load image"); //如果路径不正确,则显示无法导入图片。
return -1;
}
namedWindow("输入窗口", WINDOW_AUTOSIZE);
imshow("输入窗口", src); //在窗口显示图像
QuickDemo qd; //调用之前创建的类对象
qd.rotate_demo(src);
waitKey(0); //暂停程序,等待一个按键输入,单位ms
destroyAllWindows(); //销毁所有创建的窗口
return 0;
}
知识点:
VideoCapture capture
(“D:/opencv/pictures/1234.mp4”); //读取视频的地址capture.read(frame)
; //读取视频capture.release()
;//释放资源#include
using namespace cv;
class QuickDemo //QuickDemo对象
{
public:
void video_demo(Mat &image);
void colorSpace_Demo(Mat &image);
};
#include
#include
void QuickDemo::colorSpace_Demo(Mat &image)
{
Mat gray, hsv;
cvtColor(image, hsv, COLOR_BGR2HSV);
cvtColor(image, gray, COLOR_BGR2GRAY);
imshow("HSV", hsv);
imshow("灰度", gray);
}
void QuickDemo::video_demo(Mat &image)
{
VideoCapture capture("D:/opencv/pictures/1234.mp4"); //读取视频的地址
//VideoCapture capture(0);//读取当前摄像头的图像
Mat frame;//定义一个二值化的 frame
while (true)
{
capture.read(frame); //读取视频
//flip(frame, frame, 1);//对视频帧进行翻转操作
if (frame.empty())//保证视频不为空
{
break;
}
imshow("frame", frame);//显示视频
colorSpace_Demo(frame);//对视频帧进行颜色空间转换
int c = waitKey(100);
if (c == 27) { //esc 退出
break;
}
}
capture.release();//释放资源
}
#include
#include
#include
using namespace std;
using namespace cv;
int main(int argc,char **argv)
{
Mat src = imread("D:/opencv/pictures/3 (3).jpeg", IMREAD_ANYCOLOR);//B,G,R蓝绿红通道图像
if (src.empty())
{
printf("could not load image"); //如果路径不正确,则显示无法导入图片。
return -1;
}
namedWindow("输入窗口", WINDOW_AUTOSIZE);
imshow("输入窗口", src); //在窗口显示图像
QuickDemo qd; //调用之前创建的类对象
qd.video_demo(src);
waitKey(0); //暂停程序,等待一个按键输入,单位ms
destroyAllWindows(); //销毁所有创建的窗口
return 0;
}
知识点:
capture.get(CAP_PROP_FRAME_WIDTH)
;//获取视频宽capture.get(CAP_PROP_FRAME_HEIGHT)
;//获取视频高capture.get(CAP_PROP_FRAME_COUNT)
;//获取视频总帧数capture.get(CAP_PROP_FPS)
;//获取视频的FPSVideoWriter writer("D:/opencv/pictures//test.mp4", capture.get(CAP_PROP_FOURCC), fps, Size(frame_width, frame_height), true)
;//保存视频。参数1输出文件名;参数2编码形式;参数3输出帧率;参数4输出尺寸;参数5如果为False,可以传入灰度图片。#include
using namespace cv;
class QuickDemo //QuickDemo对象
{
public:
void video_demo2(Mat &image);
void colorSpace_Demo(Mat &image);
};
#include
#include
void QuickDemo::colorSpace_Demo(Mat &image)
{
Mat gray, hsv;
cvtColor(image, hsv, COLOR_BGR2HSV);
cvtColor(image, gray, COLOR_BGR2GRAY);
imshow("HSV", hsv);
imshow("灰度", gray);
}
void QuickDemo::video_demo2(Mat &image)
{
VideoCapture capture("D:/opencv/pictures/1234.mp4");
int frame_width = capture.get(CAP_PROP_FRAME_WIDTH);//获取视频宽
int frame_height = capture.get(CAP_PROP_FRAME_HEIGHT);//获取视频高
int count = capture.get(CAP_PROP_FRAME_COUNT);//获取视频总帧数
double fps = capture.get(CAP_PROP_FPS);//获取视频的FPS
//打印上面的这些数值
std::cout << "frame width" << frame_width << std::endl;
std::cout << "frame height" << frame_height << std::endl;
std::cout << "frame FPS" << fps << std::endl;
std::cout << "frame count" << count << std::endl;
//保存视频
VideoWriter writer("D:/opencv/pictures//test.mp4", capture.get(CAP_PROP_FOURCC), fps, Size(frame_width, frame_height), true);
Mat frame;
while (true)
{
capture.read(frame);
//flip(frame, frame, 1);//翻转操作
if (frame.empty())
{
break;
}
imshow("frame", frame);
colorSpace_Demo(frame);
writer.write(frame);
int c = waitKey(100);
if (c == 27) { //esc 退出程序
break;
}
}
capture.release();//释放相机的资源
writer.release();//释放存放的资源
}
#include
#include
#include
using namespace std;
using namespace cv;
int main(int argc,char **argv)
{
Mat src = imread("D:/opencv/pictures/3 (3).jpeg", IMREAD_ANYCOLOR);//B,G,R蓝绿红通道图像
if (src.empty())
{
printf("could not load image"); //如果路径不正确,则显示无法导入图片。
return -1;
}
namedWindow("输入窗口", WINDOW_AUTOSIZE);
imshow("输入窗口", src); //在窗口显示图像
QuickDemo qd; //调用之前创建的类对象
qd.video_demo2(src);
waitKey(0); //暂停程序,等待一个按键输入,单位ms
destroyAllWindows(); //销毁所有创建的窗口
return 0;
}
知识点:
calcHist
(&bgr_plane[0], 1, 0, Mat(), b_hist, 1, bins, ranges);//参数分别为:源图像矩阵;源图像数量;源图像通道;掩膜;直方图输出值;输出直方图的维度;统计像素值的区间;等。#include
using namespace cv;
class QuickDemo //QuickDemo对象
{
public:
void histogram_demo(Mat &image);
};
#include
#include
void QuickDemo::histogram_demo(Mat &image)
{
//三通道分离
std::vector<Mat>bgr_plane;
split(image, bgr_plane);
//定义参数变量
const int channels[1] = { 0 };
const int bins[1] = { 256 };
float hranges[2] = { 0,255 };
const float* ranges[1] = { hranges };
Mat b_hist;
Mat g_hist;
Mat r_hist;
//计算Blue,Green,Red通道的直方图
calcHist(&bgr_plane[0], 1, 0, Mat(), b_hist, 1, bins, ranges);
calcHist(&bgr_plane[1], 1, 0, Mat(), g_hist, 1, bins, ranges);
calcHist(&bgr_plane[2], 1, 0, Mat(), r_hist, 1, bins, ranges);
//显示直方图
int hist_w = 480;
int hist_h = 320;
int bin_w = cvRound((double)hist_w / bins[0]);
Mat histImage = Mat::zeros(hist_h, hist_w, CV_8UC3);
//归一化直方图数据
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
//绘制直方图曲线
for (int i = 1; i < bins[0]; i++)
{
line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(b_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h - cvRound(b_hist.at<float>(i))), Scalar(255, 0, 0), 2, 8, 0);
line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(g_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h - cvRound(g_hist.at<float>(i))), Scalar(0, 255, 0), 2, 8, 0);
line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(r_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h - cvRound(r_hist.at<float>(i))), Scalar(0, 0, 255), 2, 8, 0);
}
//显示直方图
namedWindow("Histogram Demo", WINDOW_AUTOSIZE);
imshow("Histogram Demo", histImage);
}
#include
#include
#include
using namespace std;
using namespace cv;
int main(int argc,char **argv)
{
Mat src = imread("D:/opencv/pictures/3 (3).jpeg", IMREAD_ANYCOLOR);//B,G,R蓝绿红通道图像
if (src.empty())
{
printf("could not load image"); //如果路径不正确,则显示无法导入图片。
return -1;
}
namedWindow("输入窗口", WINDOW_AUTOSIZE);
imshow("输入窗口", src); //在窗口显示图像
QuickDemo qd; //调用之前创建的类对象
qd.histogram_demo(src);
waitKey(0); //暂停程序,等待一个按键输入,单位ms
destroyAllWindows(); //销毁所有创建的窗口
return 0;
}
知识点:
applyColorMap
(hist2d_image,hist2d_image,COLORMAP_JET);//参数分别为:源图像;输出图像;可提供色彩代码值。#include
using namespace cv;
class QuickDemo //QuickDemo对象
{
public:
void histogram_2d_demo(Mat &image);
};
#include
#include
void QuickDemo::histogram_2d_demo(Mat &image)
{
//2D直方图
Mat hsv;
Mat hs_hist;
cvtColor(image, hsv, COLOR_BGR2HSV);
int hbins = 30, sbins = 32;
int hist_bins[] = { hbins,sbins };
float h_range[] = { 0,180 };
float s_range[] = { 0,256 };
const float* hs_ranges[] = { h_range,s_range };
int hs_channels[] = { 0,1 };
calcHist(&hsv, 1, hs_channels, Mat(), hs_hist, 2, hist_bins, hs_ranges, true, false);
double maxVal = 0;
minMaxLoc(hs_hist, 0, &maxVal, 0, 0);
int scale = 10;
Mat hist2d_image = Mat::zeros(sbins*scale, hbins*scale, CV_8UC3);
for (int h = 0; h < hbins; h++)
{
for (int s = 0; s < sbins; s++)
{
float binVal = hs_hist.at<float>(h, s);
int intensity = cvRound(binVal * 255 / maxVal);
rectangle(hist2d_image,
Point(h*scale, s*scale),
Point((h + 1)*scale - 1, (s + 1)*scale - 1),
Scalar::all(intensity),
-1
);
}
}
//显示直方图
applyColorMap(hist2d_image,hist2d_image,COLORMAP_JET);
imshow("H_S Histogram", hist2d_image);
imwrite("D:/opencv/pictures/hist_2d.png", hist2d_image);
}
#include
#include
#include
using namespace std;
using namespace cv;
int main(int argc,char **argv)
{
Mat src = imread("D:/opencv/pictures/3 (3).jpeg", IMREAD_ANYCOLOR);//B,G,R蓝绿红通道图像
if (src.empty())
{
printf("could not load image"); //如果路径不正确,则显示无法导入图片。
return -1;
}
namedWindow("输入窗口", WINDOW_AUTOSIZE);
imshow("输入窗口", src); //在窗口显示图像
QuickDemo qd; //调用之前创建的类对象
qd.histogram_2d_demo(src);
waitKey(0); //暂停程序,等待一个按键输入,单位ms
destroyAllWindows(); //销毁所有创建的窗口
return 0;
}