5.6. PikaCV 图像库
PikaCV图像库实现了部分常用的图像处理算法。
5.6.1. 安装
- 在 requestment.txt 中加入 PikaCV的依赖。
PikaCV==latest
- 运行 pikaPackage.exe
5.6.2. 导入
在 main.py 中加入:#main.pyimport PikaCV as cv
5.6.3. class Image():
Image类是PikaCV库的基础,后续的图像处理算法都基于Image类。使用Image类可以创建一个空图像,如:import PikaCVimg = cv.Image()
5.6.3.1. 图像读写
目前,PikaCV可以读取Jpeg格式文件与写入bmp格式文件。def read(self, path: str): """Read the image from the specified path, Need implement the `__platform_fopen()`, `__platform_fread()` and `__platform_fclose()`""" ... def write(self, path: str): """Write the image to the specified path, Need implement the `__platform_fopen()`, `__platform_fwrite()` and `__platform_fclose()`""" ... def loadJpeg(self, bytes: any): """Load the image from bytes""" def loadRGB888(self, width: int, height: int, bytes: bytes): """Load the image from bytes""" def loadRGB565(self, width: int, hight: int, bytes: bytes): """Load the image from bytes""" def loadGray(self, width: int, hight: int, bytes: bytes): """Load the image from bytes"""
5.6.3.2. 图像属性
size大小为width * hight * channel。def width(self) -> int: """Get the width of the image""" def hight(self) -> int: """Get the hight of the image""" def format(self) -> int: """Get the format of the image. The format is one of the `ImageFormat` enum, like `ImageFormat.RGB888`""" def data(self) -> bytes: """Get the data of the image""" def getPixel(self, x: int, y: int, channel: int) -> int: """Get the pixel value of the specified channel. For example, if the format of image is `RGB888`, the channel `0`, `1`, `2`, means `R`, `G`, `B`, and for the format of `GRAY8`, the channel is `0` """ def setPixel(self, x: int, y: int, channel: int, value: int): """Set the pixel value of the specified channel. For example, if the format of image is `RGB888`, the channel `0`, `1`, `2`, means `R`, `G`, `B`, and for the format of `GRAY8`, the channel is `0` """ def size(self) -> int: """Get the size of the image by bytes"""
5.6.3.3. 图像运算
add()与minus()逐像素操作,当像素值超过255时归为255,低于0时归为0。merge()与split()的通道顺序均为RGB。5.6.4. class Converter():
Converter类主要实现了图像格式之间的转换,目前Converter支持以下图像存储格式及转换:-代表不执行任何操作,*代表需要经一次中间转换,√代表可以直接转换。 图像格式转换操作示例如下:cv.Converter.toBMP(img)
5.6.5. class Transforms():
Transforms类主要实现了图像变换算法,目前已经实现的变换算法有:rotateDown(image: Image)本函数可将图像旋转180度。threshold(image:Image,thre:int,maxval:int,thresholdType:int)本函数用于将图像转换为二值图像thre:当thresholdType的取值为0-4时使用thre作为图像的分界阈值。thresholdType:阈值类型,具体含义如下:setROI(image:Image,x:int,y:int,w:int,h:int)本函数用于从一张图像出选取一片感兴趣的区域,关于区域的定义采用xywh方法,x与y代表区域的左上顶点坐标,w代表区域的宽度,h代表区域的高度。getOTSUthre(image:Image) -> intOTSU算法,具体原理请参加论文,此处不过多赘述,函数的返回值为OTSU法计算得出的阈值。setOTSU(image:Image)本函数使用OTSU算法对图像进行二值化处理。resize(image:Image,x:int,y:int,resizeType:int)本函数实现了对图像的缩放,x与y是图像的目标大小。resizeType:图像的缩放方法。0代表最近邻算法。adaptiveThreshold(image:Image,maxval:int,subsize:int,c:int,method:int)method:在一个邻域内计算阈值所采用的算法。0代表均值滤波,1代表中值滤波。c:偏移值调整量。subsize:卷积核大小。5.6.6. class Filter
Filter类实现了常用的图像滤波算法,目前已经实现的算法有:meanFilter(image:Image,ksizex:int,ksizey:int)W-F+1(ksizex等于ksizey时)。medianFilter(image:Image)中值滤波,目前仅支持尺寸为3*3的卷积核。
