查表相位方式插值

代码存在问题:

// phaseinter.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/nonfree/features2d.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/legacy/legacy.hpp"
#include<math.h>
#include <string>
using namespace cv;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int M = 0;  
    int N = 0; 
    Mat img = imread("test.png",0);//灰度图
    if(img.empty())
    {
        cout << "图像加载失败!"
            << endl;
        //system("pause");
        return
            -1;
    };
    M=img.rows;//行数、图像高度
    N=img.cols;//列数、图像宽度
//    std::cout<<M<<"行"<< std::endl; 
//    std::cout<<N<<"列"<< std::endl;
    //利用64相位4*4的模板
    cv::Mat F= (cv::Mat_<int>(64,4) << 
         0,32,0, 0,
         0,32,1,-1,
         0,32,1,-1,
        -1,32,1, 0,
        -1,32,2,-1,
        //6-10
        -1,32,2,-1,
        -2,32,3,-1,
        -2,32,3,-1,
        -3,32,4,-1,
        -3,32,4,-1,
        //11-15
        -3,32,4,-1,
        -3,32,5,-2,
        -3,31,6,-2,
        -3,30,7,-2,
        -3,30,7,-2,
        //16-20
        -4 ,30 , 8 ,-2,
        -4 ,29 , 9 ,-2,
        -4 ,29 , 9 ,-2,
        -4 ,28 ,10 ,-2,
        -4 ,27 ,11 ,-2,
        //21-25
        -4 ,27 ,11 ,-2,
        -4 ,27 ,11 ,-2,
        -4 ,26 ,13 ,-3,
        -4 ,26 ,13 ,-3,
        -4 ,25 ,14 ,-3,
        //26-30
        -4 ,24 ,15 ,-3,
        -4 ,24 ,15 ,-3,
        -4 ,24 ,15 ,-3,
        -4 ,22 ,17 ,-3,
        -4 ,21 ,18 ,-3,
        //31-35
        -4 ,21 ,18 ,-3,
        -4 ,21 ,19 ,-4,
        -8 ,24 ,24 ,-8,
        -4 ,19 ,21 ,-4,
        -3 ,18 ,21 ,-4,
        //36-40
        -3 ,18 ,21 ,-4,
        -3 ,17 ,22 ,-4,
        -3 ,15 ,24 ,-4,
        -3 ,15 ,24 ,-4,
        -3 ,15 ,24 ,-4,
        //41-45
        -3 ,14 ,25 ,-4,
        -3 ,13 ,26 ,-4,
        -3 ,13 ,26 ,-4,
        -2 ,11 ,27 ,-4,
        -2 ,11 ,27 ,-4,
        //46-50
        -2 ,11 ,27 ,-4,
        -2 ,10 ,28 ,-4,
        -2 , 9 ,29 ,-4,
        -2 , 9 ,29 ,-4,
        -2 , 8 ,30 ,-4,
        //51-55
        -2 ,7 ,30 ,-3,
        -2 ,7 ,30 ,-3,
        -2 ,6 ,31 ,-3,
        -2 ,5 ,32 ,-3,
        -1 ,4 ,32 ,-3,
        //56-60
        -1 ,4 ,32 ,-3,
        -1 ,4 ,32 ,-3,
        -1 ,3 ,32 ,-2,
        -1 ,3 ,32 ,-2,
        -1 ,2 ,32 ,-1,
        //61-64
        -1 ,2 ,32 ,-1,
         0 ,1 ,32 ,-1,
        -1 ,1 ,32 , 0,
        -1 ,1 ,32 , 0 );
    //Mat img = imread("test.png", CV_LOAD_IMAGE_UNCHANGED);
    //创建一个名字为MyWindow的窗口来显示图片
//    namedWindow("MyWindow", CV_WINDOW_AUTOSIZE);
//    imshow("MyWindow",img);
//    waitKey(0);
//    destroyWindow("MyWindow");
    int col=0;//列数、图像宽度;对应N
    int row=0;
//    Mat out;//放大高后的图
    Mat out(2*M,N,CV_8UC1,Scalar(0));

//    Mat scaler;//整体放大后的图
Mat scaler(2*M,2*N,CV_8UC1,Scalar(255));

    std::cout<<out<< std::endl; 
    std::cout<<scaler<< std::endl; 
    getchar();
//    Mat img = imread("test.png", CV_LOAD_IMAGE_UNCHANGED);
    //创建一个名字为MyWindow的窗口来显示图片
    for(col=0;col<N;col++)
    {
        for(row=0;row<2*M;row++)
        {
            double tmp=(row-1)/M+1/(2*64);
            int i=floor(tmp);
            int phaseNum=floor((tmp-i)*64)+1;
            if(i==0)
            {
                out.at<uchar>(row,col)=
                    (float)img.at<uchar>(1,col)*F.at<uchar>(phaseNum,1)+
                    (float)img.at<uchar>(1,col)*F.at<uchar>(phaseNum,2)+
                    (float)img.at<uchar>(2,col)*F.at<uchar>(phaseNum,3)+
                    (float)img.at<uchar>(3,col)*F.at<uchar>(phaseNum,4);
            }
            else if(i+2==M)
            {       
                out.at<uchar>(row,col)=
                    (float)img.at<uchar>(i,col)*F.at<uchar>(phaseNum,1)+
                    (float)img.at<uchar>(i+1,col)*F.at<uchar>(phaseNum,2)+
                    (float)img.at<uchar>(i+2,col)*F.at<uchar>(phaseNum,3)+
                    (float)img.at<uchar>(i+2,col)*F.at<uchar>(phaseNum,4);
            }
            else if(i+1==M||i==M)
            {        
                out.at<uchar>(row,col)=
                    (float)img.at<uchar>(i,col)*F.at<uchar>(phaseNum,1)+
                    (float)img.at<uchar>(i+1,col)*F.at<uchar>(phaseNum,2)+
                    (float)img.at<uchar>(i+1,col)*F.at<uchar>(phaseNum,3)+
                    (float)img.at<uchar>(i+1,col)*F.at<uchar>(phaseNum,4);
            }
            else
            {    out.at<uchar>(row,col)=
                    (float)img.at<uchar>(i,col)*F.at<uchar>(phaseNum,1)+
                    (float)img.at<uchar>(i+1,col)*F.at<uchar>(phaseNum,2)+
                    (float)img.at<uchar>(i+2,col)*F.at<uchar>(phaseNum,3)+
                    (float)img.at<uchar>(i+3,col)*F.at<uchar>(phaseNum,4);
            }
        }
    }
    namedWindow("MyWindow",CV_WINDOW_AUTOSIZE);
    imshow("MyWindow",out);
    waitKey(0);
    destroyWindow("MyWindow");
    for(row=0;row<2*M;row++)
    {
        for(col=0;col<2*N;col++)
        {
            double tmp=(col-1)/N+1/(2*64); 
            int i=floor(tmp);
            int phaseNum=floor((tmp-i)*64)+1;
            if(i==0)
            {        
                scaler.at<uchar>(row,col)=
                    out.at<uchar>(row,1)*F.at<uchar>(phaseNum,1)+
                    out.at<uchar>(row,1)*F.at<uchar>(phaseNum,2)+
                    out.at<uchar>(row,2)*F.at<uchar>(phaseNum,3)+
                    out.at<uchar>(row,3)*F.at<uchar>(phaseNum,4);
            }
            else if(i+2==N)
            {        
                scaler.at<uchar>(row,col)=
                    out.at<uchar>(row,i)*F.at<uchar>(phaseNum,1)+
                    out.at<uchar>(row,i+1)*F.at<uchar>(phaseNum,2)+
                    out.at<uchar>(row,i+2)*F.at<uchar>(phaseNum,3)+
                    out.at<uchar>(row,i+2)*F.at<uchar>(phaseNum,4);
            }
            else if(i+1==N||i==N)
            {        
                scaler.at<uchar>(row,col)=
                    out.at<uchar>(row,i)*F.at<uchar>(phaseNum,1)+
                    out.at<uchar>(row,i+1)*F.at<uchar>(phaseNum,2)+
                    out.at<uchar>(row,i+1)*F.at<uchar>(phaseNum,3)+
                    out.at<uchar>(row,i+1)*F.at<uchar>(phaseNum,4);
            }
            else
            {    scaler.at<uchar>(row,col)=
                    out.at<uchar>(row,i)*F.at<uchar>(phaseNum,1)+
                    out.at<uchar>(row,i+1)*F.at<uchar>(phaseNum,2)+
                    out.at<uchar>(row,i+2)*F.at<uchar>(phaseNum,3)+
                    out.at<uchar>(row,i+3)*F.at<uchar>(phaseNum,4);
            }

        }
    }
    namedWindow("MyWindow2",CV_WINDOW_AUTOSIZE);
    imshow("MyWindow2",scaler);
    waitKey(0);
    destroyWindow("MyWindow2");
    //std::cout<< M.at<Vec2i>(0,0)[0]<< std::endl; 
    std::cout<<scaler<< std::endl; 
    
    getchar();
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/wxl845235800/p/9082025.html