5.6. PikaCV Image Processing Libraries

PikaCV implements some commonly used image processing algorithms.

5.6.1. Install

  1. Add the dependency of PikaCV to requestment.txt.

    1. PikaCV==latest
  2. Run pikaPackage.exe

5.6.2. Import

Add in main.py:

  1. #main.py
  2. import PikaCV as cv

5.6.3. class Image():

The Image class is the basis of the PikaCV,and subsequent image processing algorithms are based on the Image class.By creating an object of the Image class,an empty image can be created.Such as:

  1. import PikaCV
  2. img = cv.Image()

5.6.3.1. Image write and read

PikaCV can read Jpeg format files and write bmp format files.

  1. def read(self, path: str):
  2. """Read the image from the specified path,
  3. Need implement the `__platform_fopen()`, `__platform_fread()`
  4. and `__platform_fclose()`"""
  5. ...
  6. def write(self, path: str):
  7. """Write the image to the specified path,
  8. Need implement the `__platform_fopen()`, `__platform_fwrite()`
  9. and `__platform_fclose()`"""
  10. ...
  11. def loadJpeg(self, bytes: any):
  12. """Load the image from bytes"""
  13. def loadRGB888(self, width: int, height: int, bytes: bytes):
  14. """Load the image from bytes"""
  15. def loadRGB565(self, width: int, hight: int, bytes: bytes):
  16. """Load the image from bytes"""
  17. def loadGray(self, width: int, hight: int, bytes: bytes):
  18. """Load the image from bytes"""

5.6.3.2. Image properties

The size of an image is width * hight * channel

  1. def width(self) -> int:
  2. """Get the width of the image"""
  3. def hight(self) -> int:
  4. """Get the hight of the image"""
  5. def format(self) -> int:
  6. """Get the format of the image.
  7. The format is one of the `ImageFormat` enum,
  8. like `ImageFormat.RGB888`"""
  9. def data(self) -> bytes:
  10. """Get the data of the image"""
  11. def getPixel(self, x: int, y: int, channel: int) -> int:
  12. """Get the pixel value of the specified channel.
  13. For example, if the format of image is `RGB888`,
  14. the channel `0`, `1`, `2`, means `R`, `G`, `B`,
  15. and for the format of `GRAY8`, the channel is `0`
  16. """
  17. def setPixel(self, x: int, y: int, channel: int, value: int):
  18. """Set the pixel value of the specified channel.
  19. For example, if the format of image is `RGB888`,
  20. the channel `0`, `1`, `2`, means `R`, `G`, `B`,
  21. and for the format of `GRAY8`, the channel is `0`
  22. """
  23. def size(self) -> int:
  24. """Get the size of the image by bytes"""

5.6.3.3. Image operations

  1. add() and minus() is pixel-by-pixel operation.When the result of the operation exceeds 255, it is classified as 255, and when it is below 0, it is classified as 0.

  2. The channel order is RGB in merge() and split()

5.6.4. class Converter():

Converter class mainly implements the conversion between image formats, and currently Converter supports the following image storage formats and conversions

From\ToRGB888RGB565GrayBMPBGR888
RGB888-
RGB565-
Gray-*
BMP-
BGR888-

- means no action,* means that an intermediate transformation is required, means that it can be converted directly

An example of an image format conversion operation is as follows:

  1. cv.Converter.toBMP(img)

5.6.5. class Transforms():

The Transforms class mainly implements image transformation algorithms, and the transformation algorithms that have been implemented so far are:

  1. rotateDown(image: Image)

    This function can rotates the image by 180 degrees.

  2. threshold(image:Image,thre:int,maxval:int,thresholdType:int)

    This function is used to convert an image to a binary image.

    thre:When the value of the thresholdType is 0-4, thre is used as the demarcation threshold for the image

    thresholdType: Threshold type, which means as follows:

    _images/thre-en.png

  3. setROI(image:Image,x:int,y:int,w:int,h:int)

    This function is used to select a ROI from an image, the definition of the area is xywh, x and y represent the upper left vertex coordinates of the region, w represents the width of the area, and h represents the height of the area.

  4. getOTSUthre(image:Image) -> int

    This function implements OTSU,For the specific principle, please participate in the paper, the return value of the function is the threshold calculated by the OTSU method.

  5. setOTSU(image:Image)

    This function uses the OTSU algorithm to binaryize the image.

  6. resize(image:Image,x:int,y:int,resizeType:int)

    This function implements the scaling of the image, with x and y being the target size of the image

    resizeType:The scaling method of the image. 0 represents the nearest neighbor algorithm.

  7. adaptiveThreshold(image:Image,maxval:int,subsize:int,c:int,method:int)

    method:An algorithm used to calculate the threshold within a neighborhood. 0 represents mean filtering, 1 represents median filtering.

    c:offset value

    subsize: Convolutional kernel size

5.6.6. class Filter

The Filter class implements some commonly used image filtering algorithms, and the algorithms that have been implemented so far are:

  1. meanFilter(image:Image,ksizex:int,ksizey:int)

    Mean filtering, ksizex and ksizey are the size of x and y of the convolutional kernels, respectively. There is currently no support for pads, so the size of the image after filtering equal W-F+1 when ksizex=ksizey.

  2. medianFilter(image:Image)

    Median filtering, currently only supports convolutional kernels with a size of 3*3.