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:

enter image description here

  • $\begingroup$ Can you show your image? $\endgroup$
    – yode
    Apr 17, 2016 at 13:19
  • $\begingroup$ Have you seen ImageTake ? $\endgroup$ Apr 17, 2016 at 13:48
  • $\begingroup$ 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. $\endgroup$ Apr 17, 2016 at 13:52
  • 1
    $\begingroup$ 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. $\endgroup$
    – bill s
    Apr 17, 2016 at 14:54
  • $\begingroup$ 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? $\endgroup$ Apr 17, 2016 at 15:19

1 Answer 1


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

enter image description here

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}];

enter image description here

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

  • $\begingroup$ 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. $\endgroup$ Apr 17, 2016 at 15:23
  • $\begingroup$ 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? $\endgroup$
    – Sumit
    Apr 17, 2016 at 16:15
  • $\begingroup$ 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. $\endgroup$ Apr 17, 2016 at 16:47
  • $\begingroup$ @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. $\endgroup$
    – MarcoB
    Apr 25, 2016 at 3:47
  • $\begingroup$ @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. $\endgroup$ Apr 28, 2016 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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