-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencv_operation.py
165 lines (115 loc) · 5.38 KB
/
opencv_operation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 25 20:26:21 2020
@author: shangyonggang
"""
import cv2
import numpy as np
if __name__ == "__main__":
#image read
if 0:
img_path = "./zhaoliying_r2.jpg"
img_color = cv2.imread(img_path, cv2.IMREAD_COLOR)
img_gray = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
print("img_color.shape = ", img_color.shape)
print("img_gray.shape = ", img_gray.shape)
cv2.imshow("img_color", img_color)
cv2.imshow("img_gray", img_gray)
cv2.waitKey(0)
#color space
if 0:
img_path = "./fengjing_r.jpg"
img_color = cv2.imread(img_path, cv2.IMREAD_COLOR)
img_rgb = cv2.cvtColor(img_color, cv2.COLOR_BGR2RGB)
img_gray = cv2.cvtColor(img_color, cv2.COLOR_BGR2GRAY)
img_hsv = cv2.cvtColor(img_color, cv2.COLOR_BGR2HSV)
cv2.imshow("img_color", img_color)
cv2.imshow("img_rgb", img_rgb)
cv2.imshow("img_gray", img_gray)
cv2.imshow("img_hsv", img_hsv)
cv2.waitKey(0)
#image roi
if 0:
img_path_1 = "./huge_r2.jpg"
img_path_2 = "./zhaoliying_r2.jpg"
img_1 = cv2.imread(img_path_1, cv2.IMREAD_COLOR)
img_2 = cv2.imread(img_path_2, cv2.IMREAD_COLOR)
img_1_roi = img_1[: , :int(img_1.shape[1]/2)]
img_2_roi = img_2[: , int(img_2.shape[1]/2):]
cv2.imshow("img_1_roi", img_1_roi)
cv2.imshow("img_2_roi", img_2_roi)
cv2.waitKey(0)
#image resize
if 0:
img_path_1 = "./huge_r2.jpg"
img_1 = cv2.imread(img_path_1, cv2.IMREAD_COLOR)
img_1_resize1 = cv2.resize(img_1, (int(img_1.shape[1]*3/4), int(img_1.shape[0]*3/4)))
img_1_resize2 = cv2.resize(img_1, (int(img_1.shape[1]/2), int(img_1.shape[0]/2)))
cv2.imshow("img_1", img_1)
cv2.imshow("img_1_resize1", img_1_resize1)
cv2.imshow("img_1_resize2", img_1_resize2)
cv2.waitKey(0)
#split and merge
if 0:
img_path = "./fengjing_r.jpg"
img_color = cv2.imread(img_path, cv2.IMREAD_COLOR)
B,G,R = cv2.split(img_color)
img_merge = cv2.merge((B,G,R))
cv2.imshow("B", B)
cv2.imshow("G", G)
cv2.imshow("R", R)
cv2.imshow("img_merge", img_merge)
cv2.waitKey(0)
#image concat
if 0:
img_path_1 = "./zhaoliying_r2.jpg"
img_path_2 = "./huge_r2.jpg"
img_1 = cv2.imread(img_path_1, cv2.IMREAD_COLOR)
img_2 = cv2.imread(img_path_2, cv2.IMREAD_COLOR)
img_1_resize = cv2.resize(img_1, (int(img_1.shape[1]/2), int(img_1.shape[0]/2)))
img_2_resize = cv2.resize(img_2, (int(img_2.shape[1]/2), int(img_2.shape[0]/2)))
img_concat1 = np.concatenate((img_1_resize, img_2_resize), axis=0)#vertical
img_concat2 = np.concatenate((img_concat1, img_concat1), axis=1)#horizontal
img_concat2 = cv2.hconcat((img_concat2, img_concat2))#horizontal
cv2.imshow("concat2", img_concat2)
cv2.waitKey(0)
#图像旋转
if 1:
img_path_1 = "./huge_r2.jpg"
img_1 = cv2.imread(img_path_1, cv2.IMREAD_COLOR)
A1 = cv2.getRotationMatrix2D((int(img_1.shape[1]/2.0), int(img_1.shape[0]/2.0)), 30, 1.0)
d1 = cv2.warpAffine(img_1, A1, (img_1.shape[1], img_1.shape[0]), borderValue = 125)
A2 = cv2.getRotationMatrix2D((int(img_1.shape[1]/2.0), int(img_1.shape[0]/2.0)), -30, 1.0)
d2 = cv2.warpAffine(img_1, A2, (img_1.shape[1], img_1.shape[0]), borderValue = 125)
cv2.imshow("30", d1)
cv2.imshow("-30", d2)
cv2.waitKey(0)
#图片叠加
if 0:
img_path_base = "./fengjing.jpg"
img_path_1 = "./huge_r2.jpg"
img_path_2 = "./zhaoliying_r2.jpg"
img_base = cv2.imread(img_path_base, cv2.IMREAD_COLOR)
img_1 = cv2.imread(img_path_1, cv2.IMREAD_COLOR)
img_2 = cv2.imread(img_path_2, cv2.IMREAD_COLOR)
A1 = cv2.getRotationMatrix2D((int(img_1.shape[1]/2.0), int(img_1.shape[0]/2.0)), 30, 1.0)
d1 = cv2.warpAffine(img_1, A1, (img_1.shape[1], img_1.shape[0]), borderValue = 125)
cv2.imshow("a", d1)
img_1_resize = cv2.resize(img_1, (int(img_1.shape[1]/2), int(img_1.shape[0]/2)))
img_2_resize = cv2.resize(img_2, (int(img_2.shape[1]/2), int(img_2.shape[0]/2)))
h_s = img_base.shape[0]-img_1_resize.shape[0]
h_e = img_base.shape[0]
w_s = int(img_base.shape[1]/2)-img_1_resize.shape[1]
w_e = int(img_base.shape[1]/2)
img_roi1 = img_base[h_s:h_e, w_s:w_e]
img_roi1 = cv2.addWeighted(img_roi1, 0.3, img_1_resize, 0.8, 0)
img_base[h_s:h_e, w_s:w_e] = img_roi1[:, :]
h_s = img_base.shape[0]-img_2_resize.shape[0]
h_e = img_base.shape[0]
w_s = int(img_base.shape[1]/2)
w_e = int(img_base.shape[1]/2)+img_1_resize.shape[1]
img_roi2 = img_base[h_s:h_e, w_s:w_e]
img_roi2 = cv2.addWeighted(img_roi2, 0.5, img_2_resize, 0.7, 0)
img_base[h_s:h_e, w_s:w_e] = img_roi2[:, :]
cv2.imshow("img_base", img_base)
cv2.waitKey(0)