当前位置:网站首页>opencv图像处理
opencv图像处理
2022-07-26 09:20:00 【Alex Su (*^▽^*)】
OpenCV基础入门系列基本操作——壹_零壹博弈的博客-CSDN博客c
参考这个博客
OpenCV 教程 — OpenCV 2.3.2 documentation
这个教程很好
一.灰度处理图像
#include <opencv2/opencv.hpp>
#include <bits/stdc++.h>
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
#define rep(i, a, b) for(int i = (a); i < (b); i++)
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("1.jpg");
int n = img.rows;
int m = img.cols;
rep(i, 0, n)
rep(j, 0, m)
{
uchar averge = (img.at<Vec3b>(i, j)[0] + img.at<Vec3b>(i, j)[1] + img.at<Vec3b>(i, j)[2]) / 3;
rep(k, 0, 3) img.at<Vec3b>(i, j)[k] = averge;
}
imshow("picture", img);
waitKey(0);
return 0;
}二.二值化操作
稍微改一下值就好了
#include <opencv2/opencv.hpp>
#include <bits/stdc++.h>
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
#define rep(i, a, b) for(int i = (a); i < (b); i++)
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("1.jpg");
int n = img.rows;
int m = img.cols;
rep(i, 0, n)
rep(j, 0, m)
{
uchar averge = (img.at<Vec3b>(i, j)[0] + img.at<Vec3b>(i, j)[1] + img.at<Vec3b>(i, j)[2]) / 3;
if (averge > 150) averge = 255;
else averge = 0;
rep(k, 0, 3) img.at<Vec3b>(i, j)[k] = averge;
}
imshow("picture", img);
waitKey(0);
return 0;
}三.通道分离
注意opencv中是BGR
#include <opencv2/opencv.hpp>
#include <bits/stdc++.h>
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
#define rep(i, a, b) for(int i = (a); i < (b); i++)
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("1.jpg");
vector<Mat> channels;
split(image, channels);
imshow("B", channels.at(0));
imshow("G", channels.at(1));
imshow("R", channels.at(2));
waitKey(0);
return 0;
}四.cvtColor函数的使用
#include <opencv2/opencv.hpp>
#include <bits/stdc++.h>
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
#define rep(i, a, b) for(int i = (a); i < (b); i++)
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("1.jpg"), img2, img3;
cvtColor(img, img2, CV_BGR2GRAY);
cvtColor(img, img3, COLOR_BGR2Lab);
imshow("picture1", img);
imshow("picture2", img2);
imshow("picture3", img3);
waitKey(0);
return 0;
}五.namedwindow函数的使用
第二个参数为0表示窗口可以改变大小,1表示不可
#include <opencv2/opencv.hpp>
#include <bits/stdc++.h>
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
#define rep(i, a, b) for(int i = (a); i < (b); i++)
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("1.jpg");
namedWindow("1", 0);
//namedWindow("1", 1);
imshow("1", img);
waitKey(0);
return 0;
}六.滤波
#include <opencv2/opencv.hpp>
#include <bits/stdc++.h>
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("1.jpg");
imshow("1", img);
blur(img, img, Size(5, 5)); //均值滤波 模糊
//medianBlur(img, img, 5); //中值滤波 消除噪声
imshow("2", img);
waitKey(0);
return 0;
}七.直方图均衡化
#include <opencv2/opencv.hpp>
#include <bits/stdc++.h>
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("1.jpg"), img2;
cvtColor(img, img, CV_BGR2GRAY);
equalizeHist(img, img2);
imshow("1", img);
imshow("2", img2);
waitKey(0);
return 0;
}自己实现
#include <opencv2/opencv.hpp>
#include <cmath>
using namespace cv;
const int N = 260;
int cnt[N], map[N];
int main()
{
Mat img = imread("2.jpg");
cvtColor(img, img, CV_BGR2GRAY);
imshow("处理前", img);
int n = img.rows, m = img.cols;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cnt[img.at<uchar>(i, j)]++;
int sum = 0;
for (int i = 0; i < 256; i++)
{
sum += cnt[i];
map[i] = round(255.0 * sum / (m * n));
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
img.at<uchar>(i, j) = map[img.at<uchar>(i, j)];
imshow("处理后", img);
waitKey(0);
return 0;
}八.边缘检测 画图
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat img, img2, img3;
img = imread("3.jpg");
imshow("input", img);
/* 画直线
line(img, Point(1, 1), Point(50, 50), Scalar(0, 0, 255), 2);
imshow("output", img);
*/
/* Sobel算子实现边缘检测
cvtColor(img, img, CV_RGB2GRAY);
Sobel(img, img2, -1, 1, 0); // -1代表图像深度 都写-1就行了 1 0 表示x方向 0 1 表示y方向
Sobel(img, img3, -1, 0, 1);
namedWindow("output2", 0);
namedWindow("output3", 0);
imshow("output2", img2);
imshow("output3", img3);*/
waitKey(0);
return 0;
}
九.轮廓分析 寻找并绘制
OpenCV中的findContours函数参数详解_小白_努力-CSDN博客_findcontours
十.Hough变换检测直线
#include <opencv2/opencv.hpp>
#include <bits/stdc++.h>
using namespace cv;
using namespace std;
int main()
{
Mat img, t;
img = imread("1.jpg");
// imshow("input", img);
//用Canny二值化
cvtColor(img, t, CV_RGB2GRAY); //先转成灰度图
GaussianBlur(t, t, Size(3, 3), 0, 0); //用高斯滤波去除噪声
Canny(t, t, 125, 350); //用Canny边缘检测
//ROI 选择感兴趣的区域
imshow("output1", t);
for (int i = 0; i < t.rows; i++)
for (int j = 0; j < t.cols; j++)
if(j < 700 || j > 1200)
t.at<uchar>(i, j) = 0;
imshow("output2", t);
//用Hough变换检测直线
vector<Vec4i> lines; //lines中每一个元素储存了一个四元组 代表直线的两点四个坐标
HoughLinesP(t, lines, 1, CV_PI / 180, 80, 100, 50);
for (auto x : lines)
{
if (x[0] == x[2]) continue;
double k = double(x[3] - x[1]) / (x[0] - x[2]);
if (k > 0 || abs(k) < 2 || abs(k) > 10) continue; //限制直线的斜率
line(img, Point(x[0], x[1]), Point(x[2], x[3]), Scalar(0, 255, 0), 10);
}
namedWindow("output", 0);
imshow("output", img);
waitKey(0);
return 0;
}十一.视频的读取与写入
#include<opencv2/opencv.hpp>
using namespace cv;
int main()
{
VideoCapture vc;
vc.open("1.avi");
if (!vc.isOpened())
{
printf("can not open ...\n");
return -1;
}
VideoWriter vw;
vw.open("output.avi", (int)vc.get(CV_CAP_PROP_FOURCC), (double)vc.get(CV_CAP_PROP_FPS),
Size((int)vc.get(CV_CAP_PROP_FRAME_WIDTH), (int)vc.get(CV_CAP_PROP_FRAME_HEIGHT)), true);
Mat frame;
while (vc.read(frame))
{
//这一行可以处理当前帧,也就是frame这个图像
vw.write(frame);
}
vc.release();
vw.release();
return 0;
}十二.车道线检测
#include<opencv2/opencv.hpp>
#include<bits/stdc++.h>
using namespace cv;
using namespace std;
int fi = 1, d, last;
void draw(Mat& img, int x)
{
circle(img, Point(last = x, img.rows - 10), 10, Scalar(0, 0, 255), -1);
}
void deal(Mat& img)
{
Mat t;
//用Canny二值化
cvtColor(img, t, CV_RGB2GRAY); //先转成灰度图
GaussianBlur(t, t, Size(3, 3), 0, 0); //用高斯滤波去除噪声
Canny(t, t, 125, 350); //用Canny边缘检测
//用Hough变换检测直线
vector<Vec4i> lines; //lines中每一个元素储存了一个四元组 代表直线的两点四个坐标
vector<int> node; //存直线与底线的交点
HoughLinesP(t, lines, 1, CV_PI / 180, 80, 100, 50); //最后两个参数对应minLineLength maxLineGap
for (auto x : lines)
{
int x1 = x[0], y1 = x[1], x2 = x[2], y2 = x[3];
if (x1 == x2) continue;
double k = double(y1 - y2) / (x1 - x2);
if (k > 0 && 2 <= abs(k) && abs(k) <= 10)//限制直线的斜率
{
// line(img, Point(x1, y1), Point(x2, y2), Scalar(0, 255, 0), 5);
int xx = (img.rows - y1) / k + x1;
node.push_back(xx);
}
}
//交点去重
vector<int> ve;
sort(node.begin(), node.end());
for (int i = 0; i < node.size(); i++)
{
if (i + 1 < node.size() && node[i + 1] - node[i] <= 10)
{
node[i + 1] = node[i];
continue;
}
ve.push_back(node[i]);
}
//画红点
if (fi) //第一帧
{
d = ve[1] - ve[0];
draw(img, ve[0] + d);
fi = 0;
}
else
{
if (!ve.size()) draw(img, last); //左右两侧直线都没检测到的情况
else
{
if (abs(ve[0] + d - last) <= 30) draw(img, ve[0] + d); //左侧直线被检测到的情况
else draw(img, ve[0]); //左侧直线没有被检测到的情况
}
}
}
int main()
{
VideoCapture vc;
vc.open("03.avi");
if (!vc.isOpened())
{
printf("can not open ...\n");
return -1;
}
VideoWriter vw;
vw.open("output3.avi", (int)vc.get(CV_CAP_PROP_FOURCC), (double)vc.get(CV_CAP_PROP_FPS),
Size((int)vc.get(CV_CAP_PROP_FRAME_WIDTH), (int)vc.get(CV_CAP_PROP_FRAME_HEIGHT)), true);
Mat frame;
while (vc.read(frame))
{
deal(frame);
vw.write(frame);
}
vc.release();
vw.release();
return 0;
}边栏推荐
- jvm命令归纳
- LeetCode三数之和问题
- Error: Cannot find module ‘umi‘ 问题处理
- Nuxt - Project packaging deployment and online to server process (SSR server rendering)
- Datax的学习笔记
- arc-gis的基本使用2
- 异常处理机制二
- HBuilderX 运行微信开发者工具 “Fail to open IDE“报错解决
- Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
- 本地缓存
猜你喜欢

Redis principle and use - Basic Features

Mysql事务

【线上死锁分析】由index_merge引发的死锁事件

语音聊天app源码——钠斯直播系统源码

分布式跟踪系统选型与实践

Selection and practice of distributed tracking system

Advanced mathematics | Takeshi's "classic series" daily question train of thought and summary of error prone points

What are CSDN spaces represented by

redis原理和使用-安装和分布式配置

Datax的学习笔记
随机推荐
js在控制台输出菱形
安卓 实现缓存机制,多种数据类型缓存
论文笔记: 知识图谱 KGAT (未完暂存)
The child and binary tree- open root inversion of polynomials
I'm faded
“could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32” 问题处理
Study notes of dataX
Cat installation and use
Sliding window, double pointer, monotone queue, monotone stack
[arkit, realitykit] turn pictures into 3D models
“No input file specified “问题的处理
Redis principle and usage - installation and distributed configuration
redis原理和使用-基本特性
力扣链表题
【线上问题】Timeout waiting for connection from pool 问题排查
NTT (fast number theory transformation) polynomial inverse 1500 word analysis
原根与NTT 五千字详解
Elastic APM installation and use
Cat安装和使用
性格测试系统v1.0