0

I have an RGB image in 2D.

I would want to create groups of pixels that have the same color (RGB value); they are read from left to right and then from top to bottom.

When the current pixel has an RGB value different from the previous, it means I found a group (which contains previous pixels).

I know there are the CImg functions CImg_for2x2(img,x,y,z,c,I,T) but the problem is that it works only on the channel c, whereas I'm interested in the RGB value. Doc: http://cimg.eu/reference/group__cimg__loops.html#lo6

Do you know if it's possible to tell "CImg" to understand that I work with RGB value and not only red's value, e.g.?

Vijayanath Viswanathan
  • 8,027
  • 3
  • 25
  • 43
JarsOfJam-Scheduler
  • 2,809
  • 3
  • 31
  • 70

1 Answers1

1

It's hard to tell from your question, but I think you are looking for "Connected Component Analysis", or "labelling".

The CImg tool for that is label().

So, if you start with this image which has 3 white blobs in it:

enter image description here

and then run this:

#include <iostream>
#include "CImg.h"
using namespace std;
using namespace cimg_library;

int main(int argc, char** const argv)
{
    CImg<int> img("input.png");
    img.label(0,0);
    img.save_png("result.png");
}

It will "label" all distinct blobs in the image with a unique number, like this:

enter image description here

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Is the black part identified too ? – JarsOfJam-Scheduler Oct 11 '17 at 07:56
  • 1
    All the black pixels have come out labelled (i.e. coloured) as 0, so they happen to be black. All the pixels in the top-left blob came out with label (i.e. colour) 1. All the pixels in the tall bar on the right came out labelled as 2. All the pixels in the large square came out labelled as 3. I then `normalised()` (i.e. spread the values out over the full scale of 0-255) the image to make 1 become 85, 2 become 170 and 3 become 255. – Mark Setchell Oct 11 '17 at 08:02