How can I segment areas, each of which has nearly same colors of pixels in an image?

For clarity, Nearly same colors of pixels belong to a category. Another nearly same colors of pixels belong to another category. The distinction depends on nearly same RGB values of pixels.