image processing – ColorReplace with thresholds and connected components

I would like to replace the colors in this etching (called img) with colors of my choice. (For simplicity, we can assume my desired colors are ColorData("Rainbow")(i) for i = 0, .1, ..., 1.)


I have used PrincipalColors(img) to extract the principal colors and then used

ColorReplace(img, ...)

where the options are a list of associations, where White -> White and each of the remaining principal colors gets mapped to the Rainbow colors. Alas, because (I think) the distributions of colors are far from uniform, and because the edges of each line “blur” colors, the process never works. Each line in the drawing that should be a single color gets split.

I’ve tried ColorQuantize, ConnectedComponents, Erosion, Dilation and EdgeDetection, in various forms, but never quite get what I seek.

I think I’m missing some image processing function that will sharpen each line and ensure each pixel is quantized to the proper color, but just can’t find that function.