opencv python 使用记录(持续更新)

1 函数使用

        1.1 绘制矩形、圆形

        函数调用如下所示,cv2.rectangle的参数依次是图像、左上角坐标、右上角坐标、颜色、线条粗细;cv2.circle的参数依次是图像、中心坐标、半径、颜色、线条粗细。其中线条粗细设置为-1时,则填充满整个图形。

image = cv2.rectangle(img,(f_xmin,f_ymin),(f_xmax,f_ymax),(0,0,255),3)
image = cv2.circle(image, center_coordinates, radius, color, thickness) 

        1.2 图片转视频

import os
import cv2

images = os.listdir('images/')
print(images)
img = cv2.imread('images/'+images[0])
print(img.shape)
# fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')#.avi格式
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')#.mp4格式
videowrite = cv2.VideoWriter('test.mp4',fourcc,10,(640,480))


for i in images:
    imgname = 'images/'+i
    img = cv2.imread(imgname)
    img = cv2.resize(img, (640,480))
    # print(img.shape)
    videowrite.write(img)
    print(imgname)
videowrite.release()
print('end!')

        1.3 读取含中文路径图片

def cv_imread(filePath):
    cv_img=cv2.imdecode(np.fromfile(filePath,dtype=np.uint8),-1)
    cv_img = cv2.cvtColor(cv_img, cv2.COLOR_RGB2BGR)
    return cv_img

        1.4 保存含中文路径图片

cv2.imencode('.png', img_array)[1].tofile(save_path)

        1.5 1维转3维

#将一维灰度图像扩展到三维
image= np.expand_dims(imagel,axis=2).repeat(3,axis=2).astype(np.uint8)

        1.6 灰度图转热力图

heat_image = cv2.applyColorMap(depth_image, cv2.COLORMAP_JET)

2 Debug记录

        2.1 cv2.imshow报错

        报错内容为:window.cpp:1274: error: (-2:Unspecified error) The function is not implemented.

Traceback (most recent call last):
File “/home/data/PJS/test_bed/img_show.py”, line 18, in
cv2.imshow(‘img’, img)
cv2.error: OpenCV(4.5.3) /tmp/pip-req-build-9gwtlx3c/opencv/modules/highgui/src/window.cpp:1274: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function ‘cvShowImage’
解决方法:

import cv2
print(cv2.__file__)
#删除打印出来的cv2文件夹即可
#如果不行,卸载并重新安装opencv-python就可以了

更多三维、二维感知算法和金融量化分析算法请关注“乐乐感知学堂”微信公众号,并将持续进行更新。

你可能感兴趣的:(python,opencv,python,opencv-python)