import numpy as np
import cv2
import mian_ji
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
cap.set(3, 640)
cap.set(4, 480)
mj = mian_ji.Mian_ji()
area = input("请输入面积阈值s(0<s<9000):")
num = int(area)
while True:
ret, img = cap.read()
img = cv2.flip(img, 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
step1 = mj.step_first(img)
step2 = mj.step_two(step1)
step3 = mj.step_three(step2, img, num)
cv2.imshow('video', img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
import numpy as np
import cv2
class Mian_ji:
def __init__(self):
return
def step_first(self, name):
lower = np.uint8([150, 150, 150])
upper = np.uint8([255, 255, 255])
white_mask = cv2.inRange(name, lower, upper)
return white_mask
def step_two(self, image):
white_pixel = 0
width = image.shape[0]
height = image.shape[1]
for i in range(150, 300):
for j in range(10, 70):
current_pixel = image[i, j]
if current_pixel == (255):
white_pixel += 1
return white_pixel
def step_three(self, area, img, num):
a = str(round(area/9000, 2))
if area > num:
print("stop!")
cv2.putText(img, a, (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 2.0, (0, 0, 255), cv2.LINE_AA)
else:
cv2.putText(img, a, (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 2.0, (255, 0, 0), cv2.LINE_AA)
cv2.rectangle(img, (10, 180), (120, 270), (55, 255, 155), 3)