博客
关于我
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/

你可能感兴趣的文章
ngrok | 内网穿透,支持 HTTPS、国内访问、静态域名
查看>>
NHibernate学习[1]
查看>>
NHibernate异常:No persister for的解决办法
查看>>
NIFI1.21.0_Mysql到Mysql增量CDC同步中_日期类型_以及null数据同步处理补充---大数据之Nifi工作笔记0057
查看>>
NIFI1.21.0_Mysql到Mysql增量CDC同步中_补充_更新时如果目标表中不存在记录就改为插入数据_Postgresql_Hbase也适用---大数据之Nifi工作笔记0059
查看>>
NIFI1.21.0_NIFI和hadoop蹦了_200G集群磁盘又满了_Jps看不到进程了_Unable to write in /tmp. Aborting----大数据之Nifi工作笔记0052
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表多表增量同步_增删改数据分发及删除数据实时同步_通过分页解决变更记录过大问题_02----大数据之Nifi工作笔记0054
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表或全表增量同步_实现指定整库同步_或指定数据表同步配置_04---大数据之Nifi工作笔记0056
查看>>
NIFI1.23.2_最新版_性能优化通用_技巧积累_使用NIFI表达式过滤表_随时更新---大数据之Nifi工作笔记0063
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_根据binlog实现数据实时delete同步_实际操作04---大数据之Nifi工作笔记0043
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_配置binlog_使用处理器抓取binlog数据_实际操作01---大数据之Nifi工作笔记0040
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_配置数据路由_实现数据插入数据到目标数据库_实际操作03---大数据之Nifi工作笔记0042
查看>>
NIFI从MySql中离线读取数据再导入到MySql中_03_来吧用NIFI实现_数据分页获取功能---大数据之Nifi工作笔记0038
查看>>
NIFI从MySql中离线读取数据再导入到MySql中_无分页功能_02_转换数据_分割数据_提取JSON数据_替换拼接SQL_添加分页---大数据之Nifi工作笔记0037
查看>>
NIFI从PostGresql中离线读取数据再导入到MySql中_带有数据分页获取功能_不带分页不能用_NIFI资料太少了---大数据之Nifi工作笔记0039
查看>>
nifi使用过程-常见问题-以及入门总结---大数据之Nifi工作笔记0012
查看>>
NIFI分页获取Mysql数据_导入到Hbase中_并可通过phoenix客户端查询_含金量很高的一篇_搞了好久_实际操作05---大数据之Nifi工作笔记0045
查看>>
NIFI同步MySql数据_到SqlServer_错误_驱动程序无法通过使用安全套接字层(SSL)加密与SQL Server_Navicat连接SqlServer---大数据之Nifi工作笔记0047
查看>>
Nifi同步过程中报错create_time字段找不到_实际目标表和源表中没有这个字段---大数据之Nifi工作笔记0066
查看>>
NIFI大数据进阶_FlowFile拓扑_对FlowFile内容和属性的修改删除添加_介绍和描述_以及实际操作---大数据之Nifi工作笔记0023
查看>>