I have a black and white .tiff file of a beam spot:

Beam profile

I am trying to characterize the source in a Monte Carlo program, to do this I want to convert the image into a 3D Histogram with controllable bin size.

Haven't been able to figure out how to convert the image data to match Histogram3D[] input parameters, closest I've gotten so far are 3D plots using:

ListPlot3D[Reverse@ImageData@RemoveAlphaChannel@ColorConvert[beam1, "Grayscale"], AxesLabel -> {"x", "y", "intensity"}, PlotRange -> All]

Any pointers on how to go about this? Thanks!

up vote 5 down vote accepted

Method 1: Histogram3D

Import and clean the image:

i = Import["https://i.stack.imgur.com/uGWn6.jpg"];
i = RemoveAlphaChannel@ColorConvert[i, "Grayscale"]

Histogram3D looks at the number of points in the bins, but that is uniform for an image as indexes of pixels are uniform. So I guess besides that you want data weighted by pixel values:

dataW=
WeightedData[
    Flatten[Array[List,Reverse@ImageDimensions[i]],1],
    Flatten[ImageData[i],1]
]

Now you can bin in a custom way:

Histogram3D[dataW, {50, 60}, ColorFunction -> "Rainbow"]

enter image description here

With a little bit of style you can get it looking quite any way you want:

Histogram3D[dataW,{30,35},
    ChartElementFunction->
    ChartElementDataFunction["ProfileCube","Profile"->2.,"TaperRatio"->0.6],
    ColorFunction->"Rainbow",ChartStyle->Opacity[.5],PlotTheme->"Marketing"]

enter image description here

Method 2: ListPlot3D

You could achieve a similar result differently, in a bit hack-ish way. Define data as:

data = ImageData[ImageResize[i, {30, 30}]];

so ImageResize spec 30x30 gives effective number of bins in X and Y directions. Resizing an image is effectively binning (in your specific case).

ListPlot3D[data,PlotRange->{{5,25},{10,25},All},InterpolationOrder->0,
ColorFunction->"Rainbow",Filling->Bottom,Mesh->None]

enter image description here

Add another data processing method

im1=RemoveAlphaChannel[
        ColorConvert[Import["https://i.stack.imgur.com/uGWn6.jpg"], 
         "Grayscale"]];
    data = ImageData[im1];
    dim = Dimensions[data];
    A = Flatten[
       Table[{i, j}, {i, dim[[2]]}, {j, dim[[1]]}, {k, 
         PixelValue[im1, {i, j}, "Byte"]}], 2];

Histogram3D[A, {{.5}, {2}}, ColorFunction -> Hue]

fig1

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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