简体   繁体   English

如何在 CImg 库中使用 label() 功能

[英]How to use the label() functionality in CImg library

I'm looking for help on how to use the label() functionality with the CImg library.我正在寻找有关如何将 label() 功能与 CImg 库一起使用的帮助。 What I want to do is simple.我想做的很简单。 I have a white background with big black dots separated from each other and I just want to count them.我有一个白色背景,大黑点彼此分开,我只想数一数。 I think it's possible with label() but I don't understand the parameters of this function.我认为 label() 是可能的,但我不明白这个 function 的参数。 Thank you for the help !感谢您的帮助 !

The library informations': [1]: https://cimg.eu/reference/structcimg__library_1_1CImg.html#aaff4a10071470e4cd255653c5c2f043e图书馆信息:[1]: https://cimg.eu/reference/structcimg__library_1_1CImg.html#aaff4a10071470e4cd255653c5c2f043e

Basically, you pass it an image and it returns another image wherein each group of similar pixels is assigned to the same class, ie it has the same greyscale intensity.基本上,您将一个图像传递给它,它会返回另一个图像,其中每组相似像素都分配给相同的 class,即它具有相同的灰度强度。 So, if we start with this:所以,如果我们从这个开始:

在此处输入图像描述

And run this:运行这个:

#include "CImg.h"
#include <iostream>

using namespace cimg_library;
using namespace std;

int main()
{
    // Create solid white image 
    CImg<unsigned char> img(320,240);
    img.fill(255);

    // Draw some black circles
    unsigned char black[] = {0};
    img.draw_circle(50, 50,30,black);
    img.draw_circle(130,100,50,black);
    img.draw_circle(200,200,35,black);
    img.draw_circle(280,140,30,black);
    img.save("start.png");

    // Label the connected components
    CImg<> labels = img.label(true,64);

    // Save result
    labels.save("result.png");

}

We will get this:我们会得到这个:

在此处输入图像描述

Which is very underwhelming, until we look at the histogram - I am using ImageMagick here:这是非常令人印象深刻的,直到我们查看直方图 - 我在这里使用ImageMagick

identify -verbose result.png

Image:
  Filename: result.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: PseudoClass
  Geometry: 320x240+0+0
  Units: Undefined
  Colorspace: Gray
  Type: Grayscale
  Base type: Undefined
  Endianness: Undefined
  Depth: 8-bit
  Channel depth:
    Gray: 8-bit
  Channel statistics:
    Pixels: 76800
    Gray:
      min: 0  (0)
      max: 4 (0.0156863)
      mean: 0.565234 (0.00221661)
      median: 0 (0)
      standard deviation: 1.13898 (0.00446658)
      kurtosis: 2.3448
      skewness: 1.89382
      entropy: 0.520843
  Colors: 5
  Histogram:
    59036: (0,0,0) #000000 gray(0)      <--- HERE
    2909: (1,1,1) #010101 gray(1)
    8005: (2,2,2) #020202 gray(2)
    2909: (3,3,3) #030303 gray(3)
    3941: (4,4,4) #040404 gray(4)   

And you can see there are 5 different greyscale values, corresponding to 5 components in the input image.你可以看到有 5 个不同的灰度值,对应于输入图像中的 5 个分量。 I can also contrast-stretch the image so you can see the components, each identified with a different intensity:我还可以对图像进行对比拉伸,以便您可以看到组件,每个组件都以不同的强度标识:

magick result.png -auto-level z.png

在此处输入图像描述

The true parameter tells CImg to consider components North-East, South-East South-West and North-West of any pixel as connected. true参数告诉CImg将任何像素的东北部、东南部、西南部和西北部视为连接的组件。 If you set this false , it only considers neighbours North, South, East and West of any pixel to be connected.如果你设置这个false ,它只考虑任何像素的北、南、东和西邻域被连接。

The threshold says how much a pixel may vary from others in its class while still being considered similar enough to be a neighbour - so it's a tolerance on colour matching.阈值表示一个像素在其 class 中可能与其他像素有多少不同,同时仍被认为足够相似以成为邻居 - 所以它是颜色匹配的容差。

Keywords : C++, CImg, image processing, labels, label, connected component analysis, blob analysis.关键词:C++,CImg,图像处理,标签,label,连通分量分析,斑点分析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM