首页 > 其他分享 >openCV抓图片

openCV抓图片

时间:2022-10-28 09:00:57浏览次数:73  
标签:loc target min cv2 template openCV human 图片

import numpy as np
import cv2
from PIL import ImageGrab
from demo import get_XY, classify_hist_with_split
import time

cap = cv2.VideoCapture(0)  # 开启摄像头


def find_picture(target, template):
    theight, twidth = template.shape[:2]
    result = cv2.matchTemplate(target, template, cv2.TM_SQDIFF_NORMED)
    cv2.normalize(result, result, 0, 1, cv2.NORM_MINMAX, -1)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
    cv2.rectangle(target, min_loc, (min_loc[0] + twidth, min_loc[1] + theight), (0, 0, 225), 2)
    x = min_loc[0]
    y = min_loc[1]

    return x, y


def get_xy(A, base='image.jpg'):
    """获取A在B的坐标, 反返回A的高和宽"""
    target = cv2.imread(A)
    w, h,  = target.shape[0], target.shape[1]  # 图片的高宽
    if base == 'image.jpg':
        template = cv2.imread(base)
    else:
        template = base
    x, y = find_picture(target, template)
    print(A, "的坐标", x, y, h, w)
    return x, y, w, h


def get_pic_from_pic(x, y, w, h, target):
    region = target[y:y + h, x:x + w]
    return region


if __name__ == '__main__':
    # x, y, h, w = get_xy("yanjing.jpg", "image.jpg")
    #
    # human_box = [x, y, w+x, h+y]

    while True:
        screen = ImageGrab.grab(bbox=(0, 0, 800, 600))
        print(screen)
        frame = cv2.cvtColor(np.array(screen), cv2.COLOR_RGB2BGR)
        # cv2.imshow("threshold", frame)  # 显示图像
        x, y, h, w = get_xy("yanjing.jpg", frame)
        human_box = [x, y, x + w, h + y]
        # cv2.waitKey(25)
        # img = cv2.imread(image_path)
        # img = cv2.imread("image.jpg")
        cv2.rectangle(frame, (human_box[0], human_box[1]), (human_box[2], human_box[3]), (255, 255, 0), 3)  #

        cv2.imshow("output_path", frame)
        cv2.waitKey(25)

标签:loc,target,min,cv2,template,openCV,human,图片
From: https://www.cnblogs.com/zxyg/p/16834654.html

相关文章

  • CSS3关于背景图片应用的总结
    背景的基本属性background-color(背景颜色)background-image(背景图片)background-repeat(背景图片展示的方式)background-attachment(背景图片滚动还是固定)background-po......
  • 前端性能优化(图片优化)
    从输入URL到页面加载完成的过程:首先通过DNS(域名解析)把URL解析为对应的IP地址,然后与该IP地址确定的服务器建立起TCP网络连接。随后向服务器发送HTTP请求,服务器处理完HTTP请求......
  • 图片保持比例最大填充
    图片保持比例最大填充各边取容器对应边与容器临边按比例计算的此边,两边长度较小者。Picture_New_Width=math.min( Picture_Container_Width, Picture_Container_He......
  • wordpress 调用特色图片
    调用代码get_post_thumbnail_id():获取文章缩略图IDget_the_post_thumbnail_url():获取文章缩略图链接the_post_thumbnail_url():这个函数直接显示文章缩略图链接get_the......
  • 背景图片模糊效果
    <html><head><style>html,body{height:100%;width:100%;}body{background-ima......
  • chrome: css:transform:scale时部分缩放比例相邻图片间有间隔缝隙(chrome 106.0.5249.
    一,问题的表现:页面上左右两个div,里面各有一张图片,图片是相邻的,在页面上应该象一张图一样显示,代码如下:<!--background:#000000;--><divstyle="width:100%;heigh......
  • Iup Label 加载图片
    IupLabel加载图片(Image)label.IMAGE=picture_path_gbkiup.SetAttribute(picture_ih,'IMAGE',picture_path_gbk)两者貌似等价。需用系统编码。当前系统编码为GBK,而......
  • OpenCV-Python learning-13.人脸检测
    如下,调用opencv使用摄像头或视频进行人脸检测,也可以在函数​​recognize(img)​​​传入​​img=cv2.imread('face.jpg')​​​。其中,人脸级联分类器xml文件我引用的是anaco......
  • 天池零样本:禁止使用外部图片数据/预训练模型进行竞赛的原因
    既然是零样本分类,在测试集中并不希望出现参与训练的类别实例,如果我们使用预训练模型,比如基于Imagenet数据集的,难保测试集中的骡子没有在ImageNet中出现过,外部数据同理。如果......
  • opencv-contrib-python的安装:Set OPENCV_ENABLE_NONFREE CMake option and rebuild th
    问题描述前往本页,可查看opencv-python和opencv-contrib-python的区别。​​https://docs.opencv.org/master/​​比如,SIFT就需要用到opencv-contrib-python包中的cv2.xfeat......