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

你可能感兴趣的文章
Openlayers实战:自定义版权属性信息
查看>>
Openlayers实战:输入WKT数据,输出GML、Polyline、GeoJSON格式数据
查看>>
Openlayers实战:选择feature,列表滑动,定位到相应的列表位置
查看>>
Openlayers实战:非4326,3857的投影
查看>>
Openlayers高级交互(1/20): 控制功能综合展示(版权、坐标显示、放缩、比例尺、测量等)
查看>>
Openlayers高级交互(10/20):绘制矩形,截取对应部分的地图并保存
查看>>
Openlayers高级交互(11/20):显示带箭头的线段轨迹,箭头居中
查看>>
Openlayers高级交互(12/20):利用高德逆地理编码,点击位置,显示坐标和地址
查看>>
Openlayers高级交互(13/20):选择左右两部分的地图内容,横向卷帘
查看>>
Openlayers高级交互(14/20):汽车移动轨迹动画(开始、暂停、结束)
查看>>
Openlayers高级交互(15/20):显示海量多边形,10ms加载完成
查看>>
Openlayers高级交互(16/20):两个多边形的交集、差集、并集处理
查看>>
Openlayers高级交互(17/20):通过坐标显示多边形,计算出最大幅宽
查看>>
Openlayers高级交互(18/20):根据feature,将图形适配到最可视化窗口
查看>>
Openlayers高级交互(19/20): 地图上点击某处,列表中显示对应位置
查看>>
Openlayers高级交互(2/20):清除所有图层的有效方法
查看>>
Openlayers高级交互(20/20):超级数据聚合,页面不再混乱
查看>>
Openlayers高级交互(3/20):动态添加 layer 到 layerGroup,并动态删除
查看>>
Openlayers高级交互(4/20):手绘多边形,导出KML文件,可以自定义name和style
查看>>
Openlayers高级交互(5/20):右键点击,获取该点下多个图层的feature信息
查看>>