ZkPhoto

目录
  1. 基本模块
    1. 安装
    2. cv2简介
      1. 读取图像
      2. 显示图片
      3. 鼠标和键盘事件
      4. 图形文字

一个可以从其他文献中描出数据的小软件,具体操作见:
吾爱破解链接

基本模块

安装

需要安装cv2,numpy等库

1
2
3
4
5
6
pip3 install opencv-python
pip3 install numpy

#using method
import cv2
import numpy as np

同时介绍一下其他的一些绘图库的安装

1
2
3
4
5
6
pip3 install pillow
pip3 install matplotlib

#using method
from PIL import Image
import matplotlib.pyplot as plt

同样,安装慢的话建议换源,参见换源。

cv2简介

读取图像

1
2
3
4
5
6
cv2.imread(filepath,flags)
'''flags:
cv2.IMREAD_COLOR:默认参数,读入一副彩色图片,忽略alpha通道
cv2.IMREAD_GRAYSCALE:读入灰度图片
cv2.IMREAD_UNCHANGED:顾名思义,读入完整图片,包括alpha通道
'''

显示图片

1
2
3
4
cv2.imshow('image',img)#first parameter is the window's name while the second is the imput object
cv2.waitKey(0)#waiting for the keyboard input, unit of time is m. if no imput during the specified time, -1 will be returned.
cv2.destoryAllWindow()#destory all windows
cv2.destoryWindow(wname)#destory the specified window

鼠标和键盘事件

  • 鼠标

    鼠标需要设置一个回调函数,把函数与操作的窗口绑定。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    def on_EVENT_LBUTTONDOWN(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
    xy = "%d,%d" % (x, y)
    a.append(x)
    b.append(y)
    cv2.circle(img, (x, y), 1, (0, 0, 255), thickness=-1)
    cv2.putText(img, xy, (x, y), cv2.FONT_HERSHEY_PLAIN,
    1.0, (0, 0, 0), thickness=1)
    cv2.imshow("image", img)
    cv2.namedWindow('image')
    cv2.setMouseCallback('image', on_EVENT_LBUTTONDOWN)

    cv2.EVENT_LBUTTONDOWN 1 左键单击
    cv2.EVENT_RBUTTONDOWN 2 右键单击
    cv2.EVENT_MBUTTONDOWN 3 中键单击
    cv2.EVENT_LBUTTONUP 4 左键释放
    cv2.EVENT_RBUTTONUP 5 右键释放
    cv2.EVENT_MBUTTONUP 6 中键释放
    cv2.EVENT_LBUTTONDBLCLK 7 左键双击
    cv2.EVENT_RBUTTONDBLCLK 8 右键双击
    cv2.EVENT_MBUTTONDBLCLK 9 中键双击

    cv2.EVENT_FLAG_LBUTTON 1 鼠标左键按下状态
    cv2.EVENT_FLAG_RBUTTON 2 鼠标右键按下状态
    cv2.EVENT_FlAG_MBUTTON 4 鼠标中键按下状态
    cv2.EVENT_FLAG_CTRLKEY 8 指示CTRL键按下状态
    cv2.EVENT_FLAG_SHIFTKEY 16 指示SHIFT键按下状态
    cv2.EVENT_FLAG_ALTKEY 32 指示ALT键按下状态

    注意event和flags是并列关系,无法进行逻辑上的组合。

  • 键盘

    首先对waitKey()进行解释

    1
    cv2.waitKey(0) #always display until keyboard pushed. Same as cv2.waitKey()
1
2
3
4
5
6
while key != 27:
cv2.imshow('image', img)
key = cv2.waitKey()
# 如果获取的键值小于256则作为ascii码输出对应字符,否则直接输出值
msg = '{} is pressed'.format(chr(key) if key < 256 else key)
print(msg)

图形文字

  • 文字

    1
    cv2.putText(img,'OpenCv',(80,90),font,size,(r,g,b),thickness=1)
  • 线条

    1
    cv2.line(img,(10,10),(200,200),(r,g,b),3) #pixel = 3
  • circle

    1
    cv2.circle(img,(60.60),30,(r,g,b),1)#point 60.60 radius 30
  • rectangle

    1
    cv2.rectangle(img,(10,10),(30,40),(r,g,b),1)#lefttop and rightbottom point
  • ellipse

    1
    cv2.ellipse(img,(200,200),(100,50),0,0,180,(r,g,b),-1)#center point = 200 and a=100 b=50 0 represent counterclockwise start 0 end 180 degree and -1 represent fill the inside.