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

你可能感兴趣的文章
OSPF技术连载16:DR和BDR选举机制,一篇文章搞定!
查看>>
OSPF技术连载17:优化OSPF网络性能利器——被动接口!
查看>>
OSPF技术连载18:OSPF网络类型:非广播、广播、点对多点、点对多点非广播、点对点
查看>>
OSPF技术连载19:深入解析OSPF特殊区域
查看>>
SQL Server 复制 订阅与发布
查看>>
OSPF技术连载20:OSPF 十大LSA类型,太详细了!
查看>>
OSPF技术连载21:OSPF虚链路,现代网络逻辑连接的利器!
查看>>
OSPF技术连载22:OSPF 路径选择 O > O IA > N1 > E1 > N2 > E2
查看>>
OSPF技术连载2:OSPF工作原理、建立邻接关系、路由计算
查看>>
OSPF技术连载5:OSPF 基本配置,含思科、华为、Junifer三厂商配置
查看>>
OSPF技术连载6:OSPF 多区域,近7000字,非常详细!
查看>>
OSPF技术连载7:什么是OSPF带宽?OSPF带宽参考值多少?
查看>>
OSPF技术连载8:OSPF认证:明文认证、MD5认证和SHA-HMAC验证
查看>>
OSPF故障排除技巧
查看>>
spring配置文件中<context:property-placeholder />的使用
查看>>
OSPF有哪些优势?解决了RIP的什么问题?
查看>>
OSPF理论
查看>>
OSPF的七种类型LSA
查看>>
OSPF的安全性考虑:全面解析与最佳实践
查看>>
OSPF知识点大全,网络工程师快速收藏!
查看>>