Skip to content

Commit 0c14fea

Browse files
committed
test
1 parent ddbb833 commit 0c14fea

10 files changed

Lines changed: 275 additions & 0 deletions

File tree

156 KB
Binary file not shown.
156 KB
Binary file not shown.

python_opencv/3.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#decoding:utf-8
2+
import numpy as np
3+
from math import *
4+
import sys
5+
import os
6+
import glob
7+
import argparse
8+
import cv2 as cv
9+
10+
drag_start = None#全局变量取方块鼠标拖拽时使用
11+
sel = (0,0,0,0)#全局变量 长方形左上颌右下定点坐标存储
12+
13+
def onmouse(event, x, y, flags, param):#鼠标事件响应函数
14+
global drag_start, sel
15+
if event == cv.EVENT_LBUTTONDOWN:#左键按下时记录当前初始坐标,并初始化矩形sel
16+
drag_start = x, y
17+
sel = 0,0,0,0
18+
elif event == cv.EVENT_LBUTTONUP:#鼠标左键叹弹起时响应
19+
if sel[2] > sel[0] and sel[3] > sel[1]:#判断右下角坐标是否大于左上角
20+
patch = gray[sel[1]:sel[3],sel[0]:sel[2]]#取矩形区域内像素作为patch图像
21+
result = cv.matchTemplate(gray,patch,cv.TM_CCOEFF_NORMED)
22+
result = np.abs(result)**3
23+
val, result = cv.threshold(result, 0.01, 0, cv.THRESH_TOZERO)#将低于0。01的值赋值为0
24+
result8 = cv.normalize(result,None,0,255,cv.NORM_MINMAX,cv.CV_8U)#将result转化到0-255区间
25+
cv.imshow("result", result8)
26+
drag_start = None
27+
elif drag_start:
28+
#print flags
29+
if flags & cv.EVENT_FLAG_LBUTTON:#取当前坐标与初始坐标较小的为矩形坐标左上,较大的为右下
30+
minpos = min(drag_start[0], x), min(drag_start[1], y)
31+
maxpos = max(drag_start[0], x), max(drag_start[1], y)
32+
sel = minpos[0], minpos[1], maxpos[0], maxpos[1]
33+
img = cv.cvtColor(gray, cv.COLOR_GRAY2BGR)
34+
cv.rectangle(img, (sel[0], sel[1]), (sel[2], sel[3]), (0,255,255), 1)
35+
cv.imshow("gray", img)
36+
else:
37+
print "selection is complete"
38+
drag_start = None
39+
40+
if __name__ == '__main__':
41+
parser = argparse.ArgumentParser(description='Demonstrate mouse interaction with images')
42+
parser.add_argument("-i","--input", default='../', help="Input directory.")
43+
args = parser.parse_args()
44+
path = args.input#获取图像路径参数
45+
46+
cv.namedWindow("gray",1)
47+
cv.setMouseCallback("gray", onmouse)
48+
'''''Loop through all the images in the directory'''
49+
for infile in glob.glob( os.path.join(path, '*.*') ):#遍历文件夹下的图片文件
50+
ext = os.path.splitext(infile)[1][1:] #get the filename extenstion
51+
if ext == "png" or ext == "jpg" or ext == "bmp" or ext == "tiff" or ext == "pbm":
52+
print infile
53+
54+
img=cv.imread(infile,1)
55+
if img == None:
56+
continue
57+
sel = (0,0,0,0)
58+
drag_start = None
59+
gray=cv.cvtColor(img, cv.COLOR_BGR2GRAY)
60+
cv.imshow("gray",gray)
61+
if (cv.waitKey() & 255) == 27:
62+
break
63+
cv.destroyAllWindows()

python_opencv/3.py~

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#decoding:utf-8
2+
#!/usr/bin/env python
3+
import numpy as np
4+
from math import *
5+
import sys
6+
import os
7+
import glob
8+
import argparse
9+
import cv2 as cv
10+
11+
drag_start = None#全局变量取方块鼠标拖拽时使用
12+
sel = (0,0,0,0)#全局变量 长方形左上颌右下定点坐标存储
13+
14+
def onmouse(event, x, y, flags, param):#鼠标事件响应函数
15+
global drag_start, sel
16+
if event == cv.EVENT_LBUTTONDOWN:#左键按下时记录当前初始坐标,并初始化矩形sel
17+
drag_start = x, y
18+
sel = 0,0,0,0
19+
elif event == cv.EVENT_LBUTTONUP:#鼠标左键叹弹起时响应
20+
if sel[2] > sel[0] and sel[3] > sel[1]:#判断右下角坐标是否大于左上角
21+
patch = gray[sel[1]:sel[3],sel[0]:sel[2]]#取矩形区域内像素作为patch图像
22+
result = cv.matchTemplate(gray,patch,cv.TM_CCOEFF_NORMED)
23+
result = np.abs(result)**3
24+
val, result = cv.threshold(result, 0.01, 0, cv.THRESH_TOZERO)#将低于0。01的值赋值为0
25+
result8 = cv.normalize(result,None,0,255,cv.NORM_MINMAX,cv.CV_8U)#将result转化到0-255区间
26+
cv.imshow("result", result8)
27+
drag_start = None
28+
elif drag_start:
29+
#print flags
30+
if flags & cv.EVENT_FLAG_LBUTTON:#取当前坐标与初始坐标较小的为矩形坐标左上,较大的为右下
31+
minpos = min(drag_start[0], x), min(drag_start[1], y)
32+
maxpos = max(drag_start[0], x), max(drag_start[1], y)
33+
sel = minpos[0], minpos[1], maxpos[0], maxpos[1]
34+
img = cv.cvtColor(gray, cv.COLOR_GRAY2BGR)
35+
cv.rectangle(img, (sel[0], sel[1]), (sel[2], sel[3]), (0,255,255), 1)
36+
cv.imshow("gray", img)
37+
else:
38+
print "selection is complete"
39+
drag_start = None
40+
41+
if __name__ == '__main__':
42+
parser = argparse.ArgumentParser(description='Demonstrate mouse interaction with images')
43+
parser.add_argument("-i","--input", default='../', help="Input directory.")
44+
args = parser.parse_args()
45+
path = args.input#获取图像路径参数
46+
47+
cv.namedWindow("gray",1)
48+
cv.setMouseCallback("gray", onmouse)
49+
'''''Loop through all the images in the directory'''
50+
for infile in glob.glob( os.path.join(path, '*.*') ):#遍历文件夹下的图片文件
51+
ext = os.path.splitext(infile)[1][1:] #get the filename extenstion
52+
if ext == "png" or ext == "jpg" or ext == "bmp" or ext == "tiff" or ext == "pbm":
53+
print infile
54+
55+
img=cv.imread(infile,1)
56+
if img == None:
57+
continue
58+
sel = (0,0,0,0)
59+
drag_start = None
60+
gray=cv.cvtColor(img, cv.COLOR_BGR2GRAY)
61+
cv.imshow("gray",gray)
62+
if (cv.waitKey() & 255) == 27:
63+
break
64+
cv.destroyAllWindows()

python_opencv/4.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#coding:utf-8
2+
# 导入必要的软件包
3+
import argparse
4+
import datetime
5+
import imutils
6+
import time
7+
import cv2
8+
9+
# 创建参数解析器并解析参数
10+
ap = argparse.ArgumentParser()
11+
ap.add_argument("-v", "--video", help="path to the video file")
12+
ap.add_argument("-a", "--min-area", type=int, default=500, help="minimum area size")
13+
args = vars(ap.parse_args())
14+
15+
# 如果video参数为None,那么我们从摄像头读取数据
16+
if args.get("video", None) is None:
17+
camera = cv2.VideoCapture(0)
18+
time.sleep(0.25)
19+
20+
# 否则我们读取一个视频文件
21+
else:
22+
camera = cv2.VideoCapture(args["video"])
23+
24+
# 初始化视频流的第一帧
25+
firstFrame = None
26+
27+
# 遍历视频的每一帧
28+
while True:
29+
# 获取当前帧并初始化occupied/unoccupied文本
30+
(grabbed, frame) = camera.read()
31+
text = "Unoccupied"
32+
33+
# 如果不能抓取到一帧,说明我们到了视频的结尾
34+
if not grabbed:
35+
break
36+
37+
# 调整该帧的大小,转换为灰阶图像并且对其进行高斯模糊
38+
frame = imutils.resize(frame, width=500)
39+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
40+
gray = cv2.GaussianBlur(gray, (21, 21), 0)
41+
42+
# 如果第一帧是None,对其进行初始化
43+
if firstFrame is None:
44+
firstFrame = gray
45+
continue
46+
# 计算当前帧和第一帧的不同
47+
frameDelta = cv2.absdiff(firstFrame, gray)
48+
thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
49+
50+
# 扩展阀值图像填充孔洞,然后找到阀值图像上的轮廓
51+
thresh = cv2.dilate(thresh, None, iterations=2)
52+
(cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
53+
cv2.CHAIN_APPROX_SIMPLE)
54+
55+
# 遍历轮廓
56+
for c in cnts:
57+
# if the contour is too small, ignore it
58+
if cv2.contourArea(c) < args["min_area"]:
59+
continue
60+
61+
# compute the bounding box for the contour, draw it on the frame,
62+
# and update the text
63+
# 计算轮廓的边界框,在当前帧中画出该框
64+
(x, y, w, h) = cv2.boundingRect(c)
65+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
66+
text = "Occupied"
67+
# draw the text and timestamp on the frame
68+
# 在当前帧上写文字以及时间戳
69+
cv2.putText(frame, "Room Status: {}".format(text), (10, 20),
70+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
71+
cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"),
72+
(10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
73+
74+
# 显示当前帧并记录用户是否按下按键
75+
cv2.imshow("Security Feed", frame)
76+
cv2.imshow("Thresh", thresh)
77+
cv2.imshow("Frame Delta", frameDelta)
78+
key = cv2.waitKey(1) &amp; 0xFF
79+
80+
# 如果q键被按下,跳出循环
81+
if key == ord("q"):
82+
break
83+
84+
# 清理摄像机资源并关闭打开的窗口
85+
camera.release()
86+
cv2.destroyAllWindows()

python_opencv/4.py~

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#coding:utf-8
2+
# 导入必要的软件包
3+
import argparse
4+
import datetime
5+
import imutils
6+
import time
7+
import cv2
8+
9+
# 创建参数解析器并解析参数
10+
ap = argparse.ArgumentParser()
11+
ap.add_argument("-v", "--video", help="path to the video file")
12+
ap.add_argument("-a", "--min-area", type=int, default=500, help="minimum area size")
13+
args = vars(ap.parse_args())
14+
15+
# 如果video参数为None,那么我们从摄像头读取数据
16+
if args.get("video", None) is None:
17+
camera = cv2.VideoCapture(0)
18+
time.sleep(0.25)
19+
20+
# 否则我们读取一个视频文件
21+
else:
22+
camera = cv2.VideoCapture(args["video"])
23+
24+
# 初始化视频流的第一帧
25+
firstFrame = None
26+
27+
# 遍历视频的每一帧
28+
while True:
29+
# 获取当前帧并初始化occupied/unoccupied文本
30+
(grabbed, frame) = camera.read()
31+
text = "Unoccupied"
32+
33+
# 如果不能抓取到一帧,说明我们到了视频的结尾
34+
if not grabbed:
35+
break
36+
37+
# 调整该帧的大小,转换为灰阶图像并且对其进行高斯模糊
38+
frame = imutils.resize(frame, width=500)
39+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
40+
gray = cv2.GaussianBlur(gray, (21, 21), 0)
41+
42+
# 如果第一帧是None,对其进行初始化
43+
if firstFrame is None:
44+
firstFrame = gray
45+
continue

python_opencv/psb_face/2.jpg

-5.29 KB
Binary file not shown.

test-gcc/.1.py.swp

12 KB
Binary file not shown.

test-gcc/1.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#coding:utf-8
2+
a = 1
3+
while a < 100:
4+
print a
5+
a += 1
6+
#中文
7+
8+
9+
10+

test-gcc/1.py~

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
a = 1
2+
while a < 100:
3+
print a
4+
a += 1
5+
6+
7+

0 commit comments

Comments
 (0)