I had another attempt at this with the CImg library which is a header-only C++ image processing library that works on all platforms. That is rather nice since there is just one simple header file to copy into your project from the CImg website and you are finished!
See the CImg website here.
The code goes like this:
#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");
}
And you can compile it with this:
g++ peaks.cpp -o peaks -lpng
Sample Output
This shows the x,y coordinates of pixels that exceed your threshold and have no immediate white neighbours.
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