python 中的单像素事件图像处理

Single pixel event image processing in python

我在处理 X 射线源的 CCD 图像时遇到了长期问题,附在此处CCD image 然后在用任意值重新调整后,我需要做的是从图像中减去多像素事件。我还需要计算属于单个像素的像素数。

*- 多像素是指在环绕像素中具有非零值的像素。

我有一个代码通过使用 PIL.Image.open() 将其读入列表并逐像素进行分析!但我正在寻找一个标准的图像处理程序以获得更可靠和更好的结果。如果你能告诉我怎么做,我会很感激。

干杯

您可以使用 ImageMagick 轻松完成此操作,它已安装在大多数 Linux 发行版中并且可用于 OSX 和 Windows。有 C/C++, Python, Perl, PHP, ,Net 和其他可用的绑定。我将在此处的命令行中执行此操作。

所以,这是您的起始图片:

首先,让我们将阈值设为任意 80%:

convert ccd.png -threshold 80% result.png

现在让我们找出周围没有其他白色像素的所有白色像素:

convert ccd.png -threshold 80% -morphology HMT peaks:1.9 result.png

该技术是 "Hit-and-Miss Morphology",在 Anthony Thyssen 的优秀 ImageMagick 使用页面中描述了 here。在该页面上搜索 "Peak" 以找到确切的段落。

现在让我们数一数:

convert ccd.png -threshold 80% -morphology HMT peaks:1.9 -format "%[fx:int(mean*w*h)]" info:
975

让我们检查一下我们删除的多像素事件:

convert ccd.png -threshold 80% \( +clone -morphology HMT peaks:1.9 \) -compose difference -composite result.png

我用 CImg 库进行了另一次尝试,它是一个适用于所有平台的 header-only C++ 图像处理库。这非常好,因为只有一个简单的 header 文件可以从 CImg 网站复制到您的项目中,您就完成了!

查看 CImg 网站 here

代码如下:

#define cimg_use_png
#define cimg_display 0
#include <iostream>
#include "CImg.h"

using namespace cimg_library;
using namespace std;

int main() {

   // Load input image
   CImg<unsigned char> im("ccd.png");
   int w=im.width();
   int h=im.height();

   // Create output image and fill with black (0)
   CImg<unsigned char> result(w,h,1,1,0);

   // Total number of white pixels with 8 black neighbours
   int t=0;

   // Threshold image at 80%
   im.threshold(255*0.8);

   // Find, and count, all white pixels with 8 black neighbours
   // Apply Dirichlet boundary conditions at edges - black virtual pixels at edges
   for(int y=0;y<h;y++){
      for(int x=0;x<w;x++){
         if((im.atXY(x,y,0,0)    !=1) ||
            (im.atXY(x-1,y-1,0,0)==1) || (im.atXY(x,y-1,0,0)==1) || (im.atXY(x+1,y-1,0,0)==1) ||
            (im.atXY(x-1,y,0,0)  ==1) || (im.atXY(x+1,y,0,0)==1) ||
            (im.atXY(x-1,y+1,0,0)==1) || (im.atXY(x,y+1,0,0)==1) || (im.atXY(x+1,y+1,0,0)==1)){
         } else {
            t++;
            // Paint output image white
            result(x,y)=255;
            cout << "DEBUG: " << x << "," << y << endl;
         }
      }
   }
   cout << t << endl;
   result.save_png("result.png");
}

你可以这样编译它:

g++ peaks.cpp -o peaks -lpng

示例输出

这显示超过阈值且没有直接白色邻居的像素的 x,y 坐标。

DEBUG: 172,1
DEBUG: 287,1
DEBUG: 390,1
DEBUG: 396,1
DEBUG: 536,1
DEBUG: 745,1
DEBUG: 956,1
DEBUG: 72,2
DEBUG: 253,2
DEBUG: 516,2
DEBUG: 671,2
DEBUG: 680,2
DEBUG: 740,2
DEBUG: 811,2
DEBUG: 844,2
DEBUG: 228,3
DEBUG: 282,3
DEBUG: 351,3
DEBUG: 505,3
DEBUG: 551,3
DEBUG: 623,3
DEBUG: 638,3
DEBUG: 689,3
...
...
DEBUG: 797,252
DEBUG: 918,252
DEBUG: 125,253
DEBUG: 357,253
DEBUG: 870,253
DEBUG: 252,254
DEBUG: 941,254
1005

首先,我要感谢@Mark Stechell 的提示和回答。然后我通过他关于 Hit-or-miss 转换的暗示找到了我的答案。我在维基百科中找到了很好的信息: https://en.wikipedia.org/wiki/Hit-or-miss_transform

然后在 Python 中有一个适用于此方法的现成函数,它适用于我以及其他模式应用程序。您会在下面 link 中找到所有内容,它简单明了。 http://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.ndimage.morphology.binary_hit_or_miss.html

干杯