OpenCV学习笔记
OpenCV3使用
下载安装
https://blog.csdn.net/Creama_/article/details/107238475
教程
https://www.icourse163.org/learn/HDU-1461554161?tid=1465877483#/learn/content?type=detail&id=1245862104&cid=1271097642
简单例程:显示图片
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
//OpenCV版本号
cout << "OpenCV_Version: " << CV_VERSION << endl;
//读取图片
Mat img = imread("C:\\Users\\97759\\Desktop\\OpenCV\\QQ20211230135939.png");
imshow("picture", img);
waitKey(0);
return 0;
}
Mat
int main()
{
//OpenCV版本号
cout << "OpenCV_Version: " << CV_VERSION << endl;
//读取图片
Mat img = imread("C:\\Users\\97759\\Desktop\\OpenCV\\QQ20211230135939.png");
imshow("picture", img);
}
图像二值化
int main()
{
//读取图片
Mat srcMat = imread("C:\\Users\\97759\\Desktop\\OpenCV\\QQ20211230135939.png");
imshow("picture", srcMat);
Mat srcMat1 = imread("C:\\Users\\97759\\Desktop\\OpenCV\\QQ20211230135939.png",0);
imshow("picture1", srcMat1);
int height = srcMat.rows; //行数
int width = srcMat.cols; //每行元素的总元素数量
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
//-----------------开始处理每个像素-----------------
uchar average = (srcMat.at<Vec3b>(j, i)[0] + srcMat.at<Vec3b>(j, i)[1] + srcMat.at<Vec3b>(j, i)[2]) / 3;
srcMat.at<Vec3b>(j, i)[0] = average;
srcMat.at<Vec3b>(j, i)[1] = average;
srcMat.at<Vec3b>(j, i)[2] = average;
//-------------结束像素处理------------------------
} //单行处理结束
}
}
取出R、G、B的数值
int main()
{
Mat sec_color = imread("C:\\Users\\97759\\Desktop\\sec.png");
vector<Mat> channels;
split(sec_color, channels);
Mat R = channels.at(0);
Mat G = channels.at(1);
Mat B = channels.at(2);
imshow("R", R);
imshow("G", G);
imshow("B", B);
waitKey(0);
}
调用相机并显示
int main()
{
VideoCapture cap;
cap.open(0);
cout << "FPS:" << cap.get(CAP_PROP_FPS) << endl;
while (true)
{
Mat frame;
if (cap.read(frame))
{
imshow("frame", frame);
}
waitKey(30);
}
waitKey(0);
}
画圆、线、矩形
int main()
{
int nHistWidth = 800;
int nHistHeight = 600;
Mat matHistImage(nHistHeight, nHistWidth, CV_8UC3, Scalar(255, 255, 255));
Point pt0,pt1,pt2;
pt0.x = 0;
pt0.y = 0;
pt1.x = 50;
pt1.y = 50;
pt2.x = 100;
pt2.y = 100;
Rect rect;
rect.x = 0;
rect.y = 0;
rect.width=100;
rect.height=100;
//画圆
circle(matHistImage, pt1, 50, CV_RGB(255, 0, 0));
//画线
line(matHistImage, pt0, pt2, CV_RGB(0, 255, 0));
//画矩形
rectangle(matHistImage, rect, CV_RGB(0, 0, 255));
imshow("histogram", matHistImage);
waitKey(0);
}
调用相机并绘制直方图
int main()
{
VideoCapture cap;
cap.open(0);
double scale = 0.5;
while (1)
{
double scale = 0.5;
while (1)
{
cv::Mat SrcImg;
cap >> SrcImg;
Size ResImgSiz = Size(SrcImg.cols * scale, SrcImg.rows * scale);
Mat matSrc = Mat(ResImgSiz, SrcImg.type());
//resize(srcMat,matSrc,ResImgsiz,INTER_LINEAR);INTER_LINEAR 函数插值方法
resize(SrcImg, matSrc, ResImgSiz, INTER_LINEAR);
Mat matRGB[3];
split(matSrc, matRGB);
int Channels[] = { 0 };
int nHistSize[] = { 256 };
float range[] = { 0, 255 };
const float* fHistRanges[] = { range };
Mat histR, histG, histB;
// 计算直方图
calcHist(&matRGB[0], 1, Channels, Mat(), histB, 1, nHistSize, fHistRanges, true, false);
calcHist(&matRGB[1], 1, Channels, Mat(), histG, 1, nHistSize, fHistRanges, true, false);
calcHist(&matRGB[2], 1, Channels, Mat(), histR, 1, nHistSize, fHistRanges, true, false);
// 创建直方图画布
int nHistWidth = 800;
int nHistHeight = 600;
int nBinWidth = cvRound((double)nHistWidth / nHistSize[0]);
Mat matHistImage(nHistHeight, nHistWidth, CV_8UC3, Scalar(255, 255, 255));
// 直方图归一化
normalize(histB, histB, 0.0, matHistImage.rows, NORM_MINMAX, -1, Mat());
normalize(histG, histG, 0.0, matHistImage.rows, NORM_MINMAX, -1, Mat());
normalize(histR, histR, 0.0, matHistImage.rows, NORM_MINMAX, -1, Mat());
// 在直方图中画出直方图
for (int i = 1; i < nHistSize[0]; i++)
{
line(matHistImage,
Point(nBinWidth * (i - 1), nHistHeight - cvRound(histB.at<float>(i - 1))),
Point(nBinWidth * (i), nHistHeight - cvRound(histB.at<float>(i))),
Scalar(255, 0, 0),
2,
8,
0);
line(matHistImage,
Point(nBinWidth * (i - 1), nHistHeight - cvRound(histG.at<float>(i - 1))),
Point(nBinWidth * (i), nHistHeight - cvRound(histG.at<float>(i))),
Scalar(0, 255, 0),
2,
8,
0);
line(matHistImage,
Point(nBinWidth * (i - 1), nHistHeight - cvRound(histR.at<float>(i - 1))),
Point(nBinWidth * (i), nHistHeight - cvRound(histR.at<float>(i))),
Scalar(0, 0, 255),
2,
8,
0);
}
// 显示直方图
imshow("frame", matSrc);
imshow("histogram", matHistImage);
waitKey(30);
}
}
}
图像形态学
-
腐蚀运算
结构元素(算子)的矩形区域内有0,就把中心锚点置0。
增强暗度
去除噪声、减少粘连。 -
膨胀运算
结构元素(算子)的矩形区域内有1,就把矩形区域置1。
增强亮度
二值化后像素集合可能会被分割,膨胀算法可以恢复连通性。
缺点:会导致目标物体形状发生变化。
- 开运算
先腐蚀,后膨胀。
既不使目标物体形状变小,又能去除噪声。 - 闭运算
先膨胀,后腐蚀。
既不使目标物体形状变大,又能恢复连通性。
灰度处理特有的运算:
-
顶帽处理
原图-开运算。
背景变暗、前景去掉。原图-结果=得到前景。
消除背景光照不均匀的现象,改善二值化的效果。
背景是暗,前景是亮,即保留比周围良的元素。 -
底帽处理
闭运算-原图。
即保留比周围暗的元素。
选择HSV范围显示图像
int main()
{
VideoCapture cap(0);
double i_maxH = 1;
double i_minH = 100;
double i_maxS = 43;
double i_minS = 255;
double i_maxV = 0;
double i_minV = 255;
while (true)
{
Mat frame;
Mat hsvMat;
Mat detectMat;
cap >> frame;
cvtColor(frame, hsvMat, COLOR_RGB2HSV);
frame.copyTo(hsvMat);
inRange(hsvMat, Scalar(i_maxH, i_maxS, i_maxV), Scalar(i_minH, i_minS, i_minV), detectMat);
imshow("frame", frame);
imshow("detectMat", detectMat);
waitKey(30);
}
waitKey(0);
}
图像二值化
int main()
{
VideoCapture cap;
cap.open(0);
while (true)
{
Mat frame;
Mat result;
cap >> frame;
cvtColor(frame, result, COLOR_BGR2GRAY);//先转换为灰度图
threshold(result, result, 100, 255, THRESH_OTSU);//再进行二值化,大津法
imshow("frame", frame);
imshow("resulet", result);
waitKey(30);
}
waitKey(0);
}
区域自适应二值化
int main()
{
VideoCapture cap;
cap.open(0);
while (true)
{
Mat frame;
Mat result;
cap >> frame;
cvtColor(frame, result, COLOR_BGR2GRAY);
adaptiveThreshold(result, result, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY_INV, 15, 10);
imshow("frame", frame);
imshow("resulet", result);
waitKey(30);
}
waitKey(0);
}
滑动条二值化选择阈值
string windows_name = "binaryMat";
void threshold_Mat(int th,void* data)
{
Mat src = *(Mat*)data;
Mat dst;
threshold(src, dst, th, 255, 0);
imshow("windows_name", dst);
}
int main()
{
VideoCapture cap;
cap.open(0);
Mat frame;
Mat result;
int initTh = 128;
int maxTh = 255;
while (true)
{
cap >> frame;
cvtColor(frame, result, COLOR_BGR2GRAY);
imshow("frame", frame);
//imshow("windows_name", result);
createTrackbar("Bar", "frame", &initTh, maxTh, threshold_Mat, &result);
waitKey(30);
}
waitKey(0);
}