分类 OpenCV 下的文章

“OpenCV的全称是Open Source Computer Vision Library,是一个跨平台的计算机视觉库。OpenCV是由英特尔公司发起并参与开发,以BSD许可证授权发行。”

对应头文件:#include <opencv2/imgproc.hpp>

参数解释
src:输入图像

dst:输出图像

dsize:输出图像的尺寸,如果传入参数0,则按照以下公式计算

dsize = Size(round(fxsrc.col, fysrc.rows))

fx:水平方向的缩放因子,如果取默认参数0,则按照以下公式计算

(double)dsize.width / src.cols

fy:垂直方向的缩放因子,如果取默认参数0,则按照以下公式计算

(double)dsize.height / src.rows

interpolation:插值方法,对于缩小图片的情况,cv::resize()函数会

默认选择INTER_AREA作为插值方法,对于放大图片的操作,通常最

最好选用cv::INTER_CUBIC(较慢),或者使用快速效果稍弱的

cv::INTER_LINER插值方法

注意:dsize和(fx,fy)参数不能同时取0

利用余弦距离计算图像特征之间的相似度(c++代码实现)
1.利用opencv提取hog特征,然后使用hog特征的特征描述子进行两张图像之间的相似度计算

float* get_hog_feature(cv::Mat img)
{


cv::HOGDescriptor hog = cv::HOGDescriptor(cvSize(20, 20), cvSize(10, 10), cvSize(5, 5), cvSize(5, 5), 9);
cv::resize(img, img, cv::Size(30, 30), (0, 0), (0, 0), cv::INTER_LINEAR);
std::vector<float> descriptors;
// float *descriptors;
hog.compute(img, descriptors, cv::Size(20, 20), cv::Size(0, 0));
float *feature_float = (float*)malloc(descriptors.size() * sizeof(float));
assert(feature_float);
for (int i = 0; i < 128; i++)
{
    feature_float[i] = descriptors[i * 2];
}

return feature_float;

}

bool getRectsHogFeature(const cv::Mat& img, DETECTIONS& d)
{

std::vector<cv::Mat> mats;
int feature_dim = 128;
for (DETECTION_ROW& dbox : d)
{
    cv::Rect rc = cv::Rect(int(dbox.tlwh(0)), int(dbox.tlwh(1)), int(dbox.tlwh(2)), int(dbox.tlwh(3)));
    rc.x = (rc.x >= 0 ? rc.x : 0);
    rc.y = (rc.y >= 0 ? rc.y : 0);
    rc.width = (rc.x + rc.width <= img.cols ? rc.width : (img.cols - rc.x));
    rc.height = (rc.y + rc.height <= img.rows ? rc.height : (img.rows - rc.y));
    cv::Mat mattmp = img(rc).clone();
    //cv::resize(mattmp, mattmp, cv::Size(64, 128));
    
    float *feature_float = get_hog_feature(mattmp);

    for (int i=0;i<feature_dim;i++)
    {
        dbox.feature[i] = feature_float[i];
    }
    
}
return true;

}

利用HOG特征比较两幅图像的相似度

opencv PAI:

CV_WRAP HOGDescriptor(Size _winSize, Size _blockSize, Size _blockStride,

              Size _cellSize, int _nbins, int _derivAperture=1, double _winSigma=-1,
              int _histogramNormType=HOGDescriptor::L2Hys,
              double _L2HysThreshold=0.2, bool _gammaCorrection=false,
              int _nlevels=HOGDescriptor::DEFAULT_NLEVELS, bool _signedGradient=false)
#include<iostream>
#include<vector>
#include "string"
#include "algorithm"
#include"opencv2/opencv.hpp"
 
void GetHOGHistogram(cv::Mat img,std::vector<float>&descriptors)//取得图像img的HOG特征向量
{
    cv::HOGDescriptor *hog=new cv::HOGDescriptor(cvSize(64,128),cvSize(32,32),cvSize(8,8),cvSize(16,16),9);
    hog->compute(img,descriptors,cv::Size(8,8), cv::Size(0,0));
}
int main()
{
    std::string imgDir01 = "/home/.../images/personTestImage/2021-05-14_15-21-06.jpg";
    std::string imgDir02 = "/home/.../images/personTestImage/2021-05-14_15-21-09.jpg";
 
    cv::Mat src01 = cv::imread(imgDir01, 0);
    std::cout<<"src01.size(x,y):"<<src01.cols<<" "<<src01.rows<<std::endl;
    int width=src01.cols;
    int height=src01.rows;
    cv::Mat src02 = cv::imread(imgDir02, 0);
 
    std::cout<<"src02.size(x,y):"<<src02.cols<<" "<<src02.rows<<std::endl;
    cv::Size size=cv::Size(width,height);
    cv::resize(src02, src02,size);
 
    std::vector<float> hogDesc01(0);
    std::vector<float> hogDesc02(0);
    GetHOGHistogram(src01, hogDesc01);
    GetHOGHistogram(src02, hogDesc02);
    std::cout<<hogDesc01.size()<<" "<<hogDesc02.size()<< std::endl;
#if 0
    //欧式距离
    float sum=0;
 
    for (auto i = 0; i < hogDesc01.size(); i++)
    {
        sum+=(hogDesc01[i]-hogDesc02[i])*(hogDesc01[i]-hogDesc02[i]);
    }
 
    sum=std::sqrt(sum);
    std::cout<<sum<<std::endl;
#else
    //余弦距离计算
    assert(hogDesc01.size()==hogDesc02.size());
    float product=0;
    float amp01=0,amp02=0;
    for(size_t i=0;i<hogDesc01.size();i++)
    {
        product+=hogDesc01[i]*hogDesc02[i];
        amp01+=pow(hogDesc01[i],2);
        amp02+=pow(hogDesc02[i],2);
    }
 
    float correlation=product/(sqrt(amp01)* sqrt(amp02));
    std::cout<<correlation<<std::endl;
#endif
 
 
    return 0;
}