常用的OpenCV操作

1、库(Linux+QT):

LIBS+=/usr/local/lib/libopencv_highgui.so \
      /usr/local/lib/libopencv_core.so    \
      /usr/local/lib/libopencv_imgproc.so \
      /usr/local/lib/libopencv_imgcodecs.so

2、读取图片:

cv::imread(std::string imgPath,cv::Mat img)

3、显示:

cv::imshow(std::string DialogName,cv::Mat img)

4、等待:

cv::waitKey(int time)

5、保存:

cv::imwrite(std::string fileName,cv::Mat img)

6、缩放:

cv::resize(cv::Mat src, cv::Mat dst, cv::Size dsize, double fx=0, double fy=0, int interpolation=cv::INTER_LINEAR );
   cv::Mat src   原图    
   cv::Mat dst  缩放后的图
   cv::Size dsize    为0,则按照dsize = Size(round(fx*src.cols), round(fy*src.rows))(round函数double取整),不为0则按此尺寸
   fx,fy  长宽缩放比
   int interpolation  插值方式,默认cv::INTER_LINEAR

7、播放视频:

cv::VideoCapture cap(std::string videoFile);
            if (!cap.isOpened()) {
                std::cout << "Failed to open video: " << file;
            return;
            }
            cv::Mat frame;
           //获取帧长
            long frame_num=static_cast(cap.get(CV_CAP_PROP_FRAME_COUNT));
            //获取当前视频帧率
            double rate = cap.get(CV_CAP_PROP_FPS);
           //每一帧之间的延时,与视频的帧率相对应
            int delay = 1000/rate;
            while(true){
            //设置到frame_pos
            cap.set(CV_CAP_PROP_POS_FRAMES,int frame_pos);
            if(!cap.read(frame))//cap>>frame;
                break;
            cv::imshow(DialogName,frame);
            //任意键停止
            if(cv::waitKey(delay)>0) // cv::waitKey(delay)=='q' //按‘q'停止
                cap.release();
                break;
            }
if (cap.isOpened()) {
                cap.release();
}

8、调用摄像头

cv::VideoCapture cap;
    cap.open(0);
    if(!cap.isOpened())
        return;
    cv::Mat frame;
    while(1)
    {
        cap>>frame;
        if(waitKey(15)>0)
            break;
    }
if (cap.isOpened())
    cap.release();

9、截取ROI

//从src截取roi到dst
cv::Mat src(cv::Rect roi).copyTo(cv::Mat dst);

10、画矩形

cv::rectangle(cv::Mat img, Rect rec,cv::Scalar(0, 0, 255), 2, 8, 0);

11、添加文字

cv::putText( cv::Mat img,std::string text, cv::Point org,
                         int fontFace, double fontScale, Scalar color,
                         int thickness = 1, int lineType = LINE_8,
                         bool bottomLeftOrigin = false );

附所有的OpenCV库(每台电脑可能不一样的):

/usr/local/lib/libopencv_aruco.so \
/usr/local/lib/libopencv_aruco.so\
/usr/local/lib/libopencv_bgsegm.so\
/usr/local/lib/libopencv_bioinspired.so\
/usr/local/lib/libopencv_calib3d.so\
/usr/local/lib/libopencv_ccalib.so\
/usr/local/lib/libopencv_core.so\
/usr/local/lib/libopencv_cudaarithm.so\
/usr/local/lib/libopencv_cudabgsegm.so\
/usr/local/lib/libopencv_cudacodec.so\
/usr/local/lib/libopencv_cudafeatures2d.so\
/usr/local/lib/libopencv_cudafilters.so\
/usr/local/lib/libopencv_cudaimgproc.so\
/usr/local/lib/libopencv_cudalegacy.so\
/usr/local/lib/libopencv_cudaobjdetect.so\
/usr/local/lib/libopencv_cudaoptflow.so\
/usr/local/lib/libopencv_cudastereo.so\
/usr/local/lib/libopencv_cudawarping.so\
/usr/local/lib/libopencv_cudev.so\
/usr/local/lib/libopencv_datasets.so\
/usr/local/lib/libopencv_dnn.so\
/usr/local/lib/libopencv_dpm.so\
/usr/local/lib/libopencv_face.so\
/usr/local/lib/libopencv_face.so.3.1\
/usr/local/lib/libopencv_features2d.so\
/usr/local/lib/libopencv_flann.so\
/usr/local/lib/libopencv_fuzzy.so\
/usr/local/lib/libopencv_hdf.so\
/usr/local/lib/libopencv_highgui.so\
/usr/local/lib/libopencv_imgcodecs.so\
/usr/local/lib/libopencv_imgproc.so\
/usr/local/lib/libopencv_line_descriptor.so\
/usr/local/lib/libopencv_ml.so\
/usr/local/lib/libopencv_objdetect.so\
/usr/local/lib/libopencv_optflow.so\
/usr/local/lib/libopencv_photo.so\
/usr/local/lib/libopencv_plot.so\
/usr/local/lib/libopencv_reg.so\
/usr/local/lib/libopencv_rgbd.so\
/usr/local/lib/libopencv_saliency.so\
/usr/local/lib/libopencv_shape.so\
/usr/local/lib/libopencv_stereo.so\
/usr/local/lib/libopencv_stitching.so\
/usr/local/lib/libopencv_structured_light.so\
/usr/local/lib/libopencv_superres.so\
/usr/local/lib/libopencv_surface_matching.so\
/usr/local/lib/libopencv_text.so\
/usr/local/lib/libopencv_tracking.so\
/usr/local/lib/libopencv_videoio.so\
/usr/local/lib/libopencv_video.so\
/usr/local/lib/libopencv_videostab.so\
/usr/local/lib/libopencv_xfeatures2d.so\
/usr/local/lib/libopencv_ximgproc.so\
/usr/local/lib/libopencv_xobjdetect.so\
/usr/local/lib/libopencv_xphoto.so\

你可能感兴趣的:(常用的OpenCV操作)