Skip to content

Commit 6892bbe

Browse files
committed
first commit
0 parents  commit 6892bbe

File tree

132 files changed

+2779
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+2779
-0
lines changed

README.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
No | Description
2+
-------- | --------
3+
code_001 | [OpenCV之图片读取与显示](python/code_001/opencv_001.py/opencv_001.py)
4+
code_002 | [OpenCV之图片灰度化](python/code_002/opencv_002.py)
5+
code_003 | [OpenCV之图像创建与赋值](python/code_003/opencv_003.py)
6+
code_004 | [OpenCV之图像像素读写](python/code_004/opencv_004.py)
7+
code_005 | [OpenCV之图像像素算术操作(加减乘除)](python/code_005/opencv_005.py)
8+
code_006 | [OpenCV之图像伪彩色增强](python/code_006/opencv_006.py)
9+
code_007 | [OpenCV之图像像素操作(逻辑操作)](python/code_007/opencv_007.py)
10+
code_008 | [OpenCV之图像通道分离合并](python/code_008/opencv_008.py)
11+
code_009 | [OpenCV之色彩空间与色彩空间转换](python/code_009/opencv_009.py)
12+
code_010 | [OpenCV之图像像素值统计](python/code_010/opencv_010.py)
13+
code_011 | [OpenCV之图像像素归一化](python/code_011/opencv_011.py)
14+
code_012 | [OpenCV之视频读写](python/code_012/opencv_012.py)
15+
code_013 | [OpenCV之图像翻转](python/code_013/opencv_013.py)
16+
code_014 | [OpenCV之图像插值](python/code_014/opencv_014.py)
17+
code_015 | [OpenCV之绘制几何形状](python/code_015/opencv_015.py)
18+
code_016 | [OpenCV之图像ROI与ROI操作](python/code_016/opencv_016.py)
19+
code_017 | [OpenCV之图像直方图](python/code_017/opencv_017.py)
20+
code_018 | [OpenCV之图像直方图均衡化](python/code_018/opencv_018.py)
21+
code_019 | [OpenCV之图像直方图比较](python/code_019/opencv_019.py)
22+
code_020 | [OpenCV之图像直方图反向投影](python/code_020/opencv_020.py)
23+
code_021 | [OpenCV之图像卷积操作](python/code_021/opencv_021.py)
24+
code_022 | [OpenCV之图像均值与高斯模糊](python/code_022/opencv_022.py)
25+
code_023 | [OpenCV之中值模糊](python/code_023/opencv_023.py)
26+
code_024 | [OpenCV之图像噪声](python/code_024/opencv_024.py)
27+
code_025 | [OpenCV之图像去噪声](python/code_025/opencv_025.py)
28+
code_026 | [OpenCV之边缘保留滤波算法 – 高斯双边模糊](python/code_026/opencv_026.py)
29+
code_027 | [OpenCV之边缘保留滤波算法 – 均值迁移模糊(mean-shift blur)](python/code_027/opencv_027.py)
30+
code_028 | [OpenCV之图像积分图算法](python/code_028/opencv_028.py)
31+
code_029 | [OpenCV之快速的图像边缘滤波算法](python/code_029/opencv_029.py)
32+
code_030 | [OpenCV之自定义滤波器](python/code_030/opencv_030.py)
33+
code_031 | [OpenCV之Sobel算子](python/code_031/opencv_031.py)
34+
code_032 | [OpenCV之更多梯度算子](python/code_032/opencv_032.py)
35+
code_033 | [OpenCV之图像梯度 – 拉普拉斯算子(二阶导数算子)](python/code_033/opencv_033.py)
36+
code_034 | [OpenCV之图像锐化](python/code_034/opencv_034.py)
37+
code_035 | [OpenCV之USM 锐化增强算法](python/code_035/opencv_035.py)
38+
code_036 | [OpenCV之Canny边缘检测器](python/code_036/opencv_036.py)
39+
code_037 | [OpenCV之图像金字塔](python/code_037/opencv_037.py)
40+
code_038 | [OpenCV之拉普拉斯金字塔](python/code_038/opencv_038.py)
41+
code_039 | [OpenCV之图像模板匹配](python/code_039/opencv_039.py)
42+
code_040 | [OpenCV之二值图像介绍](python/code_040/opencv_040.py)

python/code_001/opencv_001.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import cv2
2+
3+
src = cv2.imread("test.jpg")
4+
cv2.namedWindow("input", cv2.WINDOW_AUTOSIZE)
5+
cv2.imshow("input", src)
6+
cv2.waitKey(0)
7+
cv2.destroyAllWindows()

python/code_001/test.jpg

25.4 KB
Loading

python/code_002/gray.png

63.1 KB
Loading

python/code_002/opencv_002.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import cv2
2+
3+
src = cv2.imread("./test.png")
4+
cv2.namedWindow("input", cv2.WINDOW_AUTOSIZE)
5+
cv2.imshow("input", src)
6+
gray = cv2.cv2tColor(src, cv2.COLOR_BGR2GRAY)
7+
cv2.imwrite('gray.png', gray)
8+
cv2.imshow("gray", gray)
9+
cv2.waitKey(0)
10+
cv2.destroyAllWindows()
11+

python/code_002/test.png

195 KB
Loading

python/code_003/opencv_003.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <opencv2/opencv.hpp>
2+
#include <iostream>
3+
4+
using namespace cv;
5+
using namespace std;
6+
7+
int main(int artc, char** argv) {
8+
Mat src = imread("./test.png");
9+
if (src.empty()) {
10+
printf("could not load image...\n");
11+
return -1;
12+
}
13+
namedWindow("input", CV_WINDOW_AUTOSIZE);
14+
imshow("input", src);
15+
16+
// �������� - ��¡
17+
Mat m1 = src.clone();
18+
19+
// ����
20+
Mat m2;
21+
src.copyTo(m2);
22+
23+
// ��ֵ��
24+
Mat m3 = src;
25+
26+
// �����հ�ͼ��
27+
Mat m4 = Mat::zeros(src.size(), src.type());
28+
Mat m5 = Mat::zeros(Size(512, 512), CV_8UC3);
29+
Mat m6 = Mat::ones(Size(512, 512), CV_8UC3);
30+
31+
Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0,
32+
-1, 5, -1,
33+
0, -1, 0);
34+
35+
waitKey(0);
36+
return 0;
37+
}

python/code_003/opencv_003.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import cv2 as cv
2+
import numpy as np
3+
4+
src = cv.imread("./test.png")
5+
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
6+
cv.imshow("input", src)
7+
8+
# 克隆图像
9+
m1 = np.copy(src)
10+
11+
# 赋值
12+
m2 = src
13+
src[100:200,200:300,:] = 255
14+
cv.imshow("m2",m2)
15+
16+
m3 = np.zeros(src.shape, src.dtype)
17+
cv.imshow("m3", m3)
18+
19+
m4 = np.zeros([512,512], np.uint8)
20+
# m4[:,:] =127 try to give gray value 127
21+
cv.imshow("m4", m4)
22+
23+
m5 = np.ones(shape=[512,512,3], dtype=np.uint8)
24+
m5[:,:,0] = 255
25+
cv.imshow("m5", m5)
26+
27+
28+
29+
30+
cv.waitKey(0)
31+
cv.destroyAllWindows()
32+

python/code_003/test.png

195 KB
Loading

python/code_004/code_004.cpp

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <opencv2/opencv.hpp>
2+
#include <iostream>
3+
4+
using namespace cv;
5+
using namespace std;
6+
7+
int main(int artc, char** argv) {
8+
Mat src = imread("./test.png");
9+
if (src.empty()) {
10+
printf("could not load image...\n");
11+
return -1;
12+
}
13+
namedWindow("input", CV_WINDOW_AUTOSIZE);
14+
imshow("input", src);
15+
16+
// ֱ�Ӷ�ȡͼ������
17+
int height = src.rows;
18+
int width = src.cols;
19+
int ch = src.channels();
20+
for (int c = 0; c < ch; c++) {
21+
for (int row = 0; row < height; row++) {
22+
for (int col = 0; col < width; col++) {
23+
if (ch == 3) {
24+
Vec3b bgr = src.at<Vec3b>(row, col);
25+
bgr[0] = 255 - bgr[0];
26+
bgr[1] = 255 - bgr[1];
27+
bgr[2] = 255 - bgr[2];
28+
src.at<Vec3b>(row, col) = bgr;
29+
} else if(ch == 1) {
30+
int gray = src.at<uchar>(row, col);
31+
src.at<uchar>(row, col) = 255 - gray;
32+
}
33+
}
34+
}
35+
}
36+
imshow("output", src);
37+
38+
// ָ���ȡ
39+
Mat result = Mat::zeros(src.size(), src.type());
40+
int blue = 0, green = 0, red = 0;
41+
int gray;
42+
for (int c = 0; c < ch; c++) {
43+
for (int row = 0; row < height; row++) {
44+
uchar* curr_row = src.ptr<uchar>(row);
45+
uchar* result_row = result.ptr<uchar>(row);
46+
for (int col = 0; col < width; col++) {
47+
if (ch == 3) {
48+
blue = *curr_row++;
49+
green = *curr_row++;
50+
red = *curr_row++;
51+
52+
*result_row++ = blue;
53+
*result_row++ = green;
54+
*result_row++ = red;
55+
}
56+
else if (ch == 1) {
57+
gray = *curr_row++;
58+
*result_row++ = gray;
59+
}
60+
}
61+
}
62+
}
63+
imshow("result", result);
64+
65+
waitKey(0);
66+
return 0;
67+
}

python/code_004/opencv_004.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import cv2 as cv
2+
3+
src = cv.imread("./test.png")
4+
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
5+
cv.imshow("input", src)
6+
h, w, ch = src.shape
7+
print("h , w, ch", h, w, ch)
8+
for row in range(h):
9+
for col in range(w):
10+
b, g, r = src[row, col]
11+
b = 255 - b
12+
g = 255 - g
13+
r = 255 - r
14+
src[row, col] = [b, g, r]
15+
cv.imshow("output", src)
16+
17+
cv.waitKey(0)
18+
cv.destroyAllWindows()
19+

python/code_004/test.png

195 KB
Loading

python/code_005/code_005.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <opencv2/opencv.hpp>
2+
#include <iostream>
3+
4+
using namespace cv;
5+
using namespace std;
6+
7+
int main(int artc, char** argv) {
8+
Mat src1 = imread("./test0.jpg");
9+
Mat src2 = imread("./test1.jpg");
10+
if (src1.empty() || src2.empty()) {
11+
printf("could not load image...\n");
12+
return -1;
13+
}
14+
namedWindow("input", CV_WINDOW_AUTOSIZE);
15+
imshow("input1", src1);
16+
imshow("input2", src2);
17+
int height = src1.rows;
18+
int width = src1.cols;
19+
20+
int b1 = 0, g1 = 0, r1 = 0;
21+
int b2 = 0, g2 = 0, r2 = 0;
22+
int b = 0, g = 0, r = 0;
23+
Mat result = Mat::zeros(src1.size(), src1.type());
24+
for (int row = 0; row < height; row++) {
25+
for (int col = 0; col < width; col++) {
26+
b1 = src1.at<Vec3b>(row, col)[0];
27+
g1 = src1.at<Vec3b>(row, col)[1];
28+
r1 = src1.at<Vec3b>(row, col)[2];
29+
30+
b2 = src2.at<Vec3b>(row, col)[0];
31+
g2 = src2.at<Vec3b>(row, col)[1];
32+
r2 = src2.at<Vec3b>(row, col)[2];
33+
34+
result.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b1 + b2);
35+
result.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g1 + g2);
36+
result.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r1 + r2);
37+
}
38+
}
39+
imshow("output", result);
40+
41+
Mat add_result = Mat::zeros(src1.size(), src1.type());
42+
add(src1, src2, add_result);
43+
imshow("add_result", add_result);
44+
45+
Mat sub_result = Mat::zeros(src1.size(), src1.type());
46+
subtract(src1, src2, sub_result);
47+
imshow("sub_result", sub_result);
48+
49+
Mat mul_result = Mat::zeros(src1.size(), src1.type());
50+
multiply(src1, src2, mul_result);
51+
imshow("mul_result", mul_result);
52+
53+
Mat div_result = Mat::zeros(src1.size(), src1.type());
54+
divide(src1, src2, div_result);
55+
imshow("div_result", div_result);
56+
57+
waitKey(0);
58+
return 0;
59+
}

python/code_005/opencv_005.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import cv2 as cv
2+
import numpy as np
3+
4+
src1 = cv.imread("./test0.jpg")
5+
src2 = cv.imread("./test1.jpg")
6+
cv.imshow("input1", src1)
7+
cv.imshow("input2", src2)
8+
h, w, ch = src1.shape
9+
print("h , w, ch", h, w, ch)
10+
11+
add_result = np.zeros(src1.shape, src1.dtype)
12+
cv.add(src1, src2, add_result)
13+
cv.imshow("add_result", add_result)
14+
15+
sub_result = np.zeros(src1.shape, src1.dtype)
16+
cv.subtract(src1, src2, sub_result)
17+
cv.imshow("sub_result", sub_result)
18+
19+
mul_result = np.zeros(src1.shape, src1.dtype)
20+
cv.multiply(src1, src2, mul_result)
21+
cv.imshow("mul_result", mul_result)
22+
23+
div_result = np.zeros(src1.shape, src1.dtype)
24+
cv.divide(src1, src2, div_result)
25+
cv.imshow("div_result", div_result)
26+
27+
cv.waitKey(0)
28+
cv.destroyAllWindows()
29+

python/code_005/test0.jpg

821 KB
Loading

python/code_005/test1.jpg

294 KB
Loading

python/code_006/code_006.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <opencv2/opencv.hpp>
2+
#include <iostream>
3+
4+
using namespace cv;
5+
using namespace std;
6+
void customColorMap(Mat &image);
7+
int main(int argc, const char *argv[])
8+
{
9+
Mat src = imread("cos.jpg");
10+
if (src.empty())
11+
{
12+
printf("could not load image...\n");
13+
return -1;
14+
}
15+
Mat gray, dst;
16+
// ʹ��LUT
17+
applyColorMap(src, dst, COLORMAP_SUMMER);
18+
// ��ʾ���
19+
imshow("colorMap", dst);
20+
21+
cvtColor(src, gray, COLOR_BGR2GRAY);
22+
imshow("gray", gray);
23+
customColorMap(gray);
24+
25+
waitKey(0);
26+
return 0;
27+
}
28+
29+
void customColorMap(Mat &image) {
30+
int lut[256];
31+
for (int i = 0; i < 256; i++) {
32+
if (i < 127)
33+
lut[i] = 0;
34+
else
35+
lut[i] = 255;
36+
}
37+
38+
int h = image.rows;
39+
int w = image.cols;
40+
for (int row = 0; row < h; row++) {
41+
for (int col = 0; col < w; col++) {
42+
int pv = image.at<uchar>(row, col);
43+
image.at<uchar>(row, col) = lut[pv];
44+
}
45+
}
46+
imshow("lut demo", image);
47+
}
48+

python/code_006/opencv_006.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import cv2 as cv
2+
3+
src = cv.imread("test1.png")
4+
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
5+
cv.imshow("input", src)
6+
dst = cv.applyColorMap(src, cv.COLORMAP_COOL)
7+
cv.imshow("output", dst)
8+
9+
# 伪色彩
10+
image = cv.imread("test0.jpg")
11+
color_image = cv.applyColorMap(image, cv.COLORMAP_JET)
12+
cv.imshow("image", image)
13+
cv.imshow("color_image", color_image)
14+
cv.waitKey(0)
15+
cv.destroyAllWindows()
16+
17+

python/code_006/test0.jpg

821 KB
Loading

python/code_006/test1.png

195 KB
Loading

python/code_007/Mat.png

160 KB
Loading

0 commit comments

Comments
 (0)