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

你可能感兴趣的文章
php后台“爬虫”模拟登录第三方系统
查看>>
php后台的在控制器中就可以实现阅读数增加
查看>>
php命令行生成项目结构
查看>>
PHP命名空间带来的干扰
查看>>
PHP和MySQL Web开发从新手到高手,第1天-搭建PHP开发环境
查看>>
php商店管理系统,基于PHP的商店管理系统.doc
查看>>
PHP四大主流框架的优缺点总结
查看>>
PHP图片处理—PNG透明缩放并生成灰图
查看>>
php在liunx系统中设置777权限不起作用解决方法
查看>>
PHP基于openssl实现的非对称加密操作
查看>>
php基本符号大全
查看>>
php增删改查封装方法
查看>>
php多条件筛选功能的实现
查看>>
php多线程
查看>>
PHP大数组循环-避免产生Notice或者是Warning
查看>>
PHP大数组过滤元素、修改元素性能分析
查看>>
PHP大文件切片下载代码
查看>>
php如何定义的数位置,php如何实现不借助IDE快速定位行数或者方法定义的文件和位置...
查看>>
RabbitMQ集群 - 普通集群搭建、宕机情况
查看>>
PHP如何读取json数据
查看>>