Skip to content

Commit

Permalink
OpenCV_Window_Management
Browse files Browse the repository at this point in the history
  • Loading branch information
makelove committed Jul 19, 2017
1 parent 03fb0fd commit 14cfcbe
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 42 deletions.
10 changes: 10 additions & 0 deletions Tools工具包/OpenCV_Window_Management/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
# @Time : 2017/7/18 下午11:14
# @Author : play4fun
# @File : __init__.py
# @Software: PyCharm

"""
__init__.py:
"""

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
# @Time : 2017/7/18 下午10:34
# @Author : play4fun
# @File : opencv_windows_management.py
# @Software: PyCharm

"""
opencv_windows_management.py:
"""

import cv2, math
import tkinter as tk


class Window:
def __init__(self, name, image, weight=1):
self.name = name
self.image = image.copy()
self.weight = weight
self.shape = self.image.shape
self.hight_x = self.shape[0]
self.lenght_y = self.shape[1]


class opencv_windows_management:
def __init__(self):
self.windows = dict()

root = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
self.screen_size = (screen_width, screen_height) # (1280, 800)
root.quit()

def add(self, name, image, weight=1):
'''
权重,越高,图片显示越大
:return:
'''
cv2.namedWindow(name, flags=cv2.WINDOW_AUTOSIZE)
window = Window(name, image, weight)
self.windows[name] = window
# self.windows[name] = image

def show(self):
lenw = len(self.windows)
w_l = int(self.screen_size[0] / lenw)

max_num_line = math.ceil(math.sqrt(lenw)) # 取平方根
# TODO 权重

for i, name in enumerate(self.windows):
# if (i+1) >max_num_line:
# #TODO 换行
# cv2.moveWindow(name, w_l * i, h_x*j)
# pass

win = self.windows[name]
image = win.image
# image = self.windows[name]
# h_x = int(image.shape[1] / w_l * image.shape[0]) #保持比例
h_x = int(w_l / win.lenght_y * win.hight_x) # 保持比例
# print((w_l,h_x))
img2 = cv2.resize(image, (w_l, h_x))
cv2.moveWindow(name, w_l * i, 0)
cv2.imshow(name, img2)
37 changes: 0 additions & 37 deletions Tools工具包/OpenCV_Window_Management/test_cvwm.py

This file was deleted.

29 changes: 29 additions & 0 deletions Tools工具包/OpenCV_Window_Management/test_cvwm_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,34 @@

"""
test_cvwm_images.py:
# show 多张相片
"""

import cv2
import numpy as np
import os
import errno
from opencv_windows_management import opencv_windows_management

cvwm = opencv_windows_management()

path = '../../data/messi5.jpg'
if not os.path.exists(path):
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), path)

img = cv2.imread(path, cv2.IMREAD_UNCHANGED) # 包括图像的 alpha 通道
print(img.shape)
# cv2.imshow('src', img)
cvwm.add('src', img)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# cv2.imshow('gray', gray)
cvwm.add('gray', gray)

ret, thresh1 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
cvwm.add('thresh1', thresh1)

cvwm.show()

cv2.waitKey(0)
38 changes: 38 additions & 0 deletions Tools工具包/OpenCV_Window_Management/test_cvwm_videos.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,41 @@
test_cvwm_videos.py:
"""

import cv2
from opencv_windows_management import opencv_windows_management

cvwm = opencv_windows_management()

cap = cv2.VideoCapture(0)
ret = cap.set(3, 640)
ret = cap.set(4, 480)

#
face_cascade = cv2.CascadeClassifier('/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml')

while cap.isOpened():
ret, frame = cap.read()

frame = cv2.flip(frame, flipCode=1)
cvwm.add('frame', frame)

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# cv2.imshow('frame', gray)
cvwm.add('gray', gray)

#人脸识别
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
print("Detected ", len(faces), " face")
for (x, y, w, h) in faces:
face = gray[y:y + h, x:x + w]
cvwm.add('face', face)

cvwm.show()

key = cv2.waitKey(delay=1)
if key == ord("q"):
break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
24 changes: 24 additions & 0 deletions ch04-图片/cv2_imread_path_not_found.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# @Time : 2017/7/19 上午10:52
# @Author : play4fun
# @File : cv2_imread_path_not_found.py
# @Software: PyCharm

"""
cv2_imread_path_not_found.py:
"""

import cv2
import numpy as np
import os
import errno

path = 'messi6.jpg'#不正确的路径,文件不存在
# path = '../data/messi5.jpg'
if not os.path.exists(path):
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), path)

img = cv2.imread(path, cv2.IMREAD_UNCHANGED)

cv2.imshow('src', img)
cv2.waitKey(0)
2 changes: 1 addition & 1 deletion ch05-视频/5.VideoCapture.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# ret=cap.set(3,320)
# ret=cap.set(4,240)

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

# while (True):
Expand Down
4 changes: 2 additions & 2 deletions ch14-几何变换/14.仿射变换getAffineTransform.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@

plt.figure(figsize=(8, 7), dpi=98)
p1 = plt.subplot(211)
p1.imshow(img)
p1.show(img)
p1.set_title('Input')

p2 = plt.subplot(212)
p2.imshow(dst)
p2.show(dst)
p2.set_title('Output')

plt.show()
4 changes: 2 additions & 2 deletions ch14-几何变换/14.透视变换getPerspectiveTransform.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@

plt.figure(figsize=(8, 7), dpi=98)
p1 = plt.subplot(211)
p1.imshow(img)
p1.show(img)
p1.set_title('Input')

p2 = plt.subplot(212)
p2.imshow(dst)
p2.show(dst)
p2.set_title('Output')

plt.show()

0 comments on commit 14cfcbe

Please sign in to comment.