# How to apply by-hand image selection to different images

Mathematica supports image selections by hand, by utilizing the selection tool. I used it to extract relevant data (hundreds of little squares). Now I have a second image of same size, where I want to apply the same extraction. But I dont like to do it by hand anymore, but use the same selection boxes form the previous image. How can I transfer the selection boxes to other images?

Here is an example:

• Can you show your image?
– yode
Apr 17, 2016 at 13:19
• Have you seen ImageTake ? Apr 17, 2016 at 13:48
• Yes, and I could theoretically use it, to apply the selection on different pictures, but it's not as a powerful selection tool as the selection by hand (i have to type in numbers rather than a selection by eye). the manual selection tool also allows for circles and different selection shapes. Apr 17, 2016 at 13:52
• Use the mask tool to select the regions. Once done, choose the "copy" option and then select "Save mask as image". Then this mask can be applied to another image of your choosing. Apr 17, 2016 at 14:54
• OK, but 1. How can extract the masked image content? I want each masked area to be stored in a different image. 2. How can I apply the masks to other images? Apr 17, 2016 at 15:19

Your image is not still good enough to work on. So I made one and show how to do it.

Let say this is your picture

XX = (# + RandomReal[{-.1, .1}]) & /@ Range[19];
XX = Join[{0}, XX, {20}];
YY = (# + RandomReal[{-.1, .1}]) & /@ Range[19];
YY = Join[{0}, YY, {20}];

img = ContourPlot[Evaluate[Join[(y == #) & /@ XX, (x == #) & /@ YY]],
{x, -0.01, 20.01}, {y, -0.01, 20.01},Frame -> False, PlotRangePadding -> 1,
ContourStyle -> Black, Epilog ->
Table[{Hue[RandomReal[{0, 0.1}]],
Disk[{i - 0.5, j - 0.5}, 0.3 + RandomReal[0.1]]}, {i, 20}, {j,
20}], ImageSize -> 400] // Rasterize


First I Import the image

img = ImageResize[img, 400]


Now I have to find the position of the black lines. For that I will take a stripe and find the location of black pixel. You may have to do it few times to get the exact values.

{Lx, Ly} = ImageDimensions[img]
ImageTake[img, {25, 30}, {1, Lx}] // Sharpen;
XX = ImageValuePositions[%, Black, 0.2][[All, 1]] // Union;
XX = DeleteDuplicates[XX, EuclideanDistance[#1, #2] <= 5 &];
ImageTake[img, {1, Ly}, {293, 295}] // Sharpen;
YY = ImageValuePositions[%, Black, 0.2][[All, 2]] // Union;
YY = DeleteDuplicates[YY, EuclideanDistance[#1, #2] <= 5 &];

nx = Length[XX]
ny = Length[YY]


Now you have the location of the lines stored in XX and YY. So go ahead with ImageTake.

Table[ImageTake[img, {XX[[i]], XX[[i + 1]]}, {YY[[j]], YY[[j + 1]]}],
{j, ny - 1}, {i, nx - 1}];
%[[1]]


If you want to get rid of the black lines, use ColorReplace. Only tricky part is to choose the slice. For example when you are finding XX do not take slice which has a horizontal line. Same for YY

• that does not work properly. Look at the results. the boxes are not exactly matched. I also tried ImagePartition, which basically does the same. This is why I want to do it manually. Manually always works. Apr 17, 2016 at 15:23
• How do you create those boxes? If you know the box coordinates, you can use variable starting and end point for each boxes. Can you make those lines in black? Apr 17, 2016 at 16:15
• The uploaded image is just an example. In reality I will have to deal with pictures made by a webcam, which takes photos of an array of test tubes, and the separtion between those is even poorer defined. unfortunately I dont have a reallife example right now. But basically your idea could work, if I manage to visually seperate the test tubes by colour strips. But since the camera is static, the fastest option would be to select the pixels manually once, and for all. Apr 17, 2016 at 16:47
• @Anton then maybe you should include one of your actual images, so nobody wastes any more effort doing something whose accuracy they cannot even check. Apr 25, 2016 at 3:47
• @MarcoB :I would have already done that, if I had one, trust me. I don't have the real data yet. at Summit: I thank you for your efforts, but as I said, this method will not be appliable to the real data later, because there are no black lines. The only advantage that one can use in the case of the real data, is that the stationarity. So I need to find a way to save individual masks, and apply them to other pictures of same size. Apr 28, 2016 at 20:52