opencv 以及 c++ 函数

1. 点旋转

    vector<Point> rot_pt(vector<Point> &v_pt,const cv::Mat &map_matrix)
    {
        //std::cout<<"map_matrix="<<map_matrix<<std::endl;
        float *map = (float *)map_matrix.ptr<float>();

        vector<Point> v_pt2src;
        for(int i=0;i<v_pt.size();i++)
        {
            //std::cout<<"src_pt="<<v_pt[i]<<std::endl;
            int x_t = v_pt[i].x;
            int y_t = v_pt[i].y;
            int x =  map[0]* x_t + map[1] * y_t + map[2];
            int y = map[3] * x_t + map[4] * y_t + map[5];
            v_pt2src.push_back(Point(x,y));
        }
        return v_pt2src;
    }

2.用c++11计算耗时

#include <chrono>
#include <iostream>

int main()
{
    auto t0 = std::chrono::steady_clock::now();

    // Task to do

      std::cout << "consume time="<<std::chrono::duration_cast<std::chrono::milliseconds>
                        (std::chrono::steady_clock::now() - t0).count()<<"ms"<<std::endl;
    return 0;
}

上面是毫秒,也有秒,纳秒

std::chrono::duration_cast<std::chrono::nanoseconds>  //纳秒
std::chrono::duration_cast<std::chrono::microseconds> //微秒
std::chrono::duration_cast<std::chrono::milliseconds> //毫秒
std::chrono::duration_cast<std::chrono::seconds>      //秒

3. c++ opencv直接减均值 除方差

bool sub_mean( cv::Mat &img,cv::Mat &m_out)
{
    img.convertTo(img, CV_32FC3);//这里注意一定要是CV_32FC3,要不然默认是uint,【0-255】,负数直接变为0
    const vector<float> m_v_mean = {104.0,117.0,123.0};
    if(3 != img.channels() || 3 != m_v_mean.size() || img.empty())
    {
        return false;
    }
    cv::Mat m_arr[3];
    cv::split(img,m_arr);
    for(int i=0;i<m_v_mean.size();i++)
    {
        m_arr[i] = m_arr[i] - m_v_mean[i];
    }
    merge(m_arr,3,m_out);
    return true;
}

其实opencv可以一句话搞定!!!!

img.convertTo(img, CV_32FC3);
Mat m_out_2 = img - cv::Scalar(104.0,117.0,123.0);

opencv不支持直接除(除方差) Mat m_out_2 = img / cv::Scalar(104.0,117.0,123.0);
下面代码是opencv 减均值除方差操作

    Mat img = imread("/data_4/everyday/0902/snake/88.png",IMREAD_UNCHANGED);
    Mat img2;
    img.convertTo(img2, CV_32F);
    img2 = img2 / 255.0;
    Mat m_out_2 = img2 - cv::Scalar(0.40789655,0.44719303,0.47026116);
    vector<float> v_std_ = {0.2886383,0.27408165,0.27809834};

    std::vector<cv::Mat> bgrChannels(3);
    cv::split(m_out_2, bgrChannels);
    for(int i=0;i<3;i++)
    {
        bgrChannels[i].convertTo(bgrChannels[i], CV_32FC1, 1.0 / v_std_[i]);
    }
    Mat m_out_3;
    cv::merge(bgrChannels, m_out_3);

4. 去除颜色信息 彩色图转灰度图,灰度图转彩色图

c++版本

 cv::Mat img = cv::imread(img_path);//三通道
 cvtColor(img, img, CV_BGR2GRAY);//单通道
 cvtColor(img, img, CV_GRAY2BGR);//三通道

python 版本

        img = cv2.imread(image_path, cv2.IMREAD_COLOR).astype('float32')
        img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        img=cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

5. opencv imread函数的第二个参数

Mat imread(const string& filename, int flags = 1);
第一个参数filename是我们需要载入图片的路径名。至于第二个参数,表示的是加载的图像是什么类型,可以看到它自带的默认值为1。至于具体有多少种取值,各个取值代表什么类型,我网上找了好多资料都不尽相同。经过我在vs下亲自验证,总结一下几种参数值:
CV_LOAD_IMAGE_UNCHANGED = -1(含<-1的整数)—— 在每个通道中,每个像素的位深为8 bit,通道数(颜色)保持不变;
CV_LOAD_IMAGE_GRAYSCALE = 0 ——位深为8bit,通道数 = 1(灰度图);
CV_LOAD_IMAGE_COLOR = 1(含其余>1整数)——位深 = ?(不确定),通道数 = 3(BGR图);
CV_LOAD_IMAGE_ANYDEPTH = 2 —— 位深不变,通道数 = ?(在VS中测试为1,灰度图);
CV_LOAD_IMAGE_ANYCOLOR = 4 —— 位深 = ?, 通道数不变。

默认是1即CV_LOAD_IMAGE_COLOR,即不管什么,都以三通道彩色图读取,即使你图片是单通道的灰度图也整成三通道。要保持原图不变读取,需要加参数-1,即CV_LOAD_IMAGE_UNCHANGED = -1

6.opencv 图片保存视频

 cv::VideoWriter writer("/data_2/everyday/0804/00.avi", CV_FOURCC('D','I','V','X'), 30.0, cv::Size(1125,589));
 writer<<img;

注意,img的size需要一致!!!!! 和定义VideoWriter的时候需要一样

opencv roi 贴图 img2.copyTo(img(rt));

//img2是roi图, rt和roi大小需要一样!!!!

int main()
{
    Mat img = imread("/data_2/fengjing.jpeg");
    Mat img_src = img.clone();
    Mat img2 = imread("/data_2/dog.jpg");
    Rect rt = Rect(100,100,560,553);
    img2.copyTo(img(rt)); //img2是roi图, rt和roi大小需要一样!!!!

    namedWindow("src",0);
    imshow("src",img_src);
    namedWindow("roi",0);
    imshow("roi",img2);
    namedWindow("merge",0);
    imshow("merge",img);
    waitKey(0);
}

图片保持长宽比,缩放到固定尺寸

    int input_w = 512;
    int input_h = 512;
    float scale = cv::min(float(input_w)/img.cols,float(input_h)/img.rows);
    auto scaleSize = cv::Size(img.cols * scale,img.rows * scale);

    cv::Mat resized;
    cv::resize(img, resized,scaleSize,0,0);

    cv::Mat cropped = cv::Mat::zeros(input_h,input_w,CV_8UC3);
    cv::Rect rect((input_w- scaleSize.width)/2, (input_h-scaleSize.height)/2, scaleSize.width,scaleSize.height);

    resized.copyTo(cropped(rect));

c++读取路径下所有文件路径

#include <dirent.h>

static inline int read_files_in_dir(const char *p_dir_name, std::vector<std::string> &file_names) {
    DIR *p_dir = opendir(p_dir_name);
    if (p_dir == nullptr) {
        return -1;
    }

    struct dirent* p_file = nullptr;
    while ((p_file = readdir(p_dir)) != nullptr) {
        if (strcmp(p_file->d_name, ".") != 0 &&
            strcmp(p_file->d_name, "..") != 0) {
            //std::string cur_file_name(p_dir_name);
            //cur_file_name += "/";
            //cur_file_name += p_file->d_name;
            std::string cur_file_name(p_file->d_name);
            file_names.push_back(cur_file_name);
        }
    }

    closedir(p_dir);
    return 0;
}

caffe 打印blob

template <typename Dtype>
void prit_blob(Blob<Dtype>* blob_tmp)
{
  std::cout<<"-------------------------start print blob---------------------------"<<std::endl;
  std::cout<<"bottom shape="<<blob_tmp->shape_string()<<std::endl;
   Dtype * p = blob_tmp->mutable_cpu_data();
   for(int i=0;i<blob_tmp->count();i++)
   {
     std::cout<<p[i]<<std::endl;
   }

  std::cout<<"-------------------------end print blob---------------------------"<<std::endl;
}
posted @ 2019-12-19 13:36  无左无右  阅读(1707)  评论(0编辑  收藏  举报