OPENCV (c + +) implements second-order linear interpolation

Source: Internet
Author: User

Tag:1.0    Technology    type   har   cout    scala   opencv    around    clu   

#include <opencv2\opencv.hpp> #include <iostream>using namespace cv;using namespace Std;bool enlargedimage (Mat &src, float k1, float K2);//k1,k2 represents a multiple magnification of void main () {Mat srcimage = Imread ("flower.png"); float K1 = 1.2, K2 = 2.5 ; Enlargedimage (Srcimage, K1, K2);} BOOL Enlargedimage (Mat &src, float k1, float k2) {int height, width, theight, Twidth;int IA, ja;//new coordinates height = Src.row The high width of the s;//image = The width of the src.cols;//image theight = round (HEIGHT*K1);//enlarged Image high cout << theight << endl;twidth = Round ( WIDTH*K2);//enlarged image width cout << twidth << Endl; Mat dstimage (Theight, Twidth, Src.type (), Scalar (0));//fills the resulting new picture with a for (int i = 0; i < height; i++) {for (int j = 0; J &L T Width-1; J + +) {ia = round (I*K1); ja = round (J*K2);//If it is located in Four Corners if (ia = = 0 && ja = 0)//left Apex {dstimage.at<vec3b> (ia, JA) [0] = Src.at<vec3b> (i, J) [0];d stimage.at<vec3b> (ia, JA) [1] = Src.at<vec3b> (i, J) [1];d stimage.at< Vec3b> (ia, JA) [2] = Src.at<vec3b> (i, J) [2];} else if (iA = = theight-1 && ja = = twidth-1)//lower left corner {dstimage.at<vec3b> (ia, JA) [0] = src.at<vec3b> (i, J) [0];d STI Mage.at<vec3b> (ia, JA) [1] = Src.at<vec3b> (i, J) [1];d stimage.at<vec3b> (ia, JA) [2] = src.at<vec3b > (i, J) [2];} else if (ia = = 0 && ja = = twidth-1)//Right Apex {dstimage.at<vec3b> (ia, JA) [0] = src.at<vec3b> (i, J) [0];d STI Mage.at<vec3b> (ia, JA) [1] = Src.at<vec3b> (i, J) [1];d stimage.at<vec3b> (ia, JA) [2] = src.at<vec3b > (i, J) [2];} else if (ia = = theight-1 && ja = = twidth-1)//Lower right corner {dstimage.at<vec3b> (ia, JA) [0] = src.at<vec3b> (i, j) [0];d stimage.at<vec3b> (ia, JA) [1] = Src.at<vec3b> (i, J) [1];d stimage.at<vec3b> (ia, JA) [2] = src.at <Vec3b> (i, J) [2];} else if (ja = = twidth-1)//third case, rightmost {dstimage.at<vec3b> (round ((i-1) *k1) + 1, ja) [0] = src.at<vec3b> (i, J) [0 ];d stimage.at<vec3b> (Round ((i-1) *k1) + 1, ja) [1] = Src.at<vec3b> (i, J) [1];d stimage.at<vec3b> (Round ((i-1) *k1) + 1, ja) [2] = Src.at<vec3b> (i, J) [2];} else if (ia = = 0)//first case, topmost {dstimage.at<vec3b> (IA, round ((j-1) *k2) + 1) [0] = src.at<vec3b> (i, J) [0];d Stimag E.at<vec3b> (IA, round ((j-1) *k2) + 1) [1] = Src.at<vec3b> (i, J) [1];d stimage.at<vec3b> (IA, round ((j-1) *K2) + 1) [2] = Src.at<vec3b> (i, J) [2];} else if (ja = = 0)//second case, leftmost {dstimage.at<vec3b> (round ((i-1) *k1) + 1, ja) [0] = src.at<vec3b> (i, J) [0];d Stimag E.at<vec3b> (Round ((i-1) *k1) + 1, ja) [1] = Src.at<vec3b> (i, J) [1];d stimage.at<vec3b> (Round ((i-1) *k1 ) + 1, ja) [2] = Src.at<vec3b> (i, J) [2];} else if (ia = = theight-1)//Fourth case, Bottom {dstimage.at<vec3b> (IA, round ((j-1) *k2) + 1) [0] = src.at<vec3b> (i, J) [ 0];d stimage.at<vec3b> (IA, round ((j-1) *k2) + 1) [1] = Src.at<vec3b> (i, J) [1];d stimage.at<vec3b> (IA, Round ((j-1) *k2) + 1) [2] = Src.at<vec3b> (i, J) [2];} The last case, located in the middle, assigns the value to the upper-left corner of the value else{dstimage.at<vec3b> (round ((i-1) *K1) + 1, round ((j-1) *k2) + 1) [0] = src.at<vec3b> (i, J) [0];d stimage.at<vec3b> (Round ((i-1) *k1) + 1,  Round ((j-1) *k2) + 1) [1] = Src.at<vec3b> (i, J) [1];d stimage.at<vec3b> (Round ((i-1) *k1) + 1, round ((j-1) *K2) + 1) [2] = Src.at<vec3b> (i, J) [2];}}} for (int i = 0; i < height; i++) {//consider the rightmost int j = Width-1;ia = Round (I*K1); ja = round (j*k2);d stimage.at<vec3b> ( Round ((i-1) *k1) + 1, ja + 1) [0] = src.at<vec3b> (i, J) [0];d stimage.at<vec3b> (Round ((i-1) *k1) + 1, ja + 1) [1 ] = Src.at<vec3b> (i, J) [1];d stimage.at<vec3b> (round (i-1) *K1) + 1, ja + 1) [2] = Src.at<vec3b> (i, J) [2] ;} int A1, A2, B1, b2;//is used to denote a single-line interpolation in the upper and lower left and right of the coordinates are not 0//the use of single-threaded interpolation for (int i = 0; i < theight; i++) {for (int j = 0; J < twidth ; J + +) {//The first consideration of the IF (dstimage.at<vec3b> (i, 0) [0] = = 0 && dstimage.at<vec3b> (i, 0) [1] = = 0 && Amp Dstimage.at<vec3b> (i, 0) [2] = = 0) {continue;} if (Dstimage.at<vec3b> (i, J) [0] = = 0 && dstimage.at<vec3b> (i, J) [1] = = 0 && dstimage.at<vec3b> (i, J) [2] = = 0&& j>0 &&am P  J < twidth) {B1 = J-1;b2 = j + 1;while (dstimage.at<vec3b> (i, B1) [0] = = 0 && dstimage.at<vec3b> (i, B1) [1] = = 0 && dstimage.at<vec3b> (i, B1) [2] = = 0 && B1 >= 0) {b1--;} while (Dstimage.at<vec3b> (i, B2) [0] = = 0 && dstimage.at<vec3b> (i, B2) [1] = = 0 && dstimage.at& Lt Vec3b> (i, B2) [2] = = 0 && b2 <twidth) {b2++;} int sfrg0 = Floor (((J-B1) *1.0/(B2-B1)) *dstimage.at<vec3b> (i, B2) [0] + ((b2-j) *1.0/(B2-B1)) *DSTIMAGE.AT&L T  Vec3b> (I, B1) [0]); int sfrg1 = Floor (((J-B1) *1.0/(B2-B1)) *dstimage.at<vec3b> (i, B2) [1] + ((b2-j) *1.0/(B2 -B1)) *dstimage.at<vec3b> (I, B1) [1]); int sfrg2 = Floor (((J-B1) *1.0/(B2-B1)) *dstimage.at<vec3b> (i, B2) [2 ] + ((b2-j) *1.0/(B2-B1)) *dstimage.at<vec3b> (I, B1) [2]);d stimage.at<vec3b> (i, J) [0] = Saturate_Cast<uchar> (sfrg0);d stimage.at<vec3b> (i, J) [1] = saturate_cast<uchar> (sfrg1);d stimage.at< Vec3b> (i, J) [2] = saturate_cast<uchar> (SFRG2);}} Interpolate columns with single-threaded for (int j = 0; J < Twidth; J + +) {for (int i = 0; i < theight; i++) {if (Dstimage.at<vec3b> (i, J) [0 ] = = 0 && dstimage.at<vec3b> (i, J) [1] = = 0 && dstimage.at<vec3b> (i, J) [2] = = 0 && i&gt ; 0 && i < theight) {a1 = I-1;a2 = i + 1;while (dstimage.at<vec3b> (A1, J) [0] = = 0 && dstimage.at& Lt Vec3b> (A1, J) [1] = = 0 && dstimage.at<vec3b> (A1, J) [2] = = 0 && A1 >= 0) {a1--;} while (Dstimage.at<vec3b> (A2, J) [0] = = 0 && dstimage.at<vec3b> (A2, J) [1] = = 0 && dstimage.at& Lt Vec3b> (A2, J) [2] = = 0 && A2 < twidth) {a2++;} int s0 = Floor (((I-A1) *1.0/(A2-A1)) *dstimage.at<vec3b> (A2, J) [0] + ((a2-i) *1.0/(A2-A1)) *dstimage.at<v Ec3b> (A1, J) [0]); int S1 = Floor ((I-A1) *1.0/(A2 -A1)) *dstimage.at<vec3b> (A2, J) [1] + ((a2-i) *1.0/(A2-A1)) *dstimage.at<vec3b> (A1, J) [1]); int s2 = Floor (((I-A1) *1.0/(A2-A1)) *dstimage.at<vec3b> (A2, J) [2] + ((a2-i) *1.0/(A2-A1)) *dstimage.at<vec3b> (A1, J ) [2]);d stimage.at<vec3b> (i, J) [0] = saturate_cast<uchar> (S0);d stimage.at<vec3b> (i, J) [1] = Saturate_cast<uchar> (S1);d stimage.at<vec3b> (i, J) [2] = saturate_cast<uchar> (s2);}} Imshow ("original", SRC);//namedwindow ("Enlarged image", Cv_window_normal) imshow ("enlarged image", dstimage); Waitkey (0); return true;}

OPENCV (c + +) implements second-order linear interpolation

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.