sha_xian.py

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)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cc_sensii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值