opencv基本函数的使用

opencv基本函数的使用

打开一张图片并且显示

代码展示:

void image()
{
    string path = "test.jpeg";
    Mat image = imread(path);
    imshow("image",image);
    waitKey(0);
}

效果演示:

opencv基本函数的使用_第1张图片

打开一个视频

代码展示:

void video()
{
    string path = "test.mp4";
    VideoCapture capture(path);
    Mat image;
    while(true)
    {
        bool is = capture.read(image);
        if(!is)
        {
            break;
        }
        imshow("image",image);
        waitKey(15);
    }
}

效果演示:

opencv基本函数的使用_第2张图片

打开摄像头

代码展示:

void camera()
{
    VideoCapture capture(0);
    Mat image;
    while(true)
    {
        bool is = capture.read(image);
        if(!is)
        {
            break;
        }
        imshow("image",image);
        waitKey(15);
    }
}

效果演示:

暂不演示本人帅脸

基本的图像处理–灰显,高斯滤波,边缘检测,膨胀和腐蚀

代码展示:

void image_deal()
{
    string path = "test.jpeg";
    Mat image = imread(path);
    Mat imageGray;
    Mat imageBlur;
    Mat imageCanny;
    Mat imageDia;
    Mat imageErode;
    cvtColor(image,imageGray,COLOR_BGR2GRAY);

    GaussianBlur(image,imageBlur,Size(7,7),5,0);
    Canny(imageBlur,imageCanny,25,75);

    Mat kernel = getStructuringElement(MORPH_RECT, Size(2,2));
    dilate(imageCanny,imageDia,kernel);
    erode(imageDia,imageErode,kernel);
    imshow("image",image);
    imshow("imageGray",imageGray);
    imshow("imageBlur",imageBlur);
    imshow("imageCanny",imageCanny);
    imshow("imageDia",imageDia);
    imshow("imageErode",imageErode);
    waitKey(0);
}

效果演示:

opencv基本函数的使用_第3张图片

图像Resize和裁剪

代码展示:

void image_resize_crop()
{
    string path = "test.jpeg";
    Mat image = imread(path);
    Mat imageResize;
    Mat imageCrop;
    resize(image,imageResize,Size(),0.5,0.5);
    Rect roi(100,100,300,300);
    imageCrop = image(roi);
    imshow("imageResize",imageResize);
    imshow("imageCrop",imageCrop);
    waitKey(0);
}

效果演示:

opencv基本函数的使用_第4张图片

绘制基本体

代码展示:

void draw()
{
    Mat img(512,512,CV_8UC3, Scalar(255,255,255));
    circle(img,Point(256,256),155,Scalar(0,69,255),FILLED);
    rectangle(img,Point(130,226),Point(382,286),Scalar(255,255,255),FILLED);
    line(img,Point(130,296),Point(382,296),Scalar(255,255,255),2);
    putText(img,"Hello World!",Point(137,262),FONT_HERSHEY_DUPLEX,0.75,Scalar(0,69,255),2);
    imshow("image",img);
    waitKey(0);
}

效果演示:

opencv基本函数的使用_第5张图片

透视效果转换

代码展示:

void draw()
{
    Mat img(512,512,CV_8UC3, Scalar(255,255,255));
    circle(img,Point(256,256),155,Scalar(0,69,255),FILLED);
    rectangle(img,Point(130,226),Point(382,286),Scalar(255,255,255),FILLED);
    line(img,Point(130,296),Point(382,296),Scalar(255,255,255),2);
    putText(img,"Hello World!",Point(137,262),FONT_HERSHEY_DUPLEX,0.75,Scalar(0,69,255),2);
    imshow("image",img);
    waitKey(0);
}
void warp()
{
    float w = 250, h = 350;
    Mat matrix, imgWrap;
    string path = "111.png";
    Mat image = imread(path);

    Point2f src[4] =
    {
              {259,63},
              {377,88},
              {199,183},
              {330,215}
    };
    Point2f dst[4] =
    {
              {0.0f,0.0f},
              {w,0.0f},
              {0.0f,h},
              {w,h},
    };
    matrix = getPerspectiveTransform(src,dst);
    warpPerspective(image,imgWrap,matrix,Point(w,h));

    for(int i = 0; i < 4; i++)
    {
        circle(image,src[i],5,Scalar(0,69,255),FILLED);
    }
    imshow("image",image);
    imshow("imgWrap",imgWrap);
    waitKey(0);
}

效果演示:

opencv基本函数的使用_第6张图片

颜色转换

代码展示:

void colors_in_image()
{
    string path = "test.jpeg";
    Mat image = imread(path);
    Mat imgHSV;
    Mat mask;
    int hmin = 0;
    int smin = 110;
    int vmin = 153;;
    int hmax = 19;
    int smax = 240;
    int vmax = 255;;
    cvtColor(image,imgHSV,COLOR_BGR2HSV);
    namedWindow("track",(640,200));
    createTrackbar("turbo","track",&hmin,179);

    while(true)
    {
        Scalar lower(hmin,smin,vmin);
        Scalar upper(hmax,smax,vmax);
        inRange(imgHSV,lower,upper,mask);

        imshow("image",image);
        imshow("imgHSV",imgHSV);
        imshow("mask",mask);
        waitKey(1);
    }
}

效果演示:

opencv基本函数的使用_第7张图片

轮廓提取

代码展示:

void image_contours()
{
    string path = "test.jpeg";
    Mat image = imread(path);
    Mat imageGray;
    Mat imageBlur;
    Mat imageCanny;
    Mat imageDia;
    Mat imageErode;

    // preprocessing
    cvtColor(image,imageGray,COLOR_BGR2GRAY);
    GaussianBlur(image,imageBlur,Size(7,7),5,0);
    Canny(imageBlur,imageCanny,25,75);
    Mat kernel = getStructuringElement(MORPH_RECT, Size(2,2));
    dilate(imageCanny,imageDia,kernel);

    getContours(imageDia, image);

    imshow("image",image);
    waitKey(0);
}

效果演示:

opencv基本函数的使用_第8张图片

图像人脸检测

代码展示:

void face_in_image()
{
    string path = "test.jpeg";
    Mat image = imread(path);
    CascadeClassifier faceCascade;
    faceCascade.load("haarcascade_frontalface_default.xml");
    if(faceCascade.empty())
    {
        cerr << "XML load failed!" << endl;
    }
    vector<Rect> faces;
    faceCascade.detectMultiScale(image,faces,1.1,10);
    for(int i = 0; i < faces.size(); i++)
    {
        rectangle(image,faces[i].tl(),faces[i].br(),Scalar(255,0,255),3);
    }
    imshow("image",image);
    waitKey(0);
}

效果演示:

opencv基本函数的使用_第9张图片

视频人脸检测

代码展示:

CascadeClassifier faceCascade;
vector<Rect> image_resize(const Mat &image)
{
    Size ResImgSiz = Size(image.cols*0.5, image.rows*0.5);
    Mat ResImg = Mat(ResImgSiz, image.type());
    resize(image, ResImg, ResImgSiz, INTER_CUBIC);
    vector<Rect> faces;
    faceCascade.detectMultiScale(ResImg,faces,1.1,10);
    return faces;
}

void face_by_video()
{
    string path = "test.mp4";
    VideoCapture capture(path);
    Mat image;
    faceCascade.load("haarcascade_frontalface_default.xml");
    if(faceCascade.empty())
    {
        cerr << "XML load failed!" << endl;
        return ;
    }
    while(true)
    {
        bool is = capture.read(image);
        if(!is)
        {
            break;
        }

        vector<Rect> faces = image_resize(image);
        for(int i = 0; i < faces.size(); i++)
        {
            rectangle(image,Point (faces[i].tl().x*2,faces[i].tl().y*2),Point (faces[i].br().x*2,faces[i].br().y*2),Scalar(255,0,255),3);
        }
        imshow("image",image);
        waitKey(10);
    }
}

效果演示:

opencv基本函数的使用_第10张图片

你可能感兴趣的:(OpenCV,opencv,计算机视觉,图像处理)