Skip to content

Commit 14cfcbe

Browse files
committed
OpenCV_Window_Management
1 parent 03fb0fd commit 14cfcbe

File tree

9 files changed

+172
-42
lines changed

9 files changed

+172
-42
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017/7/18 下午11:14
3+
# @Author : play4fun
4+
# @File : __init__.py
5+
# @Software: PyCharm
6+
7+
"""
8+
__init__.py:
9+
"""
10+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017/7/18 下午10:34
3+
# @Author : play4fun
4+
# @File : opencv_windows_management.py
5+
# @Software: PyCharm
6+
7+
"""
8+
opencv_windows_management.py:
9+
"""
10+
11+
import cv2, math
12+
import tkinter as tk
13+
14+
15+
class Window:
16+
def __init__(self, name, image, weight=1):
17+
self.name = name
18+
self.image = image.copy()
19+
self.weight = weight
20+
self.shape = self.image.shape
21+
self.hight_x = self.shape[0]
22+
self.lenght_y = self.shape[1]
23+
24+
25+
class opencv_windows_management:
26+
def __init__(self):
27+
self.windows = dict()
28+
29+
root = tk.Tk()
30+
screen_width = root.winfo_screenwidth()
31+
screen_height = root.winfo_screenheight()
32+
self.screen_size = (screen_width, screen_height) # (1280, 800)
33+
root.quit()
34+
35+
def add(self, name, image, weight=1):
36+
'''
37+
权重,越高,图片显示越大
38+
:return:
39+
'''
40+
cv2.namedWindow(name, flags=cv2.WINDOW_AUTOSIZE)
41+
window = Window(name, image, weight)
42+
self.windows[name] = window
43+
# self.windows[name] = image
44+
45+
def show(self):
46+
lenw = len(self.windows)
47+
w_l = int(self.screen_size[0] / lenw)
48+
49+
max_num_line = math.ceil(math.sqrt(lenw)) # 取平方根
50+
# TODO 权重
51+
52+
for i, name in enumerate(self.windows):
53+
# if (i+1) >max_num_line:
54+
# #TODO 换行
55+
# cv2.moveWindow(name, w_l * i, h_x*j)
56+
# pass
57+
58+
win = self.windows[name]
59+
image = win.image
60+
# image = self.windows[name]
61+
# h_x = int(image.shape[1] / w_l * image.shape[0]) #保持比例
62+
h_x = int(w_l / win.lenght_y * win.hight_x) # 保持比例
63+
# print((w_l,h_x))
64+
img2 = cv2.resize(image, (w_l, h_x))
65+
cv2.moveWindow(name, w_l * i, 0)
66+
cv2.imshow(name, img2)

Tools工具包/OpenCV_Window_Management/test_cvwm.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

Tools工具包/OpenCV_Window_Management/test_cvwm_images.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,34 @@
66

77
"""
88
test_cvwm_images.py:
9+
# show 多张相片
910
"""
1011

12+
import cv2
13+
import numpy as np
14+
import os
15+
import errno
16+
from opencv_windows_management import opencv_windows_management
17+
18+
cvwm = opencv_windows_management()
19+
20+
path = '../../data/messi5.jpg'
21+
if not os.path.exists(path):
22+
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), path)
23+
24+
img = cv2.imread(path, cv2.IMREAD_UNCHANGED) # 包括图像的 alpha 通道
25+
print(img.shape)
26+
# cv2.imshow('src', img)
27+
cvwm.add('src', img)
28+
29+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
30+
31+
# cv2.imshow('gray', gray)
32+
cvwm.add('gray', gray)
33+
34+
ret, thresh1 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
35+
cvwm.add('thresh1', thresh1)
36+
37+
cvwm.show()
38+
39+
cv2.waitKey(0)

Tools工具包/OpenCV_Window_Management/test_cvwm_videos.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,41 @@
88
test_cvwm_videos.py:
99
"""
1010

11+
import cv2
12+
from opencv_windows_management import opencv_windows_management
13+
14+
cvwm = opencv_windows_management()
15+
16+
cap = cv2.VideoCapture(0)
17+
ret = cap.set(3, 640)
18+
ret = cap.set(4, 480)
19+
20+
#
21+
face_cascade = cv2.CascadeClassifier('/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml')
22+
23+
while cap.isOpened():
24+
ret, frame = cap.read()
25+
26+
frame = cv2.flip(frame, flipCode=1)
27+
cvwm.add('frame', frame)
28+
29+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
30+
# cv2.imshow('frame', gray)
31+
cvwm.add('gray', gray)
32+
33+
#人脸识别
34+
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
35+
print("Detected ", len(faces), " face")
36+
for (x, y, w, h) in faces:
37+
face = gray[y:y + h, x:x + w]
38+
cvwm.add('face', face)
39+
40+
cvwm.show()
41+
42+
key = cv2.waitKey(delay=1)
43+
if key == ord("q"):
44+
break
45+
46+
# When everything done, release the capture
47+
cap.release()
48+
cv2.destroyAllWindows()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017/7/19 上午10:52
3+
# @Author : play4fun
4+
# @File : cv2_imread_path_not_found.py
5+
# @Software: PyCharm
6+
7+
"""
8+
cv2_imread_path_not_found.py:
9+
"""
10+
11+
import cv2
12+
import numpy as np
13+
import os
14+
import errno
15+
16+
path = 'messi6.jpg'#不正确的路径,文件不存在
17+
# path = '../data/messi5.jpg'
18+
if not os.path.exists(path):
19+
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), path)
20+
21+
img = cv2.imread(path, cv2.IMREAD_UNCHANGED)
22+
23+
cv2.imshow('src', img)
24+
cv2.waitKey(0)

ch05-视频/5.VideoCapture.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
# ret=cap.set(3,320)
2525
# ret=cap.set(4,240)
2626

27-
ret = cap.set(3, 640)
27+
ret = cap.set(3, 640)#避免计算量过大
2828
ret = cap.set(4, 480)
2929

3030
# while (True):

ch14-几何变换/14.仿射变换getAffineTransform.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525

2626
plt.figure(figsize=(8, 7), dpi=98)
2727
p1 = plt.subplot(211)
28-
p1.imshow(img)
28+
p1.show(img)
2929
p1.set_title('Input')
3030

3131
p2 = plt.subplot(212)
32-
p2.imshow(dst)
32+
p2.show(dst)
3333
p2.set_title('Output')
3434

3535
plt.show()

ch14-几何变换/14.透视变换getPerspectiveTransform.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424

2525
plt.figure(figsize=(8, 7), dpi=98)
2626
p1 = plt.subplot(211)
27-
p1.imshow(img)
27+
p1.show(img)
2828
p1.set_title('Input')
2929

3030
p2 = plt.subplot(212)
31-
p2.imshow(dst)
31+
p2.show(dst)
3232
p2.set_title('Output')
3333

3434
plt.show()

0 commit comments

Comments
 (0)