python opencv数据集制作
代码
import os
import cv2
import numpy as np
def background(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
ret, binary = cv2.threshold(gray, 112, 255, cv2.THRESH_BINARY_INV)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (25, 25))
dilate = cv2.dilate(binary, kernel, iterations = 4)
erode = cv2.erode(dilate, kernel, iterations = 3)
return cv2.bitwise_and(frame, frame, mask=erode)
return binary
def resize(frame, scale):
h, w, _ = frame.shape
return cv2.resize(frame,(int(w*scale), int(h*scale)),)
def get_frame():
cap = cv2.VideoCapture("C:\\Users\\john\\Downloads\\pliers.mp4")
if (cap.isOpened()== False):
print("Error opening video stream or file")
ret, frame = cap.read()
frame_count = 0
# while ret == True:
while frame_count < 10:
filename = "C:\\Users\\john\\Downloads\\frame%s.jpg"%(frame_count)
print(filename)
frame = background(frame)
result = resize(frame, 0.1)
cv2.imwrite(filename, result)
ret, frame = cap.read()
frame_count += 1
def paste(img, roi, x, y):
img[x:x + roi.shape[0], y:y+roi.shape[1]] = roi
if __name__ == '__main__':
img = np.zeros((1080,1920,3), np.uint8)
img.fill(0)
# 读取待复制图片
roi = cv2.imread("C:\\Users\\john\\Downloads\\frame1.jpg");
# 设置画布绘制区域并复制
paste(img, roi, 100, 1000)
filename = "C:\\Users\\john\\Downloads\\empty.jpg"
cv2.imwrite(filename, img)