【问题标题】:Load bin values to densehistogram from float array将 bin 值从浮点数组加载到密集直方图
【发布时间】:2016-04-05 12:25:02
【问题描述】:

我发现了一个类似的问题 emgu Calculate histogram with matrices ...但我错过了一些步骤!

Emgu 3 / c# / Sql Server 2014

我正在尝试将一个图像与存储在 SQL Server 的 varbinary(max) 中的多个图像进行比较。

我的第一步是比较直方图,因为某些图像(对象)可能相似但颜色不同,所以在我的比较算法中,我还想考虑要比较的对象的颜色。

我已经根据此处的其他帖子将一些代码放在一起,如果我根据图像计算直方图,我能够成功地做到这一点:

histBlueSource.Calculate(new Image<Gray, byte>[] { imgBlueSource }, true, null);
histBlueTarget.Calculate(new Image<Gray, byte>[] { imgBlueTarget }, true, null);

double cBlue = CvInvoke.CompareHist(histBlueSource, histBlueTarget, HistogramCompMethod.Correl);

由于从数据库加载图像的性能问题,我想从直方图中提取 BinValues 并将它们保存在 SQL SERVER 数据库中,以便稍后将它们上传回 DenseHistogram。

我成功提取了二进制值:

//** problem may be here - dont know if i should SAVE/LOAD bin value to a 1d FLOAT[256]
float[] BlueHist = new float[256];
BlueHist = histBlue.GetBinValues(); 

我序列化,保存到sql server。我可以从 sql server 读取数据,反序列化并使用我的 bin 值返回到 float[256],与提取的值完全相同。

要将数据加载回密度直方图,我正在这样做:

DenseHistogram histBlue = new DenseHistogram(256, new RangeF(0.0f, 255.0f));
BlueHist = (float[])bformatterBlue.Deserialize(memStreamBlue);
//**Other problem may be here
Matrix<float> mtx = new Matrix<float>(BlueHist);

我觉得奇怪的是矩阵数据是 {float[256,1]}。然后将 binvalues 中的值从 [0,0] 加载到 [255,0]。

当我做最后一步时

histBlue.Calculate(new Matrix<float>[] { mtx }, false, null);

问题是未正确加载直方图 binvalues,只有 [0] 的值为 135,其他 [255] 为 0。

有人可以帮我提出这个或其他建议吗?

【问题讨论】:

    标签: c# histogram emgucv


    【解决方案1】:

    这对我有用....您不能序列化 DenseHistogram,但可以序列化 Mat。然后,您可以将后者的表示形式存储在 SQL Server Varbinary 字段中。

    这里是一些示例序列化代码。在此示例中,我使用的是灰度图像。

    Image<Gray, Byte> croppedImage = sourceImage.Copy();
    
    DenseHistogram hist1 = new DenseHistogram(256, new RangeF(0.0f, 255.0f));
    hist1.Calculate<Byte>(new Image<Gray, byte>[] { croppedImage }, true, null);
    
    Mat matFromHistogram = new Mat(hist1.Size, hist1.Depth, hist1.NumberOfChannels, hist1.DataPointer, hist1.Step);
    
    byte[] histogramBytes;
    
    using (MemoryStream stream = new MemoryStream())
    {
        BinaryFormatter bformatter = new BinaryFormatter();
        bformatter.Serialize(stream, matFromHistogram);
        histogramBytes = stream.GetBuffer();
    }
    

    然后您可以从数据库中反序列化并与 DenseHistogram 进行比较:

    Mat matToCompare;
    
    using (var memStream = new MemoryStream())
    {
        byte[] bytes = face.Histogram;
        BinaryFormatter bformatter = new BinaryFormatter();
        memStream.Write(bytes, 0, bytes.Length);
        memStream.Seek(0, SeekOrigin.Begin);
        matToCompare = bformatter.Deserialize(memStream) as Mat;                                    
    }
    
    distance = CvInvoke.CompareHist(histSource, matToCompare, HistogramCompMethod.Bhattacharyya);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 2013-09-17
      • 2011-08-12
      • 2015-09-10
      • 2015-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多