简介:opencv-python不支持用cv.imread和cv.imwrite处理带中文路径的图片,但是可以采用从内存编解码的方式获取图像,在此造个轮子方便以后使用
1. opencv函数原型
- 读取带中文路径的图片
imdecode(buf, flags) -> retval.
. @brief Reads an image from a buffer in memory.
. . The function imdecode reads an image from the specified buffer in the memory. If the buffer is too short or . contains invalid data, the function returns an empty matrix ( Mat::data==NULL ).
. . See cv::imread for the list of supported formats and flags description.
. . @note In the case of color images, the decoded images will have the channels stored in B G R order.
. @param buf Input array or vector of bytes.
. @param flags The same flags as in cv::imread, see cv::ImreadModes.
Type: builtin_function_or_method
- 保存带中文路径的图片
- imencode(ext, img[, params]) -> retval, buf.
. @brief Encodes an image into a memory buffer.
. . The function imencode compresses the image and stores it in the memory buffer that is resized to fit the . result. See cv::imwrite for the list of supported formats and flags description.
. . @param ext File extension that defines the output format.
. @param img Image to be written.
. @param buf Output buffer resized to fit the compressed image.
. @param params Format-specific parameters. See cv::imwrite and cv::ImwriteFlags.
Type: builtin_function_or_method
2. 实例:
import cv2 as cv
import numpy as np
path = r"H:\\需要生成fov定位点的图\\fov\\LUQIAN\\1\\0-28.bmp"
img = cv.imdecode(np.fromfile(path, dtype=np.uint8),1)
cv.namedWindow("img",0)
cv.imshow("img",img)
cv.waitKey(0)
cv.destroyWindow("img")
outpath = u"./图片1.jpg"
cv.imencode('.jpg',img)[1].tofile(outpath) #".jpg"是编码方式,可以改成“.bmp” ".png".....
网友评论