7
$\begingroup$

At first I did this where image is a grayscale image and example is a colored image

    BasicColorizer[image_, example_] := 
     Module[{grayed, pairs, clustered, rules, default}, 
      grayed = ColorConvert[example, "Grayscale"];
      default = RandomChoice[Commonest[Flatten[ImageData[example], 1]]];
      pairs = 
       Flatten[MapThread[List, {ImageData[grayed], ImageData[example]}, 
         2], 1];
      clustered = GatherBy[pairs, Round[100 #[[1]]] &];
      rules = 
       Append[Round[100 #[[1, 1]]] -> 
           RandomChoice[Commonest[#[[All, 2]]]] & /@ clustered, _ -> 
         default];
      Image[Replace[
        Round[100 ImageData[ColorConvert[image, "Grayscale"]]], 
        rules, {2}]]]

And then

    limg = First@ColorSeparate[img, "LAB"]
    {lref, aref, bref} = ColorSeparate[ref, "RGB"]

    l2 = HistogramTransform[limg, lref]
    ImageHistogram /@ {l2, lref}

    radius = 2
    {neighimg, neighref} = ColorCombine[{
         MeanFilter[#, radius],
         StandardDeviationFilter[#, radius]}] & /@ {l2, lref}
    nfun = Nearest[
      Flatten[ImageData@neighref, 1] -> 
       Transpose[Flatten@ImageData@# & /@ {aref, bref}]]

    col = Map[First@nfun[#, 1] &, ImageData@neighimg, {2}];
    ColorConvert[
     Image[Join[{ImageData@limg}, Transpose[col, {2, 3, 1}]], 
      Interleaving -> False, ColorSpace -> "RGB"], "LAB"]

 Import["http://fc06.deviantart.net/fs71/f/2012/047/6/b/saint_basil__s_cathedral_by_tomdal-d4pwlwo.jpg"]

This is a grayscale image and after that

Import["http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/2012/3/14/1331719456752/St.-Basils-Cathedral-001.jpg"]

This is colored image.

After that I find Corresponding Points between this to images.

Every object from grayscale image and colored images after colorization should have the same color.

And to do this I choose the same images from net one grayscale and other colored. Now how can I transfer the colors from colored image into grayscale?

I tried doing it with several methods but I encountered some colorization errors. What can I do?

$\endgroup$
  • $\begingroup$ Please show the code and results of some of the "several methods" you tried. $\endgroup$ – Rahul Dec 14 '14 at 12:25
  • $\begingroup$ i did this img=[],ref=[](img is my gray image and ref is colored),HistogramTransform[img, ref],HistogramTransform[ColorConvert[img, "LAB"], ColorConvert[ref, "LAB"]] $\endgroup$ – Narine Dec 14 '14 at 12:34
  • $\begingroup$ img=[],ref=[],limg = First@ColorSeparate[img, "LAB"] {lref, aref, bref} = ColorSeparate[ref, "RGB"],l2 = HistogramTransform[limg, lref] ImageHistogram /@ {l2, lref},radius = 2 {neighimg, neighref} = ColorCombine[{ MeanFilter[#, radius], StandardDeviationFilter[#, radius]}] & /@ {l2, lref} nfun = Nearest[ Flatten[ImageData@neighref, 1] -> Transpose[Flatten@ImageData@# & /@ {aref, bref}]],col = Map[First@nfun[#, 1] &, ImageData@neighimg, {2}]; ColorConvert[ Image[Join[{ImageData@limg}, Transpose[col, {2, 3, 1}]], Interleaving -> False, ColorSpace -> "RGB"], "LAB"] $\endgroup$ – Narine Dec 14 '14 at 12:35
  • $\begingroup$ also something like this BasicColorizer[image_,example_]:= Module[{grayed, pairs, clustered, rules, default}, grayed = ColorConvert[example, "Grayscale"]; default = RandomChoice[Commonest[Flatten[ImageData[example], 1]]]; pairs = Flatten[MapThread[List, {ImageData[grayed], ImageData[example]}, 2], 1]; clustered = GatherBy[pairs, Round[100 #[[1]]] &]; rules = Append[Round[100 #[[1, 1]]] -> RandomChoice[Commonest[#[[All, 2]]]] & /@ clustered, _ -> default]; Image[Replace[ Round[100 ImageData[ColorConvert[image, "Grayscale"]]], rules, {2}]]] $\endgroup$ – Narine Dec 14 '14 at 12:37
  • $\begingroup$ example is colored image, and image is grayscale image $\endgroup$ – Narine Dec 14 '14 at 12:39
11
$\begingroup$

Using the images in the duplicate question,

grey = Import["hstbasils.jpg"];
colour = Import["St.-Basils-Cathedral-001.jpg"];
{grey, colour}

enter image description here

transform = Last@FindGeometricTransform[colour, grey];
tcolour = 
 ImageTransformation[colour, transform, DataRange -> Full, 
  PlotRange -> Transpose@{{0, 0}, ImageDimensions@grey}];

enter image description here

ColorCombine[{First@ColorSeparate[grey, "LAB"]}~Join~
  Rest@ColorSeparate[tcolour, "LAB"], "LAB"]

enter image description here

It's not perfect, but it's something.

$\endgroup$
  • $\begingroup$ thank u very much this really helped me. $\endgroup$ – Narine Dec 18 '14 at 13:00
8
$\begingroup$

Here is one approach that does not respect object boundaries that uses HistogramTransform` in the HSB color space. For example, you can colorize the black and white image imgBW (according to the colors in the reference image img) using:

img = Import["http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/2012/3/14/1331719456752/St.-Basils-Cathedral-001.jpg"]; 
imgBW = Import["http://fc06.deviantart.net/fs71/f/2012/047/6/b/saint_basil__s_cathedral_by_tomdal-d4pwlwo.jpg"];

HistogramTransform[ColorConvert[imgBW, "HSB"], ColorConvert[img, "HSB"]]

enter image description here

which is a colorful if inauthentic colorization. You can also try the same thing in "LAB" and the other colorspaces.

There is no need to apply the histogram transformations to the same channel in the various images. Here is the application of all the different color spaces, and a sampling of the output.

fun[{x_, y_}] := HistogramTransform[ColorConvert[imgBW, x], ColorConvert[img, y]];
colSpaces = {"LAB", "RGB", "HSB", "LCH", "LUV", "XYZ"};
allCols = Flatten[Outer[List, colSpaces, colSpaces], 1];
fun[#] & /@ allCols

enter image description here

$\endgroup$
  • $\begingroup$ thank you,but there are only one problem every object from grayscale image should take the same oject color from colored image. $\endgroup$ – Narine Dec 14 '14 at 17:24
  • $\begingroup$ but i ll try this way to thank u $\endgroup$ – Narine Dec 14 '14 at 17:26
  • $\begingroup$ I doubt that it is going to be possible to accomplish colorizing on a per-object basis, primarily because it would be necessary to automatically locate "objects". While objects (in your pictures: spires, sky, domes) are visually trivial for humans, I know of no good algorithms that can reliably make these distinctions in a general scene. $\endgroup$ – bill s Dec 14 '14 at 18:12
  • $\begingroup$ yes it seems to be imposible,i am trying to do this already one week but there are no result,so to my think i will use the code you bring. $\endgroup$ – Narine Dec 14 '14 at 18:16
  • $\begingroup$ What you could try would be to segment the image (Watershed, for instance) and then operate on each detected segment individually. But this again requires matching segments from the two images -- something unlikely to be generic. $\endgroup$ – bill s Dec 14 '14 at 18:19
1
$\begingroup$

Here's another way of approaching this problem using the new ImageRestyle functionality in version 11. The restyling can be used to colorize by placing the greyscale image in the first argument and the color template in the second.

grey = Import["http://www.theeveryman.com/images/2007/em07p/hstbasils.jpg"];
color = Import["https://i.stack.imgur.com/WYoPg.png"]
ImageRestyle[grey, color]

enter image description here enter image description here enter image description here

Or Using pre-trained model of NetModel

net = NetModel@"AdaIN-Style Trained on MS-COCO and Painter by Numbers Data";
ImageResize[net[<|"Content" -> grey, "Style" -> color|>], 
            ImageDimensions@grey]

enter image description here

$\endgroup$
  • $\begingroup$ @partida -- thanks -- it's very interesting how similar the netmodel results are to the ImageRestyle -- this may be giving a hint about how ImageRestyle works... $\endgroup$ – bill s Oct 6 '17 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.