当前位置:网站首页>Opencv image processing
Opencv image processing
2022-07-26 09:55:00 【Alex Su (*^▽^*)】
OpenCV Basic introduction series basic operation —— one _ Zero one game blog -CSDN Blog c
Refer to this blog
OpenCV course — OpenCV 2.3.2 documentation
This tutorial is very good
One . Gray processing image
#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;
}
Two . Binary operation
Just change the value a little
#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;
}
3、 ... and . The channel separation
Be careful opencv Medium is 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;
}
Four .cvtColor Use of functions
#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;
}
5、 ... and .namedwindow Use of functions
The second parameter is 0 Indicates that the window can be resized ,1 Means impossible
#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;
}
6、 ... and . wave filtering
#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)); // Mean filtering Fuzzy
//medianBlur(img, img, 5); // median filtering Eliminate noise
imshow("2", img);
waitKey(0);
return 0;
}
7、 ... and . Histogram equalization
#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;
}
Realize it by yourself
#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(" Before processing ", 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(" After processing ", img);
waitKey(0);
return 0;
}
8、 ... and . edge detection drawing
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat img, img2, img3;
img = imread("3.jpg");
imshow("input", img);
/* Draw a straight line
line(img, Point(1, 1), Point(50, 50), Scalar(0, 0, 255), 2);
imshow("output", img);
*/
/* Sobel Operator implementation of edge detection
cvtColor(img, img, CV_RGB2GRAY);
Sobel(img, img2, -1, 1, 0); // -1 Represents the image depth All written -1 That's it 1 0 Express x Direction 0 1 Express y Direction
Sobel(img, img3, -1, 0, 1);
namedWindow("output2", 0);
namedWindow("output3", 0);
imshow("output2", img2);
imshow("output3", img3);*/
waitKey(0);
return 0;
}
Nine . Profile analysis Find and draw
Ten .Hough Transform detection line
#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);
// use Canny Two valued
cvtColor(img, t, CV_RGB2GRAY); // First convert to grayscale
GaussianBlur(t, t, Size(3, 3), 0, 0); // Gaussian filter is used to remove noise
Canny(t, t, 125, 350); // use Canny edge detection
//ROI Select the area of interest
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);
// use Hough Transform detection line
vector<Vec4i> lines; //lines Each element in stores a quadruple Two points and four coordinates representing a straight line
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; // Limit the slope of the line
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;
}
11、 ... and . Video reading and writing
#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))
{
// This line can handle the current frame , That is to say frame This image
vw.write(frame);
}
vc.release();
vw.release();
return 0;
}
Twelve . Lane line detection
#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;
// use Canny Two valued
cvtColor(img, t, CV_RGB2GRAY); // First convert to grayscale
GaussianBlur(t, t, Size(3, 3), 0, 0); // Gaussian filter is used to remove noise
Canny(t, t, 125, 350); // use Canny edge detection
// use Hough Transform detection line
vector<Vec4i> lines; //lines Each element in stores a quadruple Two points and four coordinates representing a straight line
vector<int> node; // Save the intersection of the straight line and the baseline
HoughLinesP(t, lines, 1, CV_PI / 180, 80, 100, 50); // The last two parameters correspond to 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)// Limit the slope of the line
{
// line(img, Point(x1, y1), Point(x2, y2), Scalar(0, 255, 0), 5);
int xx = (img.rows - y1) / k + x1;
node.push_back(xx);
}
}
// Intersection de duplication
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]);
}
// Draw red dots
if (fi) // First frame
{
d = ve[1] - ve[0];
draw(img, ve[0] + d);
fi = 0;
}
else
{
if (!ve.size()) draw(img, last); // The left and right straight lines are not detected
else
{
if (abs(ve[0] + d - last) <= 30) draw(img, ve[0] + d); // The left straight line is detected
else draw(img, ve[0]); // The left straight line is not detected
}
}
}
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;
}
边栏推荐
- 图解用户登录验证流程,写得太好了!
- Solve proxyerror: CONDA cannot proceed due to an error in your proxy configuration
- EOJ 2020 1月月赛 E数的变换
- Applet record
- Uni app learning summary
- Spolicy request case
- 在同一conda环境下先装Pytroch后装TensorFlow
- Flutter Event 派发
- Sqoop [environment setup 01] CentOS Linux release 7.5 installation configuration sqoop-1.4.7 resolve warnings and verify (attach sqoop 1 + sqoop 2 Latest installation package +mysql driver package res
- El table implements adding / deleting rows, and a parameter changes accordingly
猜你喜欢
新增市场竞争激烈,中国移动被迫推出限制性超低价5G套餐
Spolicy request case
B站这个视频我是跪着看完的
Installation and use of cocoapods
Due to fierce competition in the new market, China Mobile was forced to launch a restrictive ultra-low price 5g package
protobuf的基本用法
QT handy notes (III) use qtcharts to draw a line chart in VS
小白搞一波深拷贝 浅拷贝
分布式网络通信框架:本地服务怎么发布成RPC服务
2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)
随机推荐
The problem of accessing certsrv after configuring ADCs
Interview shock 68: why does TCP need three handshakes?
Leetcode 504. Hex number
高斯消元
Show default image when wechat applet image cannot be displayed
解决npm -v突然失效 无反应
Wechat H5 payment on WAP, for non wechat browsers
A new paradigm of distributed deep learning programming: Global tensor
El table implements adding / deleting rows, and a parameter changes accordingly
在同一conda环境下先装Pytroch后装TensorFlow
开发转测试:从0开始的6年自动化之路...
PHP executes shell script
Solve proxyerror: CONDA cannot proceed due to an error in your proxy configuration
高斯消元求解矩阵的逆(gauss)
antd TreeSelect获取父节点的值
万字详解“用知识图谱驱动企业业绩增长”
Sqoop【付诸实践 02】Sqoop1最新版 全库导入 + 数据过滤 + 字段类型支持 说明及举例代码(query参数及字段类型强制转换)
Tableviewcell highly adaptive
2021年山东省中职组“网络空间安全”B模块windows渗透(解析)
Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)