OpenCV NOTE

OpenCV小记

{% codeblock lang:c++%}
#include <iostream>  
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>  
#include <opencv2/imgproc/imgproc.hpp>  
#include <opencv2/opencv.hpp>
using namespace cv;


Mat A, C;//创建信息头部分
A = imread("1.jpg");//为矩阵开辟内存
C = A;
Mat D(A);//复制构造函数,只复制信息头和矩阵指针
//指向的是同一个数据矩阵
//所以任何一个对象所做的改变也会影响其他对象

Mat D(A, Rect(10, 10, 100, 100));
Mat E = A(Range:all(),Range(1,3));//行和列来界定
//复制矩阵本身
Mat F = A.clone();
Mat G;
A.copyTo(G);
//此时改变GF不会影响Mat信息头指向的矩阵
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>

#define WINDOW_NAME "[线性混合示例]"
using namespace std;
using namespace cv;

const int g_nMaxAlphaValue = 100;//最大值
int g_nAlphaValueSlider;//滑动条对应变量
double g_dAlphaValue;
double g_dBetaValue;
//存储图像 的变量
Mat g_srcImage1;
Mat g_srcImage2;
Mat g_dstImage;
void on_Trackbar(int, void*) {
    g_dAlphaValue = (double)g_nAlphaValueSlider / g_nMaxAlphaValue;
    g_dBetaValue = 1.0 - g_dAlphaValue;

    //根据alpha和beta值进行线性混合
    addWeighted(g_srcImage1, g_dAlphaValue, g_srcImage2, g_dBetaValue, 0.0, g_dstImage);
    imshow(WINDOW_NAME, g_dstImage);

}
int main() {
    g_srcImage1 = imread("1.jpg");
    g_srcImage2 = imread("3.jpg");
    if (!g_srcImage1.data) {
        cout << "something wrong" << endl;
        return -1;
    }
    if (!g_srcImage2.data) {
        cout << "something wrong" << endl;
        return -1;
    }

    g_nAlphaValueSlider = 70;
    cvNamedWindow("WINDOW_NAME", 1);

    char TrackbarName[50];
    sprintf_s(TrackbarName, "透明值 %d", g_nMaxAlphaValue);

    createTrackbar(TrackbarName, WINDOW_NAME, &g_nAlphaValueSlider, g_nMaxAlphaValue, on_Trackbar);

    on_Trackbar(g_nAlphaValueSlider, 0);
    waitKey(0);
    return 0;

}

Mat m1(3, 2, CV_8UC3, Scalar(0, 0, 255));
cout << "m1=" << endl << m1 << endl;
waitKey(0);
return 0;

/**图像的载入显示输出**/

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>

using namespace cv;
int main() {
    Mat girl = imread("1.jpg");
    namedWindow("sdf");
    imshow("sdf", girl);

    Mat image = imread("1.jpg", 199);
    Mat logo = imread("3.jpg");

    namedWindow("原话");

    Mat imageROI;
    imageROI = image(Rect(800, 350, logo.cols, logo.rows));
    //imgROI = image(Range(350,350+logo.rows),Range(800,800+logo.cols));

    addWeighted(image,0.5,logo,0.3,0.,imageROI);
    namedWindow("sd");
    imshow("sd", image);

    waitKey(0);

}

/***imwrite函数的利用**/

#include<opencv2/opencv.hpp>
#include<vector>//?

using namespace cv;
using namespace std;

void creatAlphaMat(Mat & mat) {
    for (int i = 0; i < mat.rows; i++) {
        for (int j = 0; j < mat.cols; ++j) {
            Vec4b & rgba = mat.at<Vec4b>(i,j);
            rgba[0] = UCHAR_MAX;
            rgba[1] = saturate_cast<uchar>((float(mat.cols - j)) / ((float)mat.cols)*UCHAR_MAX);
            rgba[2] = saturate_cast<uchar>((float(mat.rows - i)) / ((float)mat.rows)*UCHAR_MAX);
            rgba[3] = saturate_cast<uchar>(0.5 * (rgba[1] + rgba[2]));

        }
    }

}

int main() {
    Mat mat(480, 640, CV_8UC4);//创建自带alpha通道的mat
    creatAlphaMat(mat);

    vector<int>compression_params;
    compression_params.push_back(IMWRITE_PNG_COMPRESSION);
    compression_params.push_back(9);

    try {
        imwrite("透明.png",mat,compression_params);
        imshow("show",mat);
        fprintf(stdout,"png");
        waitKey(0);

    }
    catch (runtime_error& ex) {
        fprintf(stdout,"wrong");
        return 1;
    }
    return 0;

}

/*视频操作***/

int main() {
    VideoCapture capture("2.mp4");
    Mat edges;
    while (1) {
        Mat frame;//定义mat变量,存储每一帧
        capture >> frame;//读取当前帧//将图像转换为灰度图cvtColor(frame, edges, COLOR_BGR2GRAY);//转化BGR彩色图为灰度图//使用内核降噪blur(edges, edges ,Size(7,7));//进行模糊Canny(edges, edges, 0, 30, 3);imshow("canny后的视频", edges);//经处理后的当前帧if (waitKey(30) >= 0)break;
}
return 0;

}

/*视频操作***/

int main() {
    VideoCapture capture(0);//表示摄像头读取
    while (1) {
        Mat frame;//定义一个MAT变量,用于存储每一帧的图像
        capture >> frame;if (frame.empty()) {break;}imshow("读取视频", frame);waitKey(300);//延时30ms
}
return 0;

}


/*视频操作***/

int main() {
    VideoCapture capture("2.mp4");
    while (1) {
        Mat frame;//定义一个MAT变量,用于存储每一帧的图像
        capture >> frame;if (frame.empty()) {break;}imshow("读取视频", frame);waitKey(300);//延时30ms
}
return 0;

}

/*CANNY边缘检测**/

int main() {
    Mat srcImage = imread("1.jpg");
    imshow("origin", srcImage);
    //转换为灰度图
    Mat edge, grayImage;
    //将图像转换为灰度图像
    cvtColor(srcImage, grayImage,CV_BGR2GRAY);
    //使用10*10内核降噪
    blur(grayImage, edge, Size(10,10));
    //运行canny算子
    Canny(edge, edge, 3, 9, 3);
    imshow("effect", edge);
    waitKey(0);
    return 0;

}

int main() {
Mat srcImage = imread(“1.jpg”);
imshow(“均值滤波”, srcImage);
//均值滤波
Mat dstImage;
blur(srcImage, dstImage, Size(7, 7));
imshow(“均值滤波”, dstImage);
waitKey(0);
return 0;
}

int main() {
Mat srcImage = imread(“1.jpg”);
imshow(“腐蚀操作”, srcImage);//载入原图
//进行腐蚀操作
`

//返回值为指定形状的和尺寸的结构元素,内核矩阵
Mat element = getStructuringElement(MORPH_RECT, Size(5, 50));
Mat dstImage;
erode(srcImage, dstImage, element);
imshow("效果图腐蚀操作", dstImage);
waitKey(0);

return 0;

}

#include <opencv2/opencv.hpp>

#include
using namespace std;
using namespace cv;




```cpp
int main() {

    Point2f p(6,2);
    cout << p << endl;
    Point3f p2(8, 2, 5);
    cout << p2 << endl;

    //基于mat类的std::Vector
    vector<float> v;
    v.push_back(3);
    v.push_back(7);
    v.push_back(6);
    cout << Mat(v) << endl;

    //std::vector 点
    vector<Point2f>point(20);
    for (size_t i = 0; i < point.size(); i++) {
        point[i] = Point2f((float)(i * 5), (float)(i % 7));
    }
    cout << "points=" << point << ";" << endl;
    //Point 类
    Point2i pointd;//Point,Point_<int>,;Point_<float>==Point2f
    pointd.x = 9;
    pointd.y = 9;
    Point pdointd(10,8);

    Rect r(x, y, w, h);
    Point2i p(1,2);
    Point2i j(3, 2);
    float x = p.dot(j);
    p.cross(j);
    p.inside(r);//if point p is inside rect r
    r.contains(p);

    //颜色的表示Scalar类
    //4个元素的数组
    //Scalar(R,G,B);
    //Size_(_Tp_width,_Tp_height)
    //Size(5,5);
    //sz.area();sz.width;sz.height;


​    

​ //Rect类
​ //area()返回矩形的面积,contains(Points)判断点是否在矩形内;inside(Rect)判断矩形是否在矩形内
​ //tl()返回左上角点,br()返回右下角点

​ //Rect rectShift = rect + point;//平移和缩放操作
​ //Rect rectScale = rect + size;//
​ Size sz(5, 7);
​ float theta = 8.9;
​ Point2f pts[4];
​ RotatedRect rr(p,sz,theta);
​ rr.center; rr.size; rr.angle;
​ rr.points(pts[]);//??

​ Matx22d m22d;
​ Matx21f m21f(2, 5);
​ Matx16f m16f;
​ m16f = Matx16f::ones();//zeros(),eye()
​ m16f = Matx16f::randu(3,556);//(min,max)

//Vec<> is Matx<>  whose number of column is one
Vec2s v2s; Vec6d v6d;
//Complexf z1;
//Range(int start ,int end);
Range rng(0,4);//include 0123
Range::all();

//
Ptr<IplImage> img_p = cvLoadImage("1.jpg");
Ptr<IplImage> img_u(cvLoadImage("1.jpg"));

waitKey(0);
return 0;

}

#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv; 

#define window_name "【程序窗口】"
void on_mousehandle(int event, int x, int y, int flags, void*);
void drawrectangle(mat&img, rect box);

rect g_rectangle;
bool g_bdrawingbox = false;
rng g_rng(12345);

int main() {
    g_rectangle = rect(-1, -1, 0, 0);
    mat srcimage(600, 800, cv_8uc3), tempimage;
    srcimage.copyto(tempimage);
    srcimage = scalar::all(0);
    设置鼠标操作的回调函数
    namedwindow(window_name);
    setmousecallback(window_name, on_mousehandle, (void*)&srcimage);
    程序进入主循环,当进行绘制的标识符为真是,进行绘制
    while (1) {
        srcimage.copyto(tempimage);
        进行绘制的标识符为真则进行绘制
        if (g_bdrawingbox)drawrectangle(tempimage, g_rectangle);
        imshow(window_name, tempimage);
        if (waitkey(10) == 27)break;//esc 键退出
    }
    return 0;
}

void on_mousehandle(int event, int x, int y, int flags, void*param) {
    mat & image = *(mat*)param;
    switch (event) {
        鼠标移动
    case event_mousemove: {
        if (g_bdrawingbox)//如果绘制标识符为真
        {
            g_rectangle.width = x - g_rectangle.x;
            g_rectangle.height = y - g_rectangle.y;
        }

}
                      break;
case event_lbuttondown: {
    g_bdrawingbox = true;
    g_rectangle = rect(x, y, 0, 0);//记录起始点
}
                        break;
    左键抬起信息
case event_lbuttonup: {
    g_bdrawingbox = false;//
    if (g_rectangle.width < 0) {
        g_rectangle.x += g_rectangle.width;
        g_rectangle.width *= -1;
    }
    if (g_rectangle.height < 0) {
        g_rectangle.y += g_rectangle.height;
        g_rectangle.height *= -1;
    }
    drawrectangle(image, g_rectangle);
}
                      break;
}

}

自定义绘制矩形的函数

#### void drawrectangle(mat&img, rect box) {
    rectangle(img, box.tl(), box.br(), scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255)));

}


#define window "src"
mat g_srcimage, g_dstimage;
point preciouspoint;
bool p = false;
void on_mouse(int event, int x, int y, int flags, void*);
int main() {
    g_srcimage = imread("1.jpg");
    imshow(window, g_srcimage);
    setmousecallback(window, on_mouse, 0);

waitkey(0);
return 0;

}
void on_mouse(int event, int x, int y, int flags, void*) {
    if (event == event_lbuttondown) {
        preciouspoint = point(x, y);
    }
    else if (event == event_mousemove && (flags&cv_event_flag_lbutton)) {
        point pt(x, y);
        line(g_srcimage, preciouspoint, pt, scalar(255, 0, 0), 2, 5, 0);
        preciouspoint = pt;
        imshow(window, g_srcimage);
    }
}



Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2018-2021 Quincy
  • Visitors: | Views:

请我吃串串呗~

支付宝
微信