zoukankan      html  css  js  c++  java
  • 【opencv】opencv图像识别的一些基础的基础函数的使用方法

    import cv2
    import numpy as np
    from matplotlib import pyplot as plt
    
    pic_path = "deal_with.png"
    
    
    def cv_imread():
        """
        读取图片
        :return:
        """
        # imread的第二个参数告诉函数如何读取这幅图片
        # 0:以灰度模式读入图像
        # 1:读入一副彩色图像,图像的透明度会被忽略,这是默认参数
        # -1:读入一幅图像,并且包括图像的alpha通道
        img = cv2.imread(pic_path, 0)
        # print(img)
        return img
    
    
    def cv_imshow():
        """
        显示图片
        :return:
        """
        img = cv_imread()
        cv2.imshow('image', img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    
    
    def cv_imwrite():
        """
        保存图片
        :return:
        """
        img = cv_imread()
        cv2.imwrite(pic_path, img)
    
    
    def matplotlib_show_pic():
        """
        使用matplotlib
        :return:
        """
        img = cv_imread()
        plt.imshow(img, cmap='gray', interpolation='bicubic')
        plt.xticks([]), plt.yticks([])
        plt.show()
    
    
    def cv2_video_cap():
        """
        摄像头
        :return:
        """
        cap = cv2.VideoCapture(0)
        while True:
            ret, frame = cap.read()
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            cv2.imshow('frame', gray)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        cap.release()
        cv2.destroyAllWindows()
    
    
    def cv_line():
        """
        画线
        :return:
        """
        img = np.zeros((512, 512, 3), np.uint8)  # 读取图像,这里是一个黑色的图片
        cv2.line(img, (0, 0), (511, 511), (255, 0, 0), 5)  # 参数:图像,起点坐标,终点坐标,颜色,粗细(-1为填充)
        cv2.imshow('img', img)
        cv2.waitKey(0)
    
    
    def cv_rectangle():
        """
        画矩形
        :return:
        """
        img = np.zeros((512, 512, 3), np.uint8)  # 读取图像,这里是一个黑色的图片
        cv2.rectangle(img, (384, 0), (510, 128), (0, 255, 0), 3)  # 参数:图像,左上角顶点坐标,右下角顶点坐标,颜色,粗细(-1为填充)
        cv2.imshow('img', img)
        cv2.waitKey(0)
    
    
    def cv_circle():
        """
        画圆
        :return:
        """
        img = np.zeros((512, 512, 3), np.uint8)  # 读取图像,这里是一个黑色的图片
        cv2.circle(img, (447, 63), 63, (0, 255, 0), 1)  # 参数:图像,圆形中心坐标,半径大小,颜色,粗细(-1为填充)
        cv2.imshow('img', img)
        cv2.waitKey(0)
    
    
    def cv_ellipse():
        """
        画椭圆
        :return:
        """
        img = np.zeros((512, 512, 3), np.uint8)  # 读取图像,这里是一个黑色的图片
        cv2.ellipse(img, (256, 256), (100, 50), 0, 0, 360, 255,
                    -1)  # 参数:图像,中心点的坐标,长轴、短轴的长度,椭圆沿逆时针方向旋转的角度,椭圆弧演顺时针方向起始的角度和结束角度,如果是 0 跟 360,就是整个椭圆,颜色,粗细
        cv2.imshow('img', img)
        cv2.waitKey(0)
    
    
    def cv_put_text():
        """
        图片上添加文字
        :return:
        """
        img = np.zeros((512, 512, 3), np.uint8)  # 读取图像,这里是一个黑色的图片
        font = cv2.FONT_HERSHEY_SIMPLEX  # 字体类型
        cv2.putText(img, 'OpenCV', (10, 500), font, 4, (255, 255, 255), 2)  # 参数:图像,要绘制的文字,要绘制的位置,字体类型,字体大小,颜色,粗细
        cv2.imshow('img', img)
        cv2.waitKey(0)
  • 相关阅读:
    实现Path2.0中绚丽的的旋转菜单
    ColorMatrixColorFilter颜色过滤(离线用户的灰色头像处理)
    网上发现的一个android UI包
    圆角背景的ListView
    自定义Gallery 滑动中图片自动突出显示
    python文件读写操作(r/r+/rb/w/w+/wb/a/a+/ab)
    Linux(deepin) 系统: 解决 matplotlib 中文乱码问题
    python文件读写操作(r/r+/rb/w/w+/wb/a/a+/ab)
    API接口防止参数篡改和重放攻击
    API接口防止参数篡改和重放攻击
  • 原文地址:https://www.cnblogs.com/fengzx120/p/11038460.html
Copyright © 2011-2022 走看看