_, img = cap.read() zeros_image = np.zeros((img.shape[0], img.shape[1], 1), np.uint8)
img_ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
blur = cv2.GaussianBlur(img_ycrcb, (11, 11), 0)
skin_ycrcb_min = np.array((0, 0, 140))
skin_ycrcb_max = np.array((65, 253, 255))
skin_ycrcb_min = np.array((0, 0, 180))
skin_ycrcb_max = np.array((140, 255, 255))
mask = cv2.inRange(blur, skin_ycrcb_min, skin_ycrcb_max)
contours, hierarchy = cv2.findContours(
mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2:]
valid_cntrs = []
for i, cnt in enumerate(contours):
x, y, w, h = cv2.boundingRect(cnt)
area = cv2.contourArea(cnt)
cX = int(x (w/2))
cY = int(y (h/2))
if (x <= 600) & (y >= 300) & (y <= 455) & (area > 900):
valid_cntrs.append(cnt)
cv2.putText(img, str(f'{cX},{cY}'), (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,
1, (255, 0, 0), 2, cv2.LINE_AA)
cv2.rectangle(zeros_image, (x, y), (x w, y h), (255),
thickness=cv2.FILLED)
mask1 = np.zeros((zeros_image.shape[0], zeros_image.shape[1], 1), np.uint8)
mask_result = cv2.bitwise_or(zeros_image, zeros_image, mask=Sensor1.mask)
white_cell_number = np.sum(mask_result == 255)
sensor_rate = white_cell_number/Sensor1.full_mask_area
sensor_rate 列印出介于 0.85 到 31 之間或小于 0.85 的值
uj5u.com熱心網友回復:
原始代碼:
# detect whether there is car via bitwise_and
mask1=np.zeros((zeros_image.shape[0],zeros_image.shape[1],1),np.uint8)
mask_result=cv2.bitwise_or(zeros_image,zeros_image,mask=Sensor1.mask)
white_cell_number=np.sum(mask_result==255)
# detect to control whether car is passing under the red line sensor
sensor_rate=white_cell_number/Sensor1.full_mask_area
white_cell_number是 中的白色像素數mask_result。
sensor_rate是白色像素與 中總像素數的比率Sensor1.full_mask_area。
進一步查找代碼:
class Sensor:
def __init__(self,kordinat1,kordinat2,frame_weight,frame_lenght):
self.kordinat1=kordinat1
self.kordinat2=kordinat2
self.frame_weight=frame_weight
self.frame_lenght =frame_lenght
self.mask=np.zeros((frame_weight,frame_lenght,1),np.uint8)*abs(self.kordinat2.y-self.kordinat1.y)
self.full_mask_area=abs(self.kordinat2.x-self.kordinat1.x)
cv2.rectangle(self.mask,(self.kordinat1.x,self.kordinat1.y),(self.kordinat2.x,self.kordinat2.y),(255),thickness=cv2.FILLED)
self.stuation=False
self.car_number_detected=0
video=cv2.VideoCapture("video1.mp4")
ret,frame=video.read()
cropped_image= frame[0:450, 0:450]
fgbg=cv2.createBackgroundSubtractorMOG2()
Sensor1 = Sensor(
Kordinat(1, cropped_image.shape[1] - 35),
Kordinat(340, cropped_image.shape[1] - 30),
cropped_image.shape[0],
cropped_image.shape[1])
Sensor1.full_mask_area 是由兩點定義的矩形。
所以sensor_rate是矩形的填充速率。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/363363.html
上一篇:opencv-python:如何識別影像中的粉紅色木頭?
下一篇:無法在Java中創建影像檔案
