image processing - How can I properly Partition "IntensityData" output from "ComponentMeasurements"? (Recovering the (x, y) dimensions of a rect. morph. component?)
When I ask ComponentMeasurements to return "IntensityData" for some set of rectangular morphological components:
m = MorphologicalComponents[image, Method -> "BoundingBox"];
intensityDataArray = ComponentMeasurements[{m, image}, "IntensityData"][[All, 2]];
The pixel value data is returned as an unpartitioned one-dimensional array. This makes it difficult for me to understand where a particular pixel is in the image. Now, if I know the exact dimensions of each morphological component, I suppose I could fix the problem using Partition. I could also fix the problem with Partition if I could ask for something like a bounding square instead of a bounding rectangular box, though I don't think this is possible (is it?).
Besides manually measuring the dimensions of each rectangular morphological component, is there a workaround to fix this problem?
Letting m consist of strictly rectangular morphological components, it would, for example, be very nice to be able to write something like:
ComponentMeasurements[m, "Dimensions"][[All, 2]]
And have this return something like:
{{5,8}, {87, 40}, {7, 7}, ...}
Meaning that we have morphological components of dimensions $(x, y) = (5 \times 8)$, $(87 \times 40)$, $(7 \times 7)$, and so forth. Here, we're provided ordered values for the extent of the component along the $x$- and $y$-axes, respectively, allowing us to trivially use Partition to restore the cropped section of an image underlying a morphological component (i.e. the image one would have if a morphological component is used as a "cropping mask").
Clarification: Masking approaches are really neat, but not quite what I'm looking for here unless they're part of a larger approach. I need a method of grabbing the pixel data strictly underlying a morphological component (for a set of morphological components) and retaining $(x, y)$ data for each pixel. My downstream procedure then does further image processesing on this rectangular matrix. If I do a matrix multiplication masking procedure, I still need to trim away the dark space to get down to a bounding box (the size of the morphological component mask).
Also, it seems a little wasteful to have to perform multiplication operation on all pixels in the image to accomplish my goal?
Answer
I'm answering the question you posted in a comment:
but how do I then crop to a bounding box around that component
List I said, the easiest way is probably to measure the "BoundingBox"es and pass them to ImageTrim:
image=ExampleData[{"TestImage","APC"}]

m=MorphologicalComponents[Binarize@ColorNegate[ColorConvert[image,"Grayscale"]]];
Colorize[m]

components=ComponentMeasurements[{m,image},{"Area","BoundingBox"},#1>100&];
ImageTrim[image,#]&/@components[[All,2,2]]

Comments
Post a Comment