open cv 常用工具总结1 —— 画线 (python版本)

Posted 凡先生的笔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了open cv 常用工具总结1 —— 画线 (python版本)相关的知识,希望对你有一定的参考价值。

一、核心函数解释

1)OpenCV库的引入:import cv2

OpenCV读取的图片,图片数据的组织形式为bgr;

2) 读取图像:cv2.imread(arg1, arg2)

函数参数:arg1表示想要读取的图片所在的位置;arg2表示读取模式;其中arg2 = 1表示读取彩色 arg2 = 0表示灰度;

3) 展示图片:cv2.imshow(arg1, img)

函数参数:arg1表示显示出来的图片窗口的名称; img表示需要展示的图片;

4) 等待输入:cv2.waitKey(n)

函数参数:n值表示等待用户输入需要的时间,单位时毫秒,n = 0图片一直在显示;

# -----------------------------open CV画分割线--------------------------
import cv2
import sys
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QApplication, QMainWindow

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.open_img()

    def open_img(self):
        num_1 = ['A', 'B', 'C', 'D', 'E', 'F']
        image1 = cv2.imread('D:/Users/xuyf2/Desktop/jier/left/cp1631759505img_right_NG.jpg', cv2.IMREAD_COLOR)
        image = self.LineAtext1(image1, 110, 130, 360, 80, 250, 90, num_1)
        cv2.namedWindow("image")
        cv2.imshow('image', image)
        cv2.waitKey(0)            # 图片一直显示
        cv2.destroyAllWindows()

    # 分割线
    def LineAtext1(self, image, left_x, left_y, right_x, right_y, define_long, define_width, num):
        font = cv2.FONT_HERSHEY_SIMPLEX
        text_size = 0.6
        text_color = (0, 0, 0)
        text_thin = 2
        line_color = (255, 0, 0)
        line_thin = 3
        # define_long = 250  #定义方框长度
        # define_width = 90  #定义方框宽度
        # image = cv2.imread(img_path)
        # B2
        cv2.line(image, (left_x, left_y), (left_x + define_long, left_y), line_color, line_thin)
        cv2.putText(image, num[0], (left_x + int(define_long / 2), left_y + int(define_width / 2)), font, text_size,text_color, text_thin)
        # B4
        cv2.line(image, (left_x, left_y + define_width), (left_x + define_long, left_y + define_width), line_color,line_thin)
        cv2.putText(image, num[1], (left_x + int(define_long / 2), left_y + 3 * int(define_width / 2)), font, text_size,text_color, text_thin)
        # B6
        cv2.line(image, (left_x, left_y + 2 * define_width), (left_x + define_long, left_y + 2 * define_width), line_color,line_thin)
        cv2.putText(image, num[2], (left_x + int(define_long / 2), left_y + 5 * int(define_width / 2)), font, text_size,text_color, text_thin)
        #
        cv2.line(image, (left_x, left_y + 3 * define_width), (left_x + define_long, left_y + 3 * define_width), line_color,line_thin)
        #
        cv2.line(image, (left_x + define_long, 0), (left_x + define_long, 480), line_color, line_thin)
        cv2.line(image, (left_x, 0), (left_x, 480), line_color, line_thin)
        cv2.line(image, (left_x + 2 * define_long, 0), (left_x + 2 * define_long, 480), line_color, line_thin)
        # B1
        cv2.line(image, (right_x, right_y), (right_x + define_long, right_y), line_color, line_thin)
        cv2.putText(image, num[3], (right_x + int(define_long / 2), right_y + int(define_width / 2)), font, text_size,text_color, text_thin)
        # B3
        cv2.line(image, (right_x, right_y + define_width), (right_x + define_long, right_y + define_width), line_color,line_thin)
        cv2.putText(image, num[4], (right_x + int(define_long / 2), right_y + 3 * int(define_width / 2)), font, text_size,text_color, text_thin)
        # B5
        cv2.line(image, (right_x, right_y + 2 * define_width), (right_x + define_long, right_y + 2 * define_width),line_color, line_thin)
        cv2.putText(image, num[5], (right_x + int(define_long / 2), right_y + 5 * int(define_width / 2)), font, text_size,text_color, text_thin)
        #
        cv2.line(image, (right_x, right_y + 3 * define_width), (right_x + define_long, right_y + 3 * define_width),line_color, line_thin)
        return image

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    sys.exit(app.exec_())

 定义方法def LineAtext1中的内容可工具实际情况删减,此处展示模式较为完整。

以上是关于open cv 常用工具总结1 —— 画线 (python版本)的主要内容,如果未能解决你的问题,请参考以下文章

实现人脸识别性别之路---open CV将图片显示出来

[OPEN CV] 常用视频操作方法

安卓自动化工具程序设计之[识别区域提取] python + uiautomator2 + Open CV

安卓自动化工具程序设计之[识别区域提取] python + uiautomator2 + Open CV

安卓自动化工具程序设计之[识别区域提取] python + uiautomator2 + Open CV

解决from .cv2 import *ImportError: libGL.so.1: cannot open shared object file: No such file or directo