Opencv: linear interpolation for image enlargement

Source: Internet
Author: User

The interpolation method can be used for image enlargement. One simple interpolation method is linear interpolation, which is very effective even though simple.

Linear interpolation
The so-called linear interpolation means that there is a set of discrete data {A (1), a (2 ),..., A (n)}, we want to know the number A (M) (k <m <k + 1) between a (K) and a (k + 1. However, because the data is discrete, we only know a (K) and a (k + 1). Therefore, we can only make an estimation of A (m, the estimated value is called interpolation. A reasonable estimation is that the value of a point between a (K) and a (k + 1) may be a (k) + (A (k + 1) -A (k) * m, M is a constant decimal place between [0, 1], which is called linear interpolation. For example


Linear interpolation and amplification of images
An image is a pixel matrix that can be expressed as P (I, j ). P (I, j) is used to represent a point in the original image, and PS (I, j) is used to represent a point in the enlarged image. The amplification process is as follows:
First, calculate the location where interpolation is required.
The PS (x, y) value of a new image is
PS (x, y) = P (m, n)
M = Original Image Height x (x-ray New Image Height)
N = Original Image Width x (New Image Width of Y Branch)
M and n are usually decimal places, that is, the locations where the original image needs to be interpolated.
After knowing the interpolation position, we need to perform Interpolation on the original image, as shown in figure

Where p (I, n), P (M, J), P (I + 1, n), P (M, J + 1), P (m, n) it is inserted by linear interpolation,
P (I, n) = P (I, j) + (P (I, j + 1)-P (I, j) × (n-j)
P (M, j) = P (I, j) + (P (I + 1, J)-P (I, j) × (M-I)
P (I + 1, n) = P (I + 1, J) + (P (I + 1, J + 1)-P (I + 1, J )) × (n-j)
P (M, J + 1) = P (I, j + 1) + (P (I + 1, J + 1)-P (I, j + 1 )) × (M-I)
P (m, n) = P (I, n) + (P (I + 1, n)-P (I, n) × (M-I)
I = floor (m)
J = floor (N)
Floor is rounded down
In this way, the PS (x, y) values of each point in the image are all ready, and the linear interpolation and amplification of the image are completed by writing it into the new image.

VC implementation
We use opencv (beta_5) library to read the image, and then manually interpolation and amplification. The program is an MFC/doc/view structure.

...

First, access the opencv (beta_5) Library

# Include "highgui. H" // includes

# Pragma comment (Lib, "highgui. lib") // link to. Lib

# Pragma comment (Lib, "cxcore. lib") // link to. Lib

...

Class clinerdoc: Public cdocument

{

...

// Attributes

Public:

Iplimage * IMG; // Original Image

Iplimage * img_s; // scaled Image

...

};

Clinerdoc: clinerdoc ()

: IMG (null)

, Img_s (null)

{

...

}

Bool clinerdoc: onnewdocument ()

{

...

Int I, J;

Double xs = 3.0, ys = 3.0;

// Load the original image

IMG = cvloadimage ("C: // Documents and Settings // administrator // desktop // desktop_girl_v1.jpg ");

// Apply memory for scaled Image

Img_s = cvcreateimage (cvsize (INT) IMG-> width * XS, (INT) IMG-> height * ys), IMG-> depth, IMG-> nchannels );

// Scale by linear interpolation

Double XM, ym;

Unsigned char vff0, vff1, vff2, vcf0, vcf1, vcf2, vfc0, vfc1, vfc2, vcc0, vcc0, vcc2;

Unsigned char v0, V1, V2;

For (I = 0; I height; I ++ ){

For (j = 0; j width; j ++ ){

// Middle Pixel

XM = (IMG-> width-1) * j/(double) img_s-> width;

Ym = (IMG-> height-1) * I/(double) img_s-> height;

// 4 neighbor pixels of 3 color vff0 = IMG-> imagedata [(INT) floor (ym) * IMG-> widthstep + (INT) floor (XM )) * IMG-> nchannels + 0]; vff1 = IMG-> imagedata [(INT) floor (ym) * IMG-> widthstep + (INT) floor (XM )) * IMG-> nchannels + 1];

Vff2 = IMG-> imagedata [(INT) floor (ym) * IMG-> widthstep + (INT) floor (XM) * IMG-> nchannels + 2];

Vcf0 = IMG-> imagedata [(INT) floor (ym) * IMG-> widthstep + (INT) Ceil (XM) * IMG-> nchannels + 0];

Vcf1 = IMG-> imagedata [(INT) floor (ym) * IMG-> widthstep + (INT) Ceil (XM) * IMG-> nchannels + 1];

Vcf2 = IMG-> imagedata [(INT) floor (ym) * IMG-> widthstep + (INT) Ceil (XM) * IMG-> nchannels + 2];

Fcv0 = IMG-> imagedata [(INT) Ceil (ym) * IMG-> widthstep + (INT) floor (XM) * IMG-> nchannels + 0];

Vfc1 = IMG-> imagedata [(INT) Ceil (ym) * IMG-> widthstep + (INT) floor (XM) * IMG-> nchannels + 1];

Vfc2 = IMG-> imagedata [(INT) Ceil (ym) * IMG-> widthstep + (INT) floor (XM) * IMG-> nchannels + 2];

Vcc0 = IMG-> imagedata [(INT) Ceil (ym) * IMG-> widthstep + (INT) Ceil (XM) * IMG-> nchannels + 0];

Vpc3 = IMG-> imagedata [(INT) Ceil (ym) * IMG-> widthstep + (INT) Ceil (XM) * IMG-> nchannels + 1];

Vcc2 = IMG-> imagedata [(INT) Ceil (ym) * IMG-> widthstep + (INT) Ceil (XM) * IMG-> nchannels + 2];

// Perform linear interpolation

V0 = vff0 + (vcf0-vff0) * (XM-(INT) XM)

+ (Vfc0 + (vcc0-vfc0) * (XM-(INT) XM)-vff0-(vcf0-vff0) * (XM-(INT) XM) * (ym-(INT) ym );

V1 = vff1 + (vcf1-vff1) * (XM-(INT) XM)

+ (Vfc1 + (vcc1-vfc1) * (XM-(INT) XM)-vff1-(vcf1-vff1) * (XM-(INT) XM) * (ym-(INT) ym );

V2 = vff2 + (vcf2-vff2) * (XM-(INT) XM)

+ (Vfc2 + (vcc2-vfc2) * (XM-(INT) XM)-vff2-(vcf2-vff2) * (XM-(INT) XM) * (ym-(INT) ym );

// Set pixel of scaled Image

Img_s-> imagedata [I * img_s-> widthstep + J * img_s-> nchannels + 0] = V0;

Img_s-> imagedata [I * img_s-> widthstep + J * img_s-> nchannels + 1] = V1;

Img_s-> imagedata [I * img_s-> widthstep + J * img_s-> nchannels + 2] = V2;

}

}

Return true;

}

Void clinerdoc: onclosedocument ()

{

// Release created image

Cvreleaseimage (& img_s );

...

}

Void clinerview: ondraw (CDC * PDC)

{

Clinerdoc * pdoc = getdocument ();

Assert_valid (pdoc );

If (! Pdoc)

Return;

Int I, J;

Clinerdoc * Doc = (clinerdoc *) This-> getdocument ();

Iplimage * IMG = doc-> IMG; // Original Image

Iplimage * img_s = doc-> img_s; // scaled Image

Int xori = 10, Yori = 10;

Colorref C;

If (IMG ){

// Show Original Image

For (I = 0; I height; I ++ ){

For (j = 0; j width; j ++ ){

C = RGB (IMG-> imagedata [I * IMG-> widthstep + J * IMG-> nchannels + 2],

IMG-> imagedata [I * IMG-> widthstep + J * IMG-> nchannels + 1],

IMG-> imagedata [I * IMG-> widthstep + J * IMG-> nchannels + 0]);

PDC-> setpixel (J + Yori, I + xori, C );

}

}

}

If (img_s ){

// Show scaled Image

For (I = 0; I height; I ++ ){

For (j = 0; j width; j ++ ){

C = RGB (img_s-> imagedata [I * img_s-> widthstep + J * img_s-> nchannels + 2],

Img_s-> imagedata [I * img_s-> widthstep + J * img_s-> nchannels + 1],

Img_s-> imagedata [I * img_s-> widthstep + J * img_s-> nchannels + 0]);

PDC-> setpixel (J + Yori * 2 + IMG-> width, I + xori, C );

}

}

}

}

For more information, see

The left side is the original image, and the right side is magnified by 3 times by linear interpolation. It looks like a pattern.

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.