opencv自测程序(常用函数)

#include "PictureDemo.h"
#include 

PictureDemo::PictureDemo()
{

}

PictureDemo::~PictureDemo()
{

}

/* 读取和展示 */
void PictureDemo::test1()
{
    Mat src = imread("../x64/Debug/picture/2.jpg", IMREAD_REDUCED_COLOR_2);
    String winname = "Picture";
    //namedWindow(winname, WINDOW_FREERATIO);
    imshow(winname, src);
    waitKey(0);
    destroyAllWindows();
}

/* 转换和保存 */
void PictureDemo::test2()
{
    Mat src = imread("../x64/Debug/picture/2.jpg", IMREAD_REDUCED_COLOR_2);
    String winname = "Picture";

    //h 0~180, s,v 0~255
    Mat gray, hsv;
    cvtColor(src, hsv, COLOR_BGR2HSV);
    cvtColor(src, gray, COLOR_BGR2GRAY);
    imshow("1", src);
    imshow("2", hsv);
    imshow("3", gray);

    imwrite("../x64/Debug/picture/2_copy.jpg", hsv);

    waitKey(0);
    destroyAllWindows();
}

/* mat对象 */
void PictureDemo::test3()
{
    Mat dst = imread("../x64/Debug/picture/2_copy.jpg", IMREAD_REDUCED_COLOR_2);
    Mat dst1 = dst.clone();

    Mat dst2;
    dst.copyTo(dst2);

    Mat dst3 = Mat::zeros(Size(10, 10), CV_8UC2);
    std::cout << dst3 << std::endl;
    std::cout << "*********************" << dst3.cols << "*********************" << dst3.rows << std::endl;

    Mat dst4 = Mat::ones(Size(256, 256), CV_8UC3);
    dst4 = 24; //只赋值给第一个通道
    dst4 = Scalar(255, 26, 27);
    imshow("3", dst4);
}

/* mat对象数据操作 */
void PictureDemo::test4()
{
    Mat dst1 = Mat::zeros(Size(480, 270), CV_8UC3);

    for(int i = 0; i < 270; i++) {
        for(int j = 0; j < 480; j++) {
            if(0) { //灰度图
                int ii = i % 255;
                dst1.at(i, j) = ii;
            }

            if(1) { //彩色图
                dst1.at(i, j)[0] = i % 255;
                dst1.at(i, j)[1] = 255 - i % 255;
                dst1.at(i, j)[2] = i % 255;
            }
        }
    }
    imshow("3", dst1);

    Mat dst = imread("../x64/Debug/picture/2.jpg", IMREAD_REDUCED_COLOR_2);
    for(int i = 0; i < dst.rows; i++) { //数组操作
        for(int j = 0; j < dst.cols; j++) {
            dst.at(i, j)[0] = 255 - dst.at(i, j)[0];
            dst.at(i, j)[1] = 255 - dst.at(i, j)[1];
            dst.at(i, j)[2] = 255 - dst.at(i, j)[2];
        }
    }

    for(int i = 0; i < dst.rows; i++) { //指针操作
        Vec3b *curRow = dst.ptr(i);
        for(int j = 0; j < dst.cols; j++) {
            curRow[j][0] = 255 - curRow[j][0];
            curRow[j][1] = 255 - curRow[j][1];
            curRow[j][2] = 255 - curRow[j][2];
        }
    }
    imshow("2", dst);
}

/* mat算术操作 */
void PictureDemo::test5()
{
    Mat dst = imread("../x64/Debug/picture/3.jpg", IMREAD_REDUCED_COLOR_2);
    dst = dst + Scalar(50, 50, 50); //加
    imshow("1", dst);
    dst = dst - Scalar(100, 100, 100); //减
    imshow("2", dst);
    dst = dst / Scalar(2, 2, 2); //除
    imshow("3", dst);
    multiply(dst, Scalar(4, 4, 4), dst); //乘,加减除也有函数
    imshow("4", dst);

    for(int i = 0; i < dst.rows; i++) {
        for(int j = 0; j < dst.cols; j++) {
            dst.at(i, j)[0] = saturate_cast(dst.at(i, j)[0] - 150); //saturate_cast可以防止色值超越范围,图片不会花
            dst.at(i, j)[1] = saturate_cast(dst.at(i, j)[1] - 150);
            dst.at(i, j)[2] = saturate_cast(dst.at(i, j)[2] - 150);
        }
    }
    imshow("5", dst);
}

static void callbackBar(int brightness, void *data) //调整亮度
{
    Mat g_dst = imread("../x64/Debug/picture/3.jpg", IMREAD_REDUCED_COLOR_2);
    subtract(g_dst, Scalar(brightness, brightness, brightness), g_dst); //等同于g_dst = g_dst - Scalar(brightness, brightness, brightness)
    imshow("亮度与对比度", g_dst);
}

static void callbackBar2(int brightness, void *data) //调整对比度
{
    Mat g_dst = imread("../x64/Debug/picture/3.jpg", IMREAD_REDUCED_COLOR_2);

    /*参数1:src1,第一个原数组.
    * 参数2:alpha,第一个数组元素权重
    * 参数3:src2第二个原数组
    * 参数4:beta,第二个数组元素权重
    * 参数5:gamma,图1与图2作和后添加的数值。不要太大,不然图片一片白。总和等于255以上就是纯白色了。
    * 参数6:dst,输出图片 */
    addWeighted(g_dst, (float)brightness / 40.0, Scalar(0, 0, 0), 0, 0, g_dst);

    imshow("亮度与对比度", g_dst);
}

/* 滚动条拖动调整亮度、对比度 */
void PictureDemo::test6()
{
    namedWindow("亮度与对比度", WINDOW_AUTOSIZE);
    int max = 200;
    int cur = 100;

    createTrackbar("brightness", "亮度与对比度", &cur, max, callbackBar);
    createTrackbar("contrast", "亮度与对比度", &cur, max, callbackBar2);
}

/* 融合两张图片 */
void fuse()
{
    Mat g_dst = imread("../x64/Debug/picture/1.jpg", IMREAD_REDUCED_COLOR_2); //图片大小,类型要一样大
    Mat g_dst2 = imread("../x64/Debug/picture/6.jpg", IMREAD_REDUCED_COLOR_2);

    Mat dst1;
    addWeighted(g_dst, 0.5, g_dst2, 0.5, 0, dst1);
    imshow("融合", dst1);

    resize(g_dst, g_dst, Size(g_dst.cols * 0.5, g_dst.rows * 0.5));
    Mat imageROI = g_dst2(Rect(20, 40, g_dst.cols, g_dst.rows)); //利用ROI,获取将要理图像的矩形大小  
    imshow("a", imageROI);
    imshow("b", g_dst2);
    addWeighted(g_dst, 1, imageROI, 0, 0, imageROI); //修改imageROI会影响g_dst2
    imshow("混合后的图片", g_dst2);

    Mat src = imread("../x64/Debug/picture/1.jpg", IMREAD_REDUCED_COLOR_2);
    Mat src1 = imread("../x64/Debug/picture/4.jpg", IMREAD_REDUCED_COLOR_2);
    Mat src2;
    scaleAdd(src, 0.5, src1, src2); //计算缩放数组和另一个数组的和
    imshow("3", src2);
}

/* 键盘操作 */
void PictureDemo::test7()
{
    Mat g_dst = imread("../x64/Debug/picture/3.jpg", IMREAD_REDUCED_COLOR_2);
    imshow("fuse", g_dst);

    fuse();
    Mat dst;
    while(1) {
        char c = waitKey(500);
        std::cout << c << std::endl; // 打印键盘输入
        if(c == '2') { //键盘响应事件
            break;
        }
    }
}

/* 颜色表 */
void PictureDemo::test8()
{
    int colormap[] = {COLORMAP_AUTUMN, COLORMAP_BONE, COLORMAP_JET, COLORMAP_WINTER, COLORMAP_RAINBOW, COLORMAP_OCEAN,COLORMAP_SUMMER, 
        COLORMAP_SPRING, COLORMAP_COOL, COLORMAP_HSV, COLORMAP_PINK, COLORMAP_HOT, COLORMAP_PARULA, COLORMAP_MAGMA, COLORMAP_INFERNO, 
        COLORMAP_PLASMA, COLORMAP_VIRIDIS, COLORMAP_CIVIDIS, COLORMAP_TWILIGHT, COLORMAP_TWILIGHT_SHIFTED, COLORMAP_TURBO, COLORMAP_DEEPGREEN};

    Mat g_dst = imread("../x64/Debug/picture/4.jpg", IMREAD_REDUCED_COLOR_2);
    imshow("原图", g_dst);
    Mat dst;

    int i = 0;
    while(1) {
        waitKey(500);
        applyColorMap(g_dst, dst, colormap[i % 22]);
        i++;
        imshow("颜色风格", dst);
    }
}

/* 与、或、非、异或 */
void PictureDemo::test9()
{
    Mat dst1 = Mat::zeros(Size(270, 270), CV_8UC3);
    Mat dst2 = Mat::zeros(Size(270, 270), CV_8UC3);

    rectangle(dst1, Rect(0,0,40,40), Scalar(255,0,255), -1, LINE_AA, 0); //画矩形
    rectangle(dst2, Rect(30,30,50,50), Scalar(0,255,255), 2, LINE_8, 0);
    
    imshow("1", dst1);
    imshow("22", dst2);

    Rect imgRect = Rect(0,0,40,40);
    Point pt1 = Point(20, 200);
    Point pt2 = Point(220, 500);
    bool dsd = clipLine(imgRect, pt1, pt2); //判断线是否在矩形内

    Mat dst3;
    bitwise_and(dst1, dst2, dst3);
    imshow("3", dst3);

    bitwise_or(dst1, dst2, dst3);
    imshow("4", dst3);

    Mat g_dst = imread("../x64/Debug/picture/1.jpg", IMREAD_REDUCED_COLOR_2);
    bitwise_not(g_dst, g_dst);
    imshow("5", g_dst);

    bitwise_xor(dst1, dst2, dst3);
    imshow("6", dst3);

    
}

/* 通道合并和分离 */
void PictureDemo::test10()
{
    Mat m = imread("../x64/Debug/picture/1.jpg", IMREAD_REDUCED_COLOR_2);
    imshow("原图", m);

    std::vector vImg;
    split(m, vImg); //分离
    imshow("red", vImg[0]);
    imshow("green", vImg[1]);
    imshow("blue", vImg[2]);

    Mat dst;
    vImg[0] = 0;
    merge(vImg, dst); //合并
    imshow("merge", dst);

    int szMix[] = {0, 2, 1, 0, 2, 1};
    mixChannels(&m, 1, &dst, 1, szMix, 3); //混合, m和dst可以是Mat数组
    imshow("mixChannels", dst);
}

/* 扣图,替换背景 */
void PictureDemo::test11()
{
    Mat m = imread("../x64/Debug/picture/7.jpg", IMREAD_REDUCED_COLOR_2);
    imshow("原图", m);

    Mat hsv; 
    cvtColor(m, hsv, COLOR_BGR2HSV);//H色调,S饱和度,V明度

    Mat mask;
    inRange(hsv, Scalar(78,43,46), Scalar(99,255,255), mask); //扣图
    imshow("mask", mask);

    Mat newBack = Mat::zeros(m.size(), m.type());
    newBack = Scalar(250, 240, 230);
    bitwise_not(mask, mask);
    imshow("mask1", mask);

    m.copyTo(newBack, mask); //m对应mask为0(黑色的)的像素不会复制给newBack
    imshow("mask2", newBack);
}

/* 计算均值和方差 */
void PictureDemo::test12()
{
    Mat m = imread("../x64/Debug/picture/5.jpg", IMREAD_REDUCED_COLOR_2);
    imshow("原图", m);

    Scalar m1 = mean(m);
    std::cout << "*******" << m1 << std::endl;

    std::vector vImg;
    split(m, vImg); //分离

    double iMin, iMax;
    Point minLoc, maxLoc;
    Mat n;

    minMaxLoc(vImg[0], &iMin, &iMax, &minLoc, &maxLoc, n); //求最大值最小值
    std::cout << "$$$$$$$$$" << iMin << "--" << iMax << minLoc << maxLoc << std::endl;

    Mat mean, stddv;
    meanStdDev(m, mean, stddv); //mean均值 stddev标准差
    std::cout << "#########" << stddv << std::endl;
    std::cout << "&&&&&&&&&" << mean << std::endl;
}

/* 几何形状填充 */
void PictureDemo::test13()
{
    Mat dst1 = Mat::zeros(Size(1720, 880), CV_8UC3);
    dst1 = Scalar(250, 240, 230);

    Rect re;
    re.x = 50;
    re.y = 50;
    re.width = 200;
    re.height = 200;
    rectangle(dst1, re, Scalar(255,0,255), 3, LINE_4, 0);

    circle(dst1, Point(400, 400), 90, Scalar(0,10,20), 4, LINE_AA, 0); //圆

    line(dst1, Point(20, 200), Point(220, 500), Scalar(128,120,240), 5, LINE_AA, 0); //线

    RotatedRect re2;
    re2.center = Point(800, 400);
    re2.size = Size(180, 250);
    re2.angle = 0;
    ellipse(dst1, re2, Scalar(128,120,240), -1, LINE_AA); //椭圆

    line(dst1, Point(1020, 400), Point(1380, 400), Scalar(128,120,240), 3, LINE_4, 0);
    ellipse(dst1, Point(1200, 400), Size(180, 220), 0, 180, 360, Scalar(128,120,240), 3, LINE_4, 0); //半椭圆

    Point p1(600, 100);
    Point p2(900, 150);
    Point p3(900, 350);
    Point p4(850, 450);
    Point p5(650, 450);
    std::vector pts;
    pts.push_back(p1);
    pts.push_back(p2);
    pts.push_back(p3);
    pts.push_back(p4);
    pts.push_back(p5);
    polylines(dst1, pts, true, Scalar(128, 128, 128), 2, LINE_8, 0); //不规则多边形

    imshow("原图", dst1);
}

/* 随机数 */
void PictureDemo::test14()
{
    Mat dst = Mat::zeros(Size(500, 500), CV_8UC3);
    RNG cNum(12345);
    while(1) {
        int c = waitKey(500);
        if(c == 'a') {
            break;
        }
        int r = cNum.uniform(1, 255);
        int g = cNum.uniform(1, 255);
        int b = cNum.uniform(1, 255);

        dst = Scalar(255 - r, 255 - g, 255 - b);
        Point p1(cNum.uniform(1, 500), cNum.uniform(1, 500));
        Point p2(cNum.uniform(1, 500), cNum.uniform(1, 500));
        Point p3(cNum.uniform(1, 500), cNum.uniform(1, 500));
        std::vector pts;
        pts.push_back(p1);
        pts.push_back(p2);
        pts.push_back(p3);
        polylines(dst, pts, true, Scalar(r, g, b), 2, LINE_8, 0); //不规则多边形
        imshow("原图", dst);
    }
}

/* 填充多边形 */
void PictureDemo::test15()
{
    Mat dst1 = Mat::zeros(Size(1720, 880), CV_8UC3);
    dst1 = Scalar(250, 240, 230);

    Point p1(600, 100);
    Point p2(900, 150);
    Point p3(900, 350);
    Point p4(850, 450);
    Point p5(650, 450);
    Point p6(550, 300);
    std::vector pts;
    pts.push_back(p1);
    pts.push_back(p2);
    pts.push_back(p3);
    pts.push_back(p4);
    pts.push_back(p5);
    pts.push_back(p6);
    polylines(dst1, pts, true, Scalar(128, 128, 128), 4, LINE_8, 0); 

    fillPoly(dst1, pts, Scalar(180, 180, 180)); //填充

    std::vector> vContours;
    Point p11(1110, 110);
    Point p21(1410, 160);
    Point p31(1410, 360);
    Point p41(1360, 460);
    Point p51(1160, 460);
    std::vector pts1;
    pts1.push_back(p11);
    pts1.push_back(p21);
    pts1.push_back(p31);
    pts1.push_back(p41);
    pts1.push_back(p51);

    vContours.push_back(pts1);
    drawContours(dst1, vContours, 0, Scalar(180, 180, 180), -1); //绘制+填充,边界和填充颜色一致,第三个参数填-1表示vContours内的图形全绘制

    imshow("原图", dst1);
}

/* 鼠标操作与响应 */
Mat g_m1 = imread("../x64/Debug/picture/2.jpg", IMREAD_REDUCED_COLOR_2);
static void callbackMouse(int event, int x, int y, int flags, void* userdata)
{
    Mat g_m;
    static int iDownX = -1, iDownY = -1;
    if(EVENT_LBUTTONDOWN == event) { //按下
        iDownX = x;
        iDownY = y;
        g_m1.copyTo(g_m);
        imshow("原图", g_m);
    }
    else if(EVENT_LBUTTONUP == event) { //抬起
        if(iDownX >= 0 && iDownY >= 0 && x > iDownX && y > iDownY) {
            Rect g_re;
            g_re.x = iDownX;
            g_re.y = iDownY;
            g_re.width = x - iDownX;
            g_re.height = y - iDownY;
            g_m1.copyTo(g_m);
            rectangle(g_m, g_re, Scalar(255,0,255), 1, LINE_4, 0);
            imshow("原图", g_m);
            imshow("原图1", g_m(g_re));
            iDownX = -1;
            iDownY = -1;
        }
    }
    else if(EVENT_MOUSEMOVE == event) { //拖动
        if(iDownX >= 0 && iDownY >= 0 && x > iDownX && y > iDownY) {
            Rect g_re;
            g_re.x = iDownX;
            g_re.y = iDownY;
            g_re.width = x - iDownX;
            g_re.height = y - iDownY;
            g_m1.copyTo(g_m);
            rectangle(g_m, g_re, Scalar(255,0,255), 1, LINE_4, 0);
            imshow("原图", g_m);
            imshow("原图1", g_m(g_re));
            std::cout << "$$$$$$$$$" << x << "," << y << "," << iDownX  << ","<< iDownY << std::endl;
        }
    }
}


void PictureDemo::test16()
{
    Mat m = imread("../x64/Debug/picture/5.jpg", IMREAD_REDUCED_COLOR_2);
    namedWindow("原图", WINDOW_AUTOSIZE);
    setMouseCallback("原图", callbackMouse, (void*)(&m)); //注册鼠标事件
}

/* 归一化 */
void PictureDemo::test17()
{
    Mat src = imread("../x64/Debug/picture/3.jpg", IMREAD_REDUCED_COLOR_2);
    Mat src1;
    imshow("原图", src);

    src.convertTo(src1, CV_32F); 

    std::cout << "@@@@@@:" << src.type() << "$$$$$$:" << src1.type() << std::endl; //type=16表示CV_8UC3, 21表示CV_32FC3
    imshow("1", src1); //不能正常显示,浮点数必须在0-1之间

    normalize(src1, src1, 1.0, 0, NORM_MINMAX);
    imshow("2", src1);
}

/* 放缩与插值 */
void PictureDemo::test18()
{
    Mat src = imread("../x64/Debug/picture/1.jpg", IMREAD_REDUCED_COLOR_2);
    Mat enlarge, narrow;
    imshow("1", src);

    cv::resize(src, enlarge, Size(src.cols * 1.5, src.rows * 1.5), 0, 0, INTER_LANCZOS4);
    imshow("22", enlarge);

    cv::resize(src, narrow, Size(src.cols / 2, src.rows / 2), 0, 0, INTER_LINEAR);
    imshow("3", narrow);
}

/* 镜像 */
void PictureDemo::test19()
{
    Mat src = imread("../x64/Debug/picture/1.jpg", IMREAD_REDUCED_COLOR_2);
    Mat enlarge;
    imshow("1", src);

    flip(src, enlarge, 0); //上下翻转
    imshow("2", enlarge);

    flip(src, enlarge, 1); //左右翻转
    imshow("3", enlarge);

    flip(src, enlarge, -1); //对角线翻转
    imshow("4", enlarge);
}

/* 旋转不丢角 */
void FullAngleRotate(Mat src, Mat &dst, int iAngle)
{
    int w = src.cols;
    int h = src.rows;
    Mat M = getRotationMatrix2D(Point(w/2, h/2), iAngle, 1.0);

    //确定新的旋转图像大小及偏移
    double cos = abs(M.at(0, 0)); //得到角度a的余弦值cos(a)	,abs()求元素的绝对值 ,CV_64F=double
    double sin = abs(M.at(0, 1)); //得到角度a的正弦值sin(a)
    int newW = cos * w + sin * h; //获取新的图像宽度
    int newH = sin * w + cos * h; //获取新的图像宽度
    M.at(0, 2) = M.at(0, 2) + (newW / 2 - w / 2); //更改旋转矩阵M中的(0,2)处的值,此处含义类似更改旋转中心宽度
    M.at(1, 2) = M.at(1, 2) + (newH / 2 - h / 2); //更改旋转矩阵M中的(1,2)处的值,此处含义类似更改旋转中心高度
    warpAffine(src, dst, M, Size(newW, newH), INTER_LINEAR, 0, Scalar(0, 0, 0));
}

/* 旋转 */
void PictureDemo::test20()
{
    Mat src = imread("../x64/Debug/picture/1.jpg", IMREAD_REDUCED_COLOR_2);
    Mat enlarge;

    int w = src.cols;
    int h = src.rows;

    auto M = getRotationMatrix2D(Point(w/2, h/2), 45, 1.0); //获取变换矩阵
    warpAffine(src, enlarge, M, src.size(), INTER_LINEAR, 0, Scalar(0, 0, 0)); //旋转45度
    imshow("22", enlarge);

    FullAngleRotate(src, enlarge, 45);
    imshow("3", enlarge);

    //平移
    M.at(0, 2) = M.at(0, 2) + 20; //更改旋转矩阵M中的(0,2)处的值,此处含义类似更改旋转中心宽度
    M.at(1, 2) = M.at(1, 2) + 20; //更改旋转矩阵M中的(1,2)处的值,此处含义类似更改旋转中心高度
    warpAffine(src, enlarge, M, Size(w + 40, h + 40), INTER_LINEAR, 0, Scalar(0, 0, 0));
    imshow("4", enlarge);
}

/* 稀疏矩阵 */
void PictureDemo::test21(Mat src)
{
    SparseMat image(src);

    *image.ptr(0, 0, 1) = 1000; //第三个参数:1:申请内存,0:不申请内存
    int k = *image.ptr(0, 0, 1);

    *(float *)image.ptr(0, 0, 1) = 3000.14;
    float kk = *(float *)image.ptr(0, 0, 1);

    image.ref(2, 3) = 6000.28;
    kk = image.ref(2, 3);

    image.ref(3, 5) = 50 + 55*256 + 44*256*256; //50是第一通道的值,55是第二通道的值,44是第三通道的值
    int value = image.ref(3, 5);
    int k0 = (value & 0xFF);                 //得到第一通道值
    int k1 = (value & 0xFF00) / 256;         //得到第二通道值
    int k2 = (value & 0xFF0000) / (256*256); //得到第三通道值

    kk = *image.find(4, 6);

    int cout = image.nzcount();  //返回sm1中非零元素的数目

    Mat dst;
    image.copyTo(dst);
    imshow("4", dst);
}

/* 基本运算 */
void PictureDemo::test22()
{
    Mat src = imread("../x64/Debug/picture/1.jpg", IMREAD_REDUCED_COLOR_2);
    Mat src1 = imread("../x64/Debug/picture/4.jpg", IMREAD_REDUCED_COLOR_2);
    imshow("0", src);

    Mat enlarge = cv::abs(src - src1);
    imshow("1", enlarge);
}

/* 取两个图片最大值和最小值 */
void PictureDemo::test23()
{
    Mat src = imread("../x64/Debug/picture/1.jpg", IMREAD_REDUCED_COLOR_2);
    Mat src1 = imread("../x64/Debug/picture/4.jpg", IMREAD_REDUCED_COLOR_2);

    Mat enlarge;
    cv::max(src, src1, enlarge);
    imshow("22", enlarge);

    Mat enlarge1;
    cv::min(src, src1, enlarge1);
    imshow("3", enlarge1);
}

/* 求解范数 */
void PictureDemo::test24()
{
    Mat src = imread("../x64/Debug/picture/1.jpg", IMREAD_REDUCED_COLOR_2);
    Mat src1 = imread("../x64/Debug/picture/4.jpg", IMREAD_REDUCED_COLOR_2);
    double ass = norm(src);
    std::cout << ass << std::endl;
    ass = norm(src1);
    std::cout << ass << std::endl;

    double ass1 = norm(src, src1);
    std::cout << ass1 << std::endl;
}

/* 执行向量的透视矩阵转换 */
void PictureDemo::test25()
{
    float fData[16] = { 0 };
    for (int i = 0; i < 16; i++) {
        fData[i] = i * 10;
    }
    cv::Mat m(4, 4, CV_32FC1, fData);

    Mat src = imread("../x64/Debug/picture/3.jpg", IMREAD_REDUCED_COLOR_2);
    Mat src2;
    src.convertTo(src2, CV_32F);
    Mat src1;
    perspectiveTransform(src2, src1, m); //本质是将图像投影到一个新的视平面
    imshow("3", src1);
}

/* 计算图像方向场 */
void PictureDemo::test26()
{
    Mat src = imread("../x64/Debug/picture/10.jpg", IMREAD_REDUCED_COLOR_2);
    Mat grad1, grad2, angle;
    Sobel(src, grad1, CV_64FC1, 1, 0); //求梯度
    Sobel(src, grad2, CV_64FC1, 0, 1);

    blur(grad1, grad1, Size(6,6));
    blur(grad2, grad2, Size(6,6));

    phase(grad1, grad2, angle, true); //求角度
    imshow("1", grad1);
    imshow("22", grad2);
    std::cout << angle << std::endl;
}

/* 随机数 */
void PictureDemo::test27()
{
    Mat src = imread("../x64/Debug/picture/2.jpg", IMREAD_REDUCED_COLOR_2);

    randu(src, Scalar(0,0,0), Scalar(0,0,255)); //返回均匀分布的随机数,填入数组或矩阵
    imshow("1", src);

    randn(src, Scalar(0,0,0), Scalar(0,0,255)); //返回高斯分布的随机数,填入数组或矩阵
    imshow("22", src);

    Mat src1;
    reduce(src, src1, 0, REDUCE_SUM, CV_32F); //将矩阵简化为向量
    std::cout << src1 << std::endl;
}

/* 图片重复填充 */
void PictureDemo::test28()
{
    Mat src;
    Mat src1 = imread("../x64/Debug/picture/10.jpg", IMREAD_REDUCED_COLOR_2);
    repeat(src1, 8, 6, src);
    imshow("22", src);
}

/* 画正方形对角线 */
void PictureDemo::test29()
{
    Mat src1 = imread("../x64/Debug/picture/16.png", IMREAD_REDUCED_COLOR_2);
    setIdentity(src1, Scalar(255, 225, 0));
    imshow("22", src1);
}

/* 解线性方程 A*X=B */
void PictureDemo::test30()
{
    Mat A = (Mat_(2, 2) << 1,2,3,4);
    Mat B = (Mat_(2, 1) <<5,11);
    Mat C;
    solve(A, B, C, DECOMP_LU);
    std::cout << C << std::endl;
}

/* 求解三次方程的根 */
void PictureDemo::test31()
{
    Mat A = (Mat_(1, 4) << 1,2,3,4); //x^3 * 1 + x^2 * 2 + x * 3 + 4 = 0
    Mat B;
    solveCubic(A, B);
    std::cout << B << std::endl;

    A = (Mat_(1, 3) << 1,2,3); //x^3 * 1 + x^2 * 2 + x * 3 = 0
    solveCubic(A, B);
    std::cout << B << std::endl;

    //solvePoly 求解n次方程的根
}

/* 二维数组排序 */
void PictureDemo::test32()
{
    Mat A = (Mat_(3, 3) << 3,1,2,4,8,2,9,7,8);
    Mat B; //B里面是对于数组下标
    sortIdx(A, B, SORT_EVERY_COLUMN); //列排序
    std::cout << A << std::endl;
    std::cout << B << std::endl;

    sortIdx(A, B, SORT_EVERY_ROW); //行排序
    std::cout << A << std::endl;
    std::cout << B << std::endl;
}

/* 对角线元素总和 */
void PictureDemo::test33()
{
    Mat src = imread("../x64/Debug/picture/1.jpg", IMREAD_REDUCED_COLOR_2);
    auto data = trace(src);
    std::cout << data << std::endl;
}

/* 稀疏变换 */
void PictureDemo::test34()
{
    Point2f triangleA[3];
    Point2f triangleB[3];

    triangleA[0] = Point2f(0,0);
    triangleA[1] = Point2f(100,0);
    triangleA[2] = Point2f(0,50);

    triangleB[0] = Point2f(100,50);
    triangleB[1] = Point2f(200,50);
    triangleB[2] = Point2f(100,100);

    Mat affineMat = getAffineTransform(triangleA, triangleB);
    
    Mat m;
    m.create(3,1,CV_64FC2);
    m.at(0,0)[0] = 50;
    m.at(0,0)[1] = 25;
    m.at(1,0)[0] = 100;
    m.at(1,0)[1] = 25;
    m.at(2,0)[0] = 100;
    m.at(2,0)[1] = 50;

    Mat res;
    res.create(3, 1, CV_64FC2);
    transform(m, res, affineMat);
    std::cout << m << std::endl;
    std::cout << res << std::endl;
    std::cout << affineMat << std::endl;
}

/* 将矩阵沿着对角线进行翻转 */
void PictureDemo::test35()
{
    Mat src = imread("../x64/Debug/picture/1.jpg", IMREAD_REDUCED_COLOR_2);
    Mat dst;
    transpose(src, dst);
    imshow("1", src);
    imshow("22", dst);
} 

/* 编解码 */
void PictureDemo::test36()
{
    Mat src = imread("../x64/Debug/picture/1.jpg", IMREAD_COLOR);

    //编码
    std::vector buf;
    imencode(".jpg", src, buf);

    std::string strEncode(buf.begin(), buf.end());
    std::string strPath = "../x64/Debug/picture/imgencode.txt";
    std::ofstream ofs(strPath, std::ifstream::out);
    assert(ofs.is_open());
    ofs << strEncode;
    ofs.flush();
    ofs.close();

    //解码
    Mat dst;
    dst = imdecode(buf, IMREAD_COLOR);
    imshow("22", dst);
}

你可能感兴趣的:(opencv,人工智能,计算机视觉)