Pyramid of images

Image pyramid is a kind of multi-scale representation in image, which is mainly used for image segmentation. It is an effective but simple concept structure to interpret image with multi-resolution.

Image pyramid was originally used in machine vision and image compression. The pyramid of an image is a series of images arranged in pyramid shape with progressively reduced resolution and derived from the same original image. It is obtained by cascading down sampling until a certain termination condition is reached.

The bottom of the pyramid is a high resolution representation of the image to be processed, while the top is a low resolution approximation.

We compare the layers of the image to a pyramid, the higher the level, the smaller the image and the lower the resolution.

Gauss pyramid

The bottom layer of the Gaussian pyramid is the original image, and each upper layer is obtained by Gaussian filtering and 1/2 sampling (excluding even rows and columns). Through sampling under constant reduces the size of the image, and then in the pyramid contains multiple images of the scale, in general, the bottom of the gaussian pyramid as the image of the original image, each layer will pass under sampling to narrow the size of an image, usually shrinks to half of their original size, but if you have special requirements, The reduced size can also be adjusted according to actual conditions. Since the size of the image shrinks by half each time, the image ruler shrinks very quickly, so the number of layers of a typical Gauss pyramid is between three and six.

The Gaussian filter can be regarded as a low-pass filter, and only the frequency part below a certain frequency value can be retained in the image after each Gaussian filter. Therefore, the Gaussian pyramid can also be regarded as a low-pass pyramid (only the components below a certain frequency are retained at each level).

Gauss pyramid

Laplace’s pyramid

The Laplace pyramid is the opposite of the Gauss Pyramid, which builds the upper image from the lower image, whereas the Laplace pyramid builds the lower image from the smaller image. Laplacian pyramids have the function of the prediction residual error, need to use the gaussian pyramid is associated with, assuming that we already have a gaussian image pyramid, for the first layer of the image (gaussian pyramid at the bottom of layer 0), first of all a smaller than half of the image is obtained by the sampling, namely the I + 1 layer of gaussian pyramid or not in the gaussian pyramid, Then the image is upsampled to restore the image size to the i-layer image. Finally, the difference between the i-layer image of the Gaussian pyramid and the image obtained after upsampling is obtained, which is the i-layer image of the Laplace pyramid.

Laplacian pyramid generation process

API

The sampling

public static void pyrDown(Mat src, Mat dst, Size dstsize, int borderType) 

Copy the code
  • Parameter 1: SRC, input the image to be sampled.

  • Parameter 2: DST, output the sampled image. The image size can be specified, but the data type and channel number are the same as SRC.

  • Parameter 3: DSTSIZE, output image size, can default.

  • Parameter 4: borderType, the flag of the pixel boundary extrapolation method

    // C++: enum BorderTypes

    public static final int

    BORDER_CONSTANT = 0.

    BORDER_REPLICATE = 1.

    BORDER_REFLECT = 2.

    BORDER_WRAP = 3.

    BORDER_REFLECT_101 = 4.

    BORDER_TRANSPARENT = 5.

    BORDER_REFLECT101 = BORDER_REFLECT_101,

    BORDER_DEFAULT = BORDER_REFLECT_101,

    BORDER_ISOLATED = 16;

    Copy the code

By default, the size of the image output by the function is half of the size of the input image, but the size of the output image can also be set by the parameter DSTSIZE. It should be noted that no matter what the output size is, the conditions in the following formula should be met. This function first convolves the original image with the kernel matrix, as shown below, and then downsamples the image by not using even rows and columns to finally realize the downsampled image with reduced size.



On the sampling

public static void pyrUp(Mat src, Mat dst, Size dstsize, int borderType) 

Copy the code
  • Parameter 1: SRC, input the image to be sampled.
  • Parameter 2: DST, output the upsampled image. The image size can be specified, but the data type and channel number are the same as SRC.
  • Parameter 3: DSTSIZE, output image size, can default.
  • Parameter 4: borderType, the flag of the pixel boundary extrapolation method

operation

class GLPyramidActivity : CardGalleryActivity() {



override fun buildCards(a) {

val bgr = Utils.loadResource(this, R.drawable.lena)

val rgb = Mat()

Imgproc.cvtColor(bgr, rgb, Imgproc.COLOR_BGR2RGB)

bgr.release()

buildGauss(rgb)

rgb.release()

}



private fun buildGauss(source: Mat) {

val gaussList = arrayListOf<Mat>()

gaussList.add(source)

for (i in 0.2) {

val gauss = Mat()

Imgproc.pyrDown(gaussList[i], gauss)

gaussList.add(gauss)

}



for (i in gaussList.indices) {

val bitmap = Bitmap.createBitmap(

gaussList[i].width(),

gaussList[i].height(),

Bitmap.Config.ARGB_8888

)

Utils.matToBitmap(gaussList[i], bitmap)

cards.add(Card("Gauss${i}", bitmap))

}

buildLaplace(gaussList)

}



private fun buildLaplace(gaussList: List<Mat>) {

val laplaceList = arrayListOf<Mat>()

for (i in gaussList.size - 1 downTo 1) {

val lap = Mat()

val upGauss = Mat()

if (i == gaussList.size - 1) {

val down = Mat()

Imgproc.pyrDown(gaussList[i], down)

Imgproc.pyrUp(down, upGauss)

Core.subtract(gaussList[i], upGauss, lap)

laplaceList.add(lap.clone())

}

Imgproc.pyrUp(gaussList[i], upGauss)

Core.subtract(gaussList[i - 1], upGauss, lap)

laplaceList.add(lap.clone())

}



for (i in laplaceList.indices) {

val bitmap = Bitmap.createBitmap(

laplaceList[i].width(),

laplaceList[i].height(),

Bitmap.Config.ARGB_8888

)

Utils.matToBitmap(laplaceList[i], bitmap)

cards.add(Card("Laplace${i}", bitmap))

}



for (gauss in gaussList) {

gauss.release()

}

for (lap in laplaceList) {

lap.release()

}

}

}

Copy the code

The effect

The Gauss pyramid and the Laplace pyramid

This Laplace up here may be hard to see, but if you look closely you can see it. Let the image width full look.

To see the width full

To see the image of the Laplacian Pyramid, the image size itself is not

The source code

Github.com/onlyloveyd/…


This article is formatted using MDNICE