First, there is no need to use Module in your sample code above. You could write simply:

CCGray[pic_?ImageQ] := ColorConvert[pic,"GrayScale"]

Now, if you want to manage the channel mixing manually you can use ImageApply and Dot:

customGray[img_?ImageQ, ker_?VectorQ] := ImageApply[ker.# &, img]

img = Import["https://i.sstatic.net/wtlqF.jpg"];

customGray[img, {0.8, 0.5, -0.3}]

You'll notice that my three values add to one; this is typically necessary to preserve white as white. If you want to make this automatic just divide by the total:

customGray2[img_?ImageQ, ker_?VectorQ] := With[{k = ker/Tr@ker}, ImageApply[k.# &, img]]

Or with normalization control as an Option:

Options[customGray2] = {Normalize -> True};

customGray2[img_?ImageQ, ker_?VectorQ, OptionsPattern[]] :=
  With[{k = If[OptionValue[Normalize], ker/Tr@ker, ker]},
    ImageApply[k.# &, img]
  ]

This makes it easy to Manipulate:

Manipulate[
 customGray2[img, {a, b, c}],
 {a, -5, 5}, {b, -5, 5}, {c, -5, 5}
]

Answer from Mr.Wizard on Stack Exchange
🌐
Wolfram Language
reference.wolfram.com › language › ref › ColorConvert.html
ColorConvert: Change the image color space—Wolfram Documentation
ColorConvert works with colors and arbitrary 2D and 3D images, as well as an explicit list of color channel values. ColorConvert[list,colspace] by default interprets list as follows: ... When converting to or from "Grayscale", "RGB", "CMYK", and "HSB", channel values are clipped to be between 0 and 1.
🌐
Wolfram Community
community.wolfram.com › groups › - › m › t › 1311034
[?] Convert an rgb image into grayscale using For function and matrix? - Online Technical Discussion Groups—Wolfram Community
Wolfram Community forum discussion about [?] Convert an rgb image into grayscale using For function and matrix?. Stay on top of important topics and build connections by joining Wolfram Community groups relevant to your interests.
Top answer
1 of 3
11

Using the images in the duplicate question,

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

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

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

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

2 of 3
8

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"]]

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

Top answer
1 of 2
5

Using ND is not necessary, as Mathematica makes it easy to construct InterpolatingFunction objects, which can then be formally differentiated using Derivative. First, a sample image:

data = Import[
  "http://41.media.tumblr.com/bece7ed81870c8e9b904290700451eb2/tumblr_\
mv8m2sE2FW1slvzano1_400.jpg"]

Convert to grayscale, and then convert the resulting matrix into an InterpolatingFunction object:

imData = ImageData[ColorConvert[data, "Grayscale"]];
f = ListInterpolation[imData];

Plot the InterpolatingFunction using DensityPlot:

DensityPlot[f[-x, y], {y, 1, 400}, {x, -1, -398}, PlotPoints -> 300, 
 ColorFunction -> GrayLevel]

Plot the first partial derivative in the $x$-direction:

g = Derivative[1, 0][f];
DensityPlot[g[-x, y], {y, 1, 400}, {x, -1, -398}, PlotPoints -> 300, 
 PlotRange -> All, ColorFunction -> GrayLevel]

Plot the second partial derivative in the $x$-direction:

h = Derivative[2, 0][f];
DensityPlot[h[-x, y], {y, 1, 400}, {x, -1, -398}, PlotPoints -> 300, 
 PlotRange -> All, ColorFunction -> GrayLevel]

Of course, you can get better results with even less work by using bill s's answer.

2 of 2
5

You can get the values of the pixel intensities directly using the built-in functions PixelValue and/or ImageValue. For example, using DumpsterDoofus's image, the RGB pixel values at location {100,100} are easy to find, and their mean is (roughly) the intensity.

data = Import["http://41.media.tumblr.com/bece7ed81870c8e9b904290700451eb2/tumblr_mv8m2sE2FW1slvzano1_400.jpg"]; 
PixelValue[data, {100, 100}]
Mean[PixelValue[data, {100, 100}]]

Depending on what you want to do with the image, there may be no need to do the interpolation at all. There are many functions that deal directly with images, including GradientFilter (that does more or less what you ask for in a single command). If your goal in taking the derivatives is to detect edges, then there is Edgedetect. GradientOreintationFilter is another option.

GradientFilter[data, {2, 0.1}]

You can take the second derivative

GradientFilter[GradientFilter[data, {1, 0.1}], {1, 0.1}]

and there are numerous options to control the process.

Find elsewhere
🌐
Wolfram Language
reference.wolfram.com › language › ref › Binarize.html
Binarize: Threshold into a black and white image—Wolfram Documentation
Binarize converts multichannel and color images into grayscale images, then produces an image in which every pixel has value 0 or 1. Binarize works with arbitrary 2D and 3D images.
🌐
Wolfram|Alpha
wolframalpha.com › input
convert tiger image to grayscale - Wolfram|Alpha
Compute answers using Wolfram's breakthrough technology & knowledgebase, relied on by millions of students & professionals. For math, science, nutrition, history, geography, engineering, mathematics, linguistics, sports, finance, music…
🌐
Wolfram
resources.wolframcloud.com › FunctionRepository › resources › FaustGrayscaleConvert
FaustGrayscaleConvert | Wolfram Function Repository
May 17, 2021 - ResourceFunction["FaustGrayscaleConvert"] can be used to change the color specification of an image to grayscale.
🌐
Wolfram Community
community.wolfram.com › groups › - › m › t › 842655
Convert image from one color to another? - Online Technical Discussion Groups—Wolfram Community
Wolfram Community forum discussion about Convert image from one color to another?. Stay on top of important topics and build connections by joining Wolfram Community groups relevant to your interests.
🌐
Wolfram Language
reference.wolfram.com › language › ref › ColorSeparate.html
ColorSeparate: Separate color channels—Wolfram Documentation
Convert a color image to grayscale by selecting the maximum channel value of each pixel: Compute the gradient of a multichannel image using three different methods: Separating colors of an image in a space is equivalent to color separation after ...