与已知图像匹配的局部二值模式 我正在寻找一种使用OpenCV和C++实现局部二进制模式的方法。
目前我发现: 然而,我需要比较两幅图像或LBP直方图,并给出一些相似性指数 这是我修改过的代码:与已知图像匹配的局部二值模式 我正在寻找一种使用OpenCV和C++实现局部二进制模式的方法。,c++,opencv,image-processing,histogram,lbph-algorithm,C++,Opencv,Image Processing,Histogram,Lbph Algorithm,目前我发现: 然而,我需要比较两幅图像或LBP直方图,并给出一些相似性指数 这是我修改过的代码: #include <opencv/cv.h> #include <opencv/highgui.h> #include "lbp.hpp" #include "histogram.hpp" using namespace cv; int main(int argc, const char *argv[]) { int deviceId = 0; i
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include "lbp.hpp"
#include "histogram.hpp"
using namespace cv;
int main(int argc, const char *argv[]) {
int deviceId = 0;
if(argc > 1)
deviceId = atoi(argv[1]);
VideoCapture cap(deviceId);
if(!cap.isOpened()) {
cerr << "Capture Device ID " << deviceId << "cannot be opened." << endl;
return -1;
}
// initial values
int radius = 1;
int neighbors = 8;
// windows
namedWindow("original",CV_WINDOW_AUTOSIZE);
namedWindow("lbp",CV_WINDOW_AUTOSIZE);
// matrices used
Mat test;
Mat test1;
Mat frame; // always references the last frame
Mat dst; // image after preprocessing
Mat dst1;
Mat lbp; // lbp image
Mat lbp1;
// just to switch between possible lbp operators
vector<string> lbp_names;
lbp_names.push_back("Extended LBP"); // 0
lbp_names.push_back("Fixed Sampling LBP"); // 1
lbp_names.push_back("Variance-based LBP"); // 2
int lbp_operator=1;
bool running=true;
while(running) {
//cap >> frame;
dst = imread("Coin1.jpg", CV_LOAD_IMAGE_GRAYSCALE); //Known Image
dst1 = imread("Coin2.jpg", CV_LOAD_IMAGE_GRAYSCALE); //Compared to
switch(lbp_operator) {
case 0:
lbp::ELBP(test, lbp, radius, neighbors); // use the extended operator
break;
case 1:
lbp::OLBP(dst, lbp); // use the original operator
lbp::OLBP(dst1, lbp1); // use the original operator
break;
case 2:
lbp::VARLBP(dst, lbp, radius, neighbors);
break;
}
// now to show the patterns a normalization is necessary
// a simple min-max norm will do the job...
normalize(lbp, lbp, 0, 255, NORM_MINMAX, CV_8UC1);
Mat lbp_hist, lbp1_hist;
int histSize[] = {256};
float s_ranges[] = { 0, 256 };
const float* ranges[] = { s_ranges };
// Use the o-th and 1-st channels
int channels[] = { 0 };
calcHist( &lbp, 1, channels, Mat(), lbp_hist, 1, histSize, ranges, true, false );
normalize( lbp1_hist, lbp1_hist, 0, 1, NORM_MINMAX, -1, Mat() );
calcHist( &lbp1, 1, channels, Mat(), lbp1_hist, 1, histSize, ranges, true, false );
normalize( lbp_hist, lbp_hist, 0, 1, NORM_MINMAX, -1, Mat() );
double base_base = compareHist( lbp_hist, lbp1_hist, 0 );
printf("%f\n",base_base); //get a similarity
//imshow("original", lbp);
//imshow("lbp", lbp1);
imshow("1", lbp_hist);
imshow("2", lbp1_hist);
char key = (char) waitKey(0);;
}
return 0; // success
}
#包括
#包括
#包括“lbp.hpp”
#包括“直方图.hpp”
使用名称空间cv;
int main(int argc,const char*argv[]{
int deviceId=0;
如果(argc>1)
deviceId=atoi(argv[1]);
视频捕获帽(deviceId);
如果(!cap.isOpened()){
cerr我记得在开始使用OpenCV LBPH时遇到了类似的问题
尝试使用此函数获取直方图
void lbp::histogram(const Mat& src, Mat& hist, int numPatterns) {
switch(src.type()) {
case CV_8SC1: histogram_<char>(src, hist, numPatterns); break;
case CV_8UC1: histogram_<unsigned char>(src, hist, numPatterns); break;
case CV_16SC1: histogram_<short int>(src, hist, numPatterns); break;
case CV_16UC1: histogram_<unsigned short>(src, hist, numPatterns); break;
case CV_32SC1: histogram_<int>(src, hist, numPatterns); break;
}
}
template <typename _Tp>
void lbp::histogram_(const Mat& src, Mat& hist, int numPatterns) {
hist = Mat::zeros(1, numPatterns, CV_32SC1);
for(int i = 0; i < src.rows; i++) {
for(int j = 0; j < src.cols; j++) {
int bin = src.at<_Tp>(i,j);
hist.at<int>(0,bin) += 1;
}
}
}
//Manual normalization
cv::Mat hist_norm=cv::Mat::zeros(1,hist.cols,CV_32F);
int sum=0;
for(int j=0;j<hist.cols;j++){sum+=hist.at<int>(0,j);}
for(int j=0;j<hist.cols;j++){hist_norm.at<float>(0,j)+= (float)hist.at<int>(0,j)/(float)sum;}
void lbp::直方图(常量Mat和src、Mat和hist、int numPatterns){
开关(src.type()){
案例CV_8SC1:直方图(src、hist、numPatterns);中断;
案例CV_8UC1:直方图(src、hist、numPatterns);中断;
案例CV_16SC1:直方图(src、hist、numPatterns);中断;
案例CV_16UC1:直方图(src、hist、numPatterns);中断;
案例CV_32SC1:直方图(src、hist、numPatterns);中断;
}
}
模板
无效lbp::直方图(常数矩阵和src、矩阵和历史、整数){
hist=Mat::零(1,numPatterns,CV_32SC1);
对于(int i=0;i 对于(int j=0;jI厌倦了用lbp::histogram(lbp,lbp_hist,2)来称呼它);但是最终得到了一个类似的微小的histogram。你是说直方图的大小像256x1吗?这是正常的。