当前位置:网站首页>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 the problem of storing cookies in IE7 & IE8
- QT handy notes (VI) -- update interface, screenshot, file dialog box
- In the same CONDA environment, install pytroch first and then tensorflow
- 2022年中科磐云——服务器内部信息获取 解析flag
- Double authentication of server and client
- Uni app learning summary
- Redis sentinel mode setup under Windows
- Production of a-modal drag function in antui
- The combination of officially issued SSL certificate and self signed certificate realizes website two-way authentication
- Draw arrows with openlayer
猜你喜欢

Qt随手笔记(三)在vs中使用QtCharts画折线图

解决npm -v突然失效 无反应

Customize permission validation in blazor

Azkaban【基础知识 01】核心概念+特点+Web界面+架构+Job类型(一篇即可入门Azkaban工作流调度系统)

Wechat applet learning notes 1

QT handy notes (III) use qtcharts to draw a line chart in VS

面试突击68:为什么 TCP 需要 3 次握手?

Node memory overflow and V8 garbage collection mechanism

图解用户登录验证流程,写得太好了!

Spolicy request case
随机推荐
Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
In the same CONDA environment, install pytroch first and then tensorflow
Customize permission validation in blazor
JS judge the data types object.prototype.tostring.call and typeof
js 表格自动循环滚动,鼠标移入暂停
JS one line code to obtain the maximum and minimum values of the array
面试突击68:为什么 TCP 需要 3 次握手?
R语言ggplot2可视化: 将图例标题(legend title)对齐到ggplot2中图例框的中间(默认左对齐、align legend title to middle of legend)
spolicy请求案例
网络流学习笔记
Applet record
解决npm -v突然失效 无反应
WARNING: [pool www] server reached pm. max_ children setting (5), consider raising it
Wechat applet learning notes 2
Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market
Application of Gauss elimination
2019 ICPC Asia Yinchuan regional (water problem solution)
A new paradigm of distributed deep learning programming: Global tensor
QT handy notes (III) use qtcharts to draw a line chart in VS
在Blazor 中自定义权限验证