# Using image data to create a 3D Histogram

I have a black and white .tiff file of a beam spot: 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]

## 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"] 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"] ## 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] 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[]}, {j, dim[]}, {k,
PixelValue[im1, {i, j}, "Byte"]}], 2];

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