Open CV学习之路(十八)—视差图修改

#include<opencv2\opencv.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
void on_mouse(int event, int x, int y, int flags, void* ustc);
Mat image;
int main()
{

    /*VideoCapture capture("E:\\SamsungMP3Player.avi");
    int numFrame = capture.get(CAP_PROP_FRAME_COUNT);
    cout << "numFrame:" << numFrame <<endl;*/
    Size s(1920, 1152);
    char videofile[128];
    sprintf_s(videofile, "myvideo%2f.avi", '01');
    //VideoWriter writer = VideoWriter(videofile, -1, 24, s);//save image to video
    for (int i = 1; i <500; i++)
    {
        char rowfilename[255];
        sprintf(rowfilename, "E:\\2\\a%d.bmp", i);//(384,640)
        image = imread(rowfilename);
        /*cout << "Image's rows:" << image.rows << endl;
        cout << "Image's cols:" << image.cols << endl;*/
        namedWindow("Image1", WINDOW_NORMAL);
        namedWindow("Image2", WINDOW_NORMAL);
        if (image.empty())
        {
            break;
        }
        //setMouseCallback("Image", on_mouse, 0);//
        //---图像处理
        Mat image1 = Mat(1152, 1920, image.type());//create a new image
        for (int i = 0; i < image.rows; i++)
        {
            Vec3b* p_image = image.ptr<Vec3b>(i);
            Vec3b* p_image1 = image1.ptr<Vec3b>(i);
            for (int j = 0; j < image.cols; j++)
            {
                p_image1[j][0] = p_image[j][0];
                p_image1[j][1] = p_image[j][1];
                p_image1[j][2] = p_image[j][2];

            }
        }
        //---move
        for (int i = 1023; i >= 768; i--)
        {
            Vec3b* p = image1.ptr<Vec3b>(i);
            for (int j = 640; j <640 * 2; j++)
            {
                int x = j;
                int y = i + 128;
                if (x >= 0 && y >= 0 && x <= image1.cols && y <= image1.rows)
                    image1.ptr<Vec3b>(y)[x] = p[j];

            }
        }
        for (int i = 1023; i >= 768; i--)
        {
            Vec3b* p = image1.ptr<Vec3b>(i);
            for (int j = 0; j <640; j++)
            {
                int x = j;
                int y = i + 128;
                if (x >= 0 && y >= 0 && x <= image1.cols && y <= image1.rows)
                    image1.ptr<Vec3b>(y)[x] = p[j];

            }
        }
        for (int i = 768; i<896; i++)
        {
            Vec3b* p = image1.ptr<Vec3b>(i);
            for (int j = 640; j <640 * 2; j++)
            {
                int x = j - 640;
                int y = i;
                if (x >= 0 && y >= 0 && x <= image1.cols && y <= image1.rows)
                    image1.ptr<Vec3b>(y)[x] = p[j];

            }
        }
        for (int i = 768; i<1024; i++)
        {
            Vec3b* p = image1.ptr<Vec3b>(i);
            for (int j = 640 * 2; j <640 * 3; j++)
            {
                int x = j - 640;
                int y = i;
                if (x >= 0 && y >= 0 && x <= image1.cols && y <= image1.rows)
                    image1.ptr<Vec3b>(y)[x] = p[j];

            }
        }
        //---set to white
        for (int i = 768; i<1152; i++)
        {
            Vec3b* p = image1.ptr<Vec3b>(i);
            for (int j = 640 * 2; j <640 * 3; j++)
            {
                p[j][0] = 255;
                p[j][1] = 255;
                p[j][2] = 255;

            }
        }
        //---移动块
        Mat image2 = Mat(1152, 1920, image.type(),Scalar(255,255,255));
        vector<Rect> rect;
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                Rect rectTemp(i * 640, j * 384, 640, 384);
                rect.push_back(rectTemp);
            }
        }
        //Rect rect1(0, 0, 640, 384);
        image1(rect[1]).copyTo(image2(rect[1]));
        //Rect rect2(640,384, 640, 384);
        image1(rect[2]).copyTo(image2(rect[2]));
        //---
        //imshow("Image", image);
        //---save to video
        //writer << image1;
        cout << "save " << i << " frame...\n";
        //---
        imshow("Image1", image1);
        imshow("Image2", image2);
        //---save to image
        char filename[255];
        sprintf(filename, "E:\\3\\img%d.bmp", i);
        //imwrite(filename, image1);
        //---
        if (waitKey(30) == 27)
            break;
    }

    return 0;
}
//---图像平移
void translateTransform(Mat const& src, Mat& dst, int dx, int dy)
{
    CV_Assert(src.depth() == CV_8U);

    const int rows = src.rows;
    const int cols = src.cols;

    dst.create(rows, cols, src.type());

    Vec3b *p;
    for (int i = 0; i < rows; i++)
    {
        p = dst.ptr<Vec3b>(i);
        for (int j = 0; j < cols; j++)
        {
            //平移后坐标映射到原图像
            int x = j - dx;
            int y = i - dy;

            //保证映射后的坐标在原图像范围内
            if (x >= 0 && y >= 0 && x < cols && y < rows)
                p[j] = src.ptr<Vec3b>(y)[x];
        }
    }
}
//---获取图像上点的坐标
void on_mouse(int event, int x, int y, int flags, void* ustc)
{
    /*CvFont font;
    cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);*/
    if (event == CV_EVENT_LBUTTONDOWN)
    {
        CvPoint pt = cvPoint(x, y);
        char temp[16];
        sprintf(temp, "(%d,%d)", pt.x, pt.y);
        cout << "position:" << temp << endl;
        putText(image, temp, pt, FONT_HERSHEY_SCRIPT_SIMPLEX, 3, Scalar(0, 0, 255), 3, 8);

        /*
        void cvPutText( CvArr* img, const char* text, CvPoint org, const CvFont* font,CvScalar color );
              img:输入图像
                    text:要显示的字符串
                          org:第一个字母左下角的坐标
                                font:指向字体结构的指针
                                      color:文本的颜色.
                                      */
                                      //cvCircle(src, pt, 2, cvScalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);
                                      /*
                                      定义
                                      void cvCircle( CvArr* img, CvPoint center, int radius, CvScalar color, int thickness=1, int line_type=8, int shift=0 );[1]

                                      img 图像
                                      center 圆心坐标
                                      radius 圆形的半径
                                      color 线条的颜色
                                      thickness 如果是正数,表示组成圆的线条的粗细程度。否则,表示圆是否被填充
                                      line_type 线条的类型。见 cvLine 的描述
                                      shift 圆心坐标点和半径值的小数点位数
                                      */
        imshow("Image", image);
    }
}
//---
/*
OpenCV中的鼠标响应的函数是setMouseCallback(),可以实现画图的功能。
c++: void setMousecallback(const string& winname, MouseCallback onMouse, void* userdata=0)
winname:窗口的名字
onMouse:鼠标响应函数,回调函数。指定窗口里每次鼠标时间发生的时候,被调用的函数指针。 这个函数的原型应该为void on_Mouse(int event, int x, int y, int flags, void* param);
userdate:传给回调函数的参数

void on_Mouse(int event, int x, int y, int flags, void* param);
event是 CV_EVENT_*变量之一
x和y是鼠标指针在图像坐标系的坐标(不是窗口坐标系)
flags是CV_EVENT_FLAG的组合, param是用户定义的传递到setMouseCallback函数调用的参数。
*/
#include<opencv2\opencv.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
void on_mouse(int event, int x, int y, int flags, void* ustc);
Mat image;
int main()
{

    /*VideoCapture capture("E:\\SamsungMP3Player.avi");
    int numFrame = capture.get(CAP_PROP_FRAME_COUNT);
    cout << "numFrame:" << numFrame <<endl;*/
    Size s(1920, 1152);
    char videofile[128];
    sprintf_s(videofile, "myvideo%2f.avi", '01');
    //VideoWriter writer = VideoWriter(videofile, -1, 24, s);//save image to video
    for (int i = 1; i <500; i++)
    {
        char rowfilename[255];
        sprintf(rowfilename, "E:\\2\\a%d.bmp", i);//(384,640)
        image = imread(rowfilename);
        /*cout << "Image's rows:" << image.rows << endl;
        cout << "Image's cols:" << image.cols << endl;*/
        /*namedWindow("Image", WINDOW_NORMAL);
        namedWindow("Image1", WINDOW_NORMAL);*/
        if (image.empty())
        {
            break;
        }
        //setMouseCallback("Image", on_mouse, 0);//
        //---图像处理
        Mat image1 = Mat(1152, 1920, image.type());//create a new image
        for (int i = 0; i < image.rows; i++)
        {
            Vec3b* p_image = image.ptr<Vec3b>(i);
            Vec3b* p_image1 = image1.ptr<Vec3b>(i);
            for (int j = 0; j < image.cols; j++)
            {
                p_image1[j][0] = p_image[j][0];
                p_image1[j][1] = p_image[j][1];
                p_image1[j][2] = p_image[j][2];

            }
        }
        //---move
        for (int i = 1023; i >= 768; i--)
        {
            Vec3b* p = image1.ptr<Vec3b>(i);
            for (int j = 640; j <640 * 2; j++)
            {
                int x = j;
                int y = i + 128;
                if (x >= 0 && y >= 0 && x <= image1.cols && y <= image1.rows)
                    image1.ptr<Vec3b>(y)[x] = p[j];

            }
        }
        for (int i = 1023; i >= 768; i--)
        {
            Vec3b* p = image1.ptr<Vec3b>(i);
            for (int j = 0; j <640; j++)
            {
                int x = j;
                int y = i + 128;
                if (x >= 0 && y >= 0 && x <= image1.cols && y <= image1.rows)
                    image1.ptr<Vec3b>(y)[x] = p[j];

            }
        }
        for (int i = 768; i<896; i++)
        {
            Vec3b* p = image1.ptr<Vec3b>(i);
            for (int j = 640; j <640 * 2; j++)
            {
                int x = j - 640;
                int y = i;
                if (x >= 0 && y >= 0 && x <= image1.cols && y <= image1.rows)
                    image1.ptr<Vec3b>(y)[x] = p[j];

            }
        }
        for (int i = 768; i<1024; i++)
        {
            Vec3b* p = image1.ptr<Vec3b>(i);
            for (int j = 640 * 2; j <640 * 3; j++)
            {
                int x = j - 640;
                int y = i;
                if (x >= 0 && y >= 0 && x <= image1.cols && y <= image1.rows)
                    image1.ptr<Vec3b>(y)[x] = p[j];

            }
        }
        //---set to white
        for (int i = 768; i<1152; i++)
        {
            Vec3b* p = image1.ptr<Vec3b>(i);
            for (int j = 640 * 2; j <640 * 3; j++)
            {
                p[j][0] = 255;
                p[j][1] = 255;
                p[j][2] = 255;

            }
        }
        //---移动块
        /*Mat image2 = Mat(1152, 1920, image.type(),Scalar(255,255,255));
        Rect rect1(0, 0, 640, 384);
        image1(rect1).copyTo(image2);
        Rect rect2(640,384, 640, 384);
        image1(rect2).copyTo(image2);*/
        //---
        //imshow("Image", image);
        //---save to video
        //writer << image1;
        cout << "save " << i << " frame...\n";
        //---
        imshow("Image1", image1);
        imshow("Image2", image2);
        //---save to image
        char filename[255];
        sprintf(filename, "E:\\3\\img%d.bmp", i);
        //imwrite(filename, image1);
        //---
        if (waitKey(30) == 27)
            break;
    }

    return 0;
}
//---图像平移
void translateTransform(Mat const& src, Mat& dst, int dx, int dy)
{
    CV_Assert(src.depth() == CV_8U);

    const int rows = src.rows;
    const int cols = src.cols;

    dst.create(rows, cols, src.type());

    Vec3b *p;
    for (int i = 0; i < rows; i++)
    {
        p = dst.ptr<Vec3b>(i);
        for (int j = 0; j < cols; j++)
        {
            //平移后坐标映射到原图像
            int x = j - dx;
            int y = i - dy;

            //保证映射后的坐标在原图像范围内
            if (x >= 0 && y >= 0 && x < cols && y < rows)
                p[j] = src.ptr<Vec3b>(y)[x];
        }
    }
}
//---获取图像上点的坐标
void on_mouse(int event, int x, int y, int flags, void* ustc)
{
    /*CvFont font;
    cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);*/
    if (event == CV_EVENT_LBUTTONDOWN)
    {
        CvPoint pt = cvPoint(x, y);
        char temp[16];
        sprintf(temp, "(%d,%d)", pt.x, pt.y);
        cout << "position:" << temp << endl;
        putText(image, temp, pt, FONT_HERSHEY_SCRIPT_SIMPLEX, 3, Scalar(0, 0, 255), 3, 8);

        /*
        void cvPutText( CvArr* img, const char* text, CvPoint org, const CvFont* font,CvScalar color );
              img:输入图像
                    text:要显示的字符串
                          org:第一个字母左下角的坐标
                                font:指向字体结构的指针
                                      color:文本的颜色.
                                      */
                                      //cvCircle(src, pt, 2, cvScalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);
                                      /*
                                      定义
                                      void cvCircle( CvArr* img, CvPoint center, int radius, CvScalar color, int thickness=1, int line_type=8, int shift=0 );[1]

                                      img 图像
                                      center 圆心坐标
                                      radius 圆形的半径
                                      color 线条的颜色
                                      thickness 如果是正数,表示组成圆的线条的粗细程度。否则,表示圆是否被填充
                                      line_type 线条的类型。见 cvLine 的描述
                                      shift 圆心坐标点和半径值的小数点位数
                                      */
        imshow("Image", image);
    }
}
//---
/*
OpenCV中的鼠标响应的函数是setMouseCallback(),可以实现画图的功能。
c++: void setMousecallback(const string& winname, MouseCallback onMouse, void* userdata=0)
winname:窗口的名字
onMouse:鼠标响应函数,回调函数。指定窗口里每次鼠标时间发生的时候,被调用的函数指针。 这个函数的原型应该为void on_Mouse(int event, int x, int y, int flags, void* param);
userdate:传给回调函数的参数

void on_Mouse(int event, int x, int y, int flags, void* param);
event是 CV_EVENT_*变量之一
x和y是鼠标指针在图像坐标系的坐标(不是窗口坐标系)
flags是CV_EVENT_FLAG的组合, param是用户定义的传递到setMouseCallback函数调用的参数。
*/
#include<opencv2\opencv.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
void on_mouse(int event, int x, int y, int flags, void* ustc);
Mat image;
int main()
{

    /*VideoCapture capture("E:\\SamsungMP3Player.avi");
    int numFrame = capture.get(CAP_PROP_FRAME_COUNT);
    cout << "numFrame:" << numFrame <<endl;*/
    Size s(1920, 1152);
    char videofile[128];
    sprintf_s(videofile, "myvideo%2f.avi", '01');
    VideoWriter writer = VideoWriter(videofile,-1, 24, s);
    for (int i = 1; i <500; i++)
    {
        char rowfilename[255];
        sprintf(rowfilename, "E:\\2\\a%d.bmp", i);//(640,384)
        image = imread(rowfilename);
        cout << "Image's rows:" << image.rows << endl;
        cout << "Image's cols:" << image.cols << endl;
        namedWindow("Image",WINDOW_NORMAL);
        //namedWindow("Image1", WINDOW_NORMAL);
        namedWindow("Image2", WINDOW_NORMAL);
        if (image.empty())
        {
            break;
        }
        //setMouseCallback("Image", on_mouse, 0);//
        //---图像处理
        Mat image1 = Mat(1152, 1920, image.type());
        for (int i = 0; i < image.rows; i++)
        {
            Vec3b* p_image = image.ptr<Vec3b>(i);
            Vec3b* p_image1 = image1.ptr<Vec3b>(i);
            for (int j = 0; j < image.cols; j++)
            {
                p_image1[j][0] = p_image[j][0];
                p_image1[j][1] = p_image[j][1];
                p_image1[j][2] = p_image[j][2];

            }
        }
        //Mat image2 = image1.clone();
        Mat image2 = Mat(1152, 1920, image.type());
        for (int i = 0; i < image1.rows; i++)
        {
            Vec3b* p_image1 = image1.ptr<Vec3b>(i);
            Vec3b* p_image2 = image2.ptr<Vec3b>(i);
            for (int j = 0; j < image.cols; j++)
            {
                p_image2[j][0] = p_image1[j][0];
                p_image2[j][1] = p_image1[j][1];
                p_image2[j][2] = p_image1[j][2];

            }
        }
        //---move
        for (int i = 1023; i >= 768; i--)
        {
            Vec3b* p = image2.ptr<Vec3b>(i);
            for (int j = 640; j <640*2; j++)
            {
                int x = j;
                int y = i + 128;
                if (x >= 0 && y >= 0 && x <= image2.cols && y <= image2.rows)
                    image2.ptr<Vec3b>(y)[x] = p[j];

            }
        }
        for (int i =1023; i>=768; i--)
        {
            Vec3b* p = image2.ptr<Vec3b>(i);
            for (int j = 0; j <640; j++)
            {
                int x = j;
                int y = i +128;
                if (x >= 0 && y >= 0 && x <=image2.cols && y <= image2.rows)
                    image2.ptr<Vec3b>(y)[x] = p[j];

            }
        }
        for (int i =768;i<896; i++)
        {
            Vec3b* p = image2.ptr<Vec3b>(i);
            for (int j = 640; j <640*2; j++)
            {
                int x = j-640;
                int y = i;
                if (x >= 0 && y >= 0 && x <= image2.cols && y <= image2.rows)
                    image2.ptr<Vec3b>(y)[x] = p[j];

            }
        }
        for (int i = 768; i<1024; i++)
        {
            Vec3b* p = image2.ptr<Vec3b>(i);
            for (int j = 640*2; j <640 * 3; j++)
            {
                int x = j - 640;
                int y = i;
                if (x >= 0 && y >= 0 && x <= image2.cols && y <= image2.rows)
                    image2.ptr<Vec3b>(y)[x] = p[j];

            }
        }
        //---set to white
        for (int i = 768; i<1152; i++)
        {
            Vec3b* p = image2.ptr<Vec3b>(i);
            for (int j = 640*2; j <640 * 3; j++)
            {
                p[j][0] =255;
                p[j][1] =255;
                p[j][2] =255;

            }
        }
        //---
        //imshow("Image", image);
        //imshow("Image1", image1);
        //---save to video
        writer << image2;
        cout << "save " << i << " frame...";
        //---
        imshow("Image2", image2);
        //---save to image
        char filename[255];
        sprintf(filename, "E:\\3\\img%d.bmp", i);
        imwrite(filename,image2);
        //---
        waitKey(24);
    }

    return 0;
}
//---图像平移
void translateTransform(Mat const& src,Mat& dst, int dx, int dy)
{
    CV_Assert(src.depth() == CV_8U);

    const int rows = src.rows;
    const int cols = src.cols;

    dst.create(rows, cols, src.type());

    Vec3b *p;
    for (int i = 0; i < rows; i++)
    {
        p = dst.ptr<Vec3b>(i);
        for (int j = 0; j < cols; j++)
        {
            //平移后坐标映射到原图像
            int x = j - dx;
            int y = i - dy;

            //保证映射后的坐标在原图像范围内
            if (x >= 0 && y >= 0 && x < cols && y < rows)
                p[j] = src.ptr<Vec3b>(y)[x];
        }
    }
}
//---获取图像上点的坐标
void on_mouse(int event, int x, int y, int flags, void* ustc)
{
    /*CvFont font;
    cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);*/
    if (event == CV_EVENT_LBUTTONDOWN)
    {
        CvPoint pt = cvPoint(x, y);
        char temp[16];
        sprintf(temp, "(%d,%d)", pt.x, pt.y);
        cout << "position:" << temp << endl;
        putText(image, temp, pt, FONT_HERSHEY_SCRIPT_SIMPLEX,3,Scalar(0,0,255),3,8);

        /*
        void cvPutText( CvArr* img, const char* text, CvPoint org, const CvFont* font,CvScalar color );
      img:输入图像
      text:要显示的字符串
      org:第一个字母左下角的坐标
      font:指向字体结构的指针
      color:文本的颜色.
        */
        //cvCircle(src, pt, 2, cvScalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);
        /*
        定义
        void cvCircle( CvArr* img, CvPoint center, int radius, CvScalar color, int thickness=1, int line_type=8, int shift=0 );[1] 

        img 图像
        center 圆心坐标
        radius 圆形的半径
        color 线条的颜色
        thickness 如果是正数,表示组成圆的线条的粗细程度。否则,表示圆是否被填充
        line_type 线条的类型。见 cvLine 的描述
        shift 圆心坐标点和半径值的小数点位数
        */
        imshow("Image", image);
    }
}
//---
/*
OpenCV中的鼠标响应的函数是setMouseCallback(),可以实现画图的功能。
c++: void setMousecallback(const string& winname, MouseCallback onMouse, void* userdata=0)
winname:窗口的名字
onMouse:鼠标响应函数,回调函数。指定窗口里每次鼠标时间发生的时候,被调用的函数指针。 这个函数的原型应该为void on_Mouse(int event, int x, int y, int flags, void* param);
userdate:传给回调函数的参数

void on_Mouse(int event, int x, int y, int flags, void* param);
event是 CV_EVENT_*变量之一
x和y是鼠标指针在图像坐标系的坐标(不是窗口坐标系)
flags是CV_EVENT_FLAG的组合, param是用户定义的传递到setMouseCallback函数调用的参数。
*/

猜你喜欢

转载自blog.csdn.net/u014413083/article/details/53106210