博客
关于我
5.11 用颜色和关键点对斑点进行分类
阅读量:700 次
发布时间:2019-03-21

本文共 4392 字,大约阅读时间需要 14 分钟。

斑点分类器是一个基于直方图和关键点的多分类器,该分类器能够根据输入图像中的斑点生成相应的分类结果。该分类器通过计算输入图像中的特征描述符,并与已知描述符进行比较,来确定图像所属的类别。

常量定义

#include "BlobClassifier.hpp"#include 
#include
#ifdef WITH_OPENCV_CONTRIB#include
#endifconst int HISTOGRAM_NUM_BINS_PER_CHANNEL = 32;const int HISTOGRAM_COMPARISON_METHOD = cv::HISTCMP_CHISQR_ALT;const float HISTOGRAM_DISTANCE_WEIGHT = 0.98f;const float KEYPOINT_MATCHING_DISTANCE_WEIGHT = 1.0f - HISTOGRAM_DISTANCE_WEIGHT;

构造函数

BlobClassifier::BlobClassifier() : clahe(cv::createCLAHE()) {    #ifdef WITH_OPENCV_CONTRIB        , featureDetectorAndDescriptorExtractor(cv::xfeatures2d::SURF::create())        , descriptorMatcher(cv::DescriptorMatcher::create("FlannBased"))    #else        , featureDetectorAndDescriptorExtractor(cv::ORB::create())        , descriptorMatcher(cv::DescriptorMatcher::create("BruteForce-HammingLUT"))    #endif}

更新方法

void BlobClassifier::update(const Blob &referenceBlob) {    referenceBlobDescriptors.push_back(createBlobDescriptor(referenceBlob));}

清空方法

void BlobClassifier::clear() {    referenceBlobDescriptors.clear();}

分类方法

void BlobClassifier::classify(Blob &detectedBlob) const {    BlobDescriptor detectedBlobDescriptor = createBlobDescriptor(detectedBlob);    float bestDistance = FLT_MAX;    uint32_t bestLabel = 0;    for (const BlobDescriptor &referenceBlobDescriptor : referenceBlobDescriptors) {        float distance = findDistance(detectedBlobDescriptor, referenceBlobDescriptor);        if (distance < bestDistance) {            bestDistance = distance;            bestLabel = referenceBlobDescriptor.getLabel();        }    }    detectedBlob.setLabel(bestLabel);}

创建描述符辅助方法

BlobDescriptor BlobClassifier::createBlobDescriptor(const Blob &blob) const {    const cv::Mat &mat = blob.getMat();    int numChannels = mat.channels();    // Calculate the histogram of the blob's image.    cv::Mat histogram;    int channels[] = {0, 1, 2};    int numBins[] = {HISTOGRAM_NUM_BINS_PER_CHANNEL, HISTOGRAM_NUM_BINS_PER_CHANNEL, HISTOGRAM_NUM_BINS_PER_CHANNEL};    float range[] = {0.0f, 256.0f};    const float *ranges[] = {range, range, range};    cv::calcHist(&mat, 1, channels, cv::Mat(), histogram, 3, numBins, ranges);    // Normalize the histogram.    histogram *= (1.0f / (mat.rows * mat.cols));    // Convert the blob's image to grayscale.    cv::Mat grayMat;    switch (numChannels) {        case 4:            cv::cvtColor(mat, grayMat, cv::COLOR_BGRA2GRAY);            break;        default:            cv::cvtColor(mat, grayMat, cv::COLOR_BGR2GRAY);            break;    }    // Detect features in the grayscale image.    std::vector
keypoints; featureDetectorAndDescriptorExtractor->detect(grayMat, keypoints); // Extract descriptors of the features. cv::Mat keypointDescriptors; featureDetectorAndDescriptorExtractor->compute(grayMat, keypoints, keypointDescriptors); return BlobDescriptor(histogram, keypointDescriptors, blob.getLabel());}

计算距离辅助方法

float BlobClassifier::findDistance(const BlobDescriptor &detectedBlobDescriptor, const BlobDescriptor &referenceBlobDescriptor) const {    float histogramDistance = (float)cv::compareHist(detectedBlobDescriptor.getNormalizedHistogram(), referenceBlobDescriptor.getNormalizedHistogram(), HISTOGRAM_COMPARISON_METHOD);    float keypointMatchingDistance = 0.0f;    std::vector
keypointMatches; descriptorMatcher->match(detectedBlobDescriptor.getKeypointDescriptors(), referenceBlobDescriptor.getKeypointDescriptors(), keypointMatches); for (const cv::dmatch &keypointMatch : keypointMatches) { keypointMatchingDistance += keypointMatch.distance; } return histogramDistance * HISTOGRAM_DISTANCE_WEIGHT + keypointMatchingDistance * KEYPOINT_MATCHING_DISTANCE_WEIGHT;}

BlobDescriptor类

#include "BlobDescriptor.hpp"BlobDescriptor::BlobDescriptor(const cv::Mat &normalizedHistogram, const cv::Mat &keypointDescriptors, uint32_t label) : normalizedHistogram(normalizedHistogram), keypointDescriptors(keypointDescriptors), label(label) {}const cv::Mat &BlobDescriptor::getNormalizedHistogram() const {    return normalizedHistogram;}const cv::Mat &BlobDescriptor::getKeypointDescriptors() const {    return keypointDescriptors;}uint32_t BlobDescriptor::getLabel() const {    return label;}

该分类器通过将图像转换为灰度并提取特征来进行分类。特征包括颜色直方图和关键点描述符。直方图和关键点的匹配距离结合计算,用于最终分类结果。代码采用了不同的特征检测算法(如SURF或ORB)以及相应的描述符匹配方法,以适应不同的性能需求。

转载地址:http://lqxez.baihongyu.com/

你可能感兴趣的文章
mysql命令:set sql_log_bin=on/off
查看>>
mySQL和Hive的区别
查看>>
MySQL和Java数据类型对应
查看>>
mysql和oorcale日期区间查询【含左右区间问题】
查看>>
MYSQL和ORACLE的一些操作区别
查看>>
mysql和redis之间互相备份
查看>>
MySQL和SQL入门
查看>>
mysql在centos下用命令批量导入报错_Variable ‘character_set_client‘ can‘t be set to the value of ‘---linux工作笔记042
查看>>
Mysql在Linux运行时新增配置文件提示:World-wrirable config file ‘/etc/mysql/conf.d/my.cnf‘ is ignored 权限过高导致
查看>>
Mysql在Windows上离线安装与配置
查看>>
MySQL在渗透测试中的应用
查看>>
Mysql在离线安装时启动失败:mysql服务无法启动,服务没有报告任何错误
查看>>
Mysql在离线安装时提示:error: Found option without preceding group in config file
查看>>
MySQL基于SSL的主从复制
查看>>
Mysql基本操作
查看>>
mysql基本操作
查看>>
mysql基本知识点梳理和查询优化
查看>>
mysql基础
查看>>
Mysql基础 —— 数据基础操作
查看>>
mysql基础---mysql查询机制
查看>>