Jsvascript Image Processing-(Computer Vision application) image pyramid _ javascript tips-js tutorial

Source: Internet
Author: User
In the previous article, we explained the edge gradient computing function. This article describes the image pyramid. Image pyramid is widely used in computer vision applications. Image pyramid is an image set, all the images in the set are derived from the same original image and obtained through continuous downsampling of the original image. Preface
In the previous article, we explained the edge gradient computing function. This article is about image pyramid.

Image pyramid?
Image pyramid is widely used in computer vision applications.
An image pyramid is a collection of images. All the images in the set are derived from the same original image and obtained through continuous downsampling of the original image.

There are two common image pyramid types::
• Gaussian pyramid: used for downward sampling
• Laplacian pyramid: used to reconstruct the unsampled upper layer image from the lower layer of the pyramid.

Gaussian pyramid


Like a pyramid, the Gaussian pyramid gradually samples downward from the underlying original graph and becomes smaller and smaller.

So how can we get the next image?

First, convolution with Gaussian Kernel:

Delete all even columns.
It can be seen that the next level of image is about 1/4 of the next level.

So how can I change the value of the upward conversion?
First, expand the row and column of the image to double the original one, and then fill the added row and column with 0.
Then, multiply the Gaussian Kernel by 4 and perform convolution.

Gaussian pyramid implementation

The Code is as follows:


Var pyrDown = function (_ src, _ dst ){
_ Src | error (arguments. callee, IS_UNDEFINED_OR_NULL/* {line }*/);
If (_ src. type & _ src. type = "CV_RGBA "){
Var width = _ src. col,
Height = _ src. row,
DWidth = (width & 1) + width)/2,
DHeight = (height & 1) + height)/2,
SData = _ src. data,
Dst = _ dst | new Mat (dHeight, dWidth, CV_RGBA ),
DstData = dst. data;
Var withBorderMat = copyMakeBorder (_ src, 2, 2, 0, 0 ),
MData = withBorderMat. data,
MWidth = withBorderMat. col;
Var newValue, nowX, offsetY, offsetI, dOffsetI, I, j;
Var kernel = [1, 4, 6, 4, 1,
, 16, 24, 16, 4,
, 24, 36, 24, 6,
, 16, 24, 16, 4,
, 4, 6, 4, 1
];
For (I = dHeight; I --;){
DOffsetI = I * dWidth;
For (j = dWidth; j --;){
For (c = 3; c --;){
NewValue = 0;
For (y = 5; y --;){
OffsetY = (y + I * 2) * mWidth * 4;
For (x = 5; x --;){
NowX = (x + j * 2) * 4 + c;
NewValue + = (mData [offsetY + nowX] * kernel [y * 5 + x]);
}
}
DstData [(j + dOffsetI) * 4 + c] = newValue/256;
}
DstData [(j + dOffsetI) * 4 + 3] = mData [offsetY + 2 * mWidth * 4 + (j * 2 + 2) * 4 + 3];
}
}
} Else {
Error (arguments. callee, UNSPPORT_DATA_TYPE/* {line }*/);
}
Return dst;
};


DWidth = (width & 1) + width)/2,
DHeight = (height & 1) + height)/2
Here, a & 1 is equivalent to a % 2, that is, the remainder divided by 2.
We didn't follow the above steps to achieve this, because the efficiency would be low. Instead, we directly created a matrix of 1/4 of the original matrix and skipped the rows and columns to be deleted during convolution.

The same is true for the following. After the convolution is created, some elements in the kernel are ignored in the actual convolution process.

The Code is as follows:


Var pyrUp = function (_ src, _ dst ){
_ Src | error (arguments. callee, IS_UNDEFINED_OR_NULL/* {line }*/);
If (_ src. type & _ src. type = "CV_RGBA "){
Var width = _ src. col,
Height = _ src. row,
DWidth = width * 2,
DHeight = height * 2,
SData = _ src. data,
Dst = _ dst | new Mat (dHeight, dWidth, CV_RGBA ),
DstData = dst. data;
Var withBorderMat = copyMakeBorder (_ src, 2, 2, 0, 0 ),
MData = withBorderMat. data,
MWidth = withBorderMat. col;
Var newValue, nowX, offsetY, offsetI, dOffsetI, I, j;
Var kernel = [1, 4, 6, 4, 1,
, 16, 24, 16, 4,
, 24, 36, 24, 6,
, 16, 24, 16, 4,
, 4, 6, 4, 1
];
For (I = dHeight; I --;){
DOffsetI = I * dWidth;
For (j = dWidth; j --;){
For (c = 3; c --;){
NewValue = 0;
For (y = 2 + (I & 1); y --;){
OffsetY = (y + (I + 1)> 1) * mWidth * 4;
For (x = 2 + (j & 1); x --;){
NowX = (x + (j + 1)> 1) * 4 + c;
NewValue + = (mData [offsetY + nowX] * kernel [(y * 2 + (I & 1 ^ 1 )) * 5 + (x * 2 + (j & 1 ^ 1)]);
}
}
DstData [(j + dOffsetI) * 4 + c] = newValue/64;
}
DstData [(j + dOffsetI) * 4 + 3] = mData [offsetY + 2 * mWidth * 4 + (j + 1)> 1) + 2) * 4 + 3];
}
}
} Else {
Error (arguments. callee, UNSPPORT_DATA_TYPE/* {line }*/);
}
Return dst;
};



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.