python opencv数据集制作

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)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值