作者 | 努比
来源 | 小白学视觉
C:\Users\Razvan>python --version
Python 3.7.6
(OpenCV) C:\Users\Razvan>python
Python 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print(cv2.__version__)
4.2.0
>>>
import cv2
import csv
VIDEO_SOURCE = 1
cap = cv2.VideoCapture(VIDEO_SOURCE)
suc, image = cap.read()
cv2.imwrite("frame0.jpg", image)
cap.release()
cv2.destroyAllWindows()
img = cv2.imread("frame0.jpg")
r = cv2.selectROIs('Selector', img, showCrosshair = False, fromCenter = False)
with open('data/rois.csv', 'w', newline='') as outf:
csvw = csv.writer(outf)
csvw.writerows(rlist)
def drawRectangle(img, a, b, c, d):
sub_img = img[b:b + d, a:a + c]
edges = cv2.Canny(sub_img, lowThreshold, highThreshold)
pix = cv2.countNonZero(edges)
if pix in range(min, max):
cv2.rectangle(img, (a, b), (a + c, b + d), (0, 255, 0), 3)
spots.loc += 1
else:
cv2.rectangle(img, (a, b), (a + c, b + d), (0, 0, 255), 3)
def callback(foo):
pass
cv2.namedWindow('parameters')
cv2.createTrackbar('Threshold1', 'parameters', 186, 700, callback)
cv2.createTrackbar('Threshold2', 'parameters', 122, 700, callback)
cv2.createTrackbar('Min pixels', 'parameters', 100, 1500, callback)
cv2.createTrackbar('Max pixels', 'parameters', 323, 1500, callback)
class spots:
loc = 0
with open('data/rois.csv', 'r', newline='') as inf:
csvr = csv.reader(inf)
rois = list(csvr)
rois = [[int(float(j)) for j in i] for i in rois]
VIDEO_SOURCE = 1
cap = cv2.VideoCapture(VIDEO_SOURCE)
while True:
spots.loc = 0
ret, frame = cap.read()
ret2, frame2 = cap.read()
min = cv2.getTrackbarPos('Min pixels', 'parameters')
max = cv2.getTrackbarPos('Max pixels', 'parameters')
lowThreshold = cv2.getTrackbarPos('Threshold1', 'parameters')
highThreshold = cv2.getTrackbarPos('Threshold2', 'parameters')
for i in range(len(rois)):
drawRectangle(frame, rois[i][0], rois[i][1], rois[i][2], rois[i][3])
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, 'Available spots: ' + str(spots.loc), (10, 30), font, 1, (0, 255, 0), 3)
cv2.imshow('Detector', frame)
canny = cv2.Canny(frame2, lowThreshold, highThreshold)
cv2.imshow('canny', canny)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
分享
点收藏
点点赞
点在看
文章转发自AI科技大本营微信公众号,版权归其所有。文章内容不代表本站立场和任何投资暗示。
Copyright © 2021.Company 元宇宙YITB.COM All rights reserved.元宇宙YITB.COM