# Split a (complex) image into sub parts

I'm aware of the solution to a similar problem by Hayashi Yoshiaki. Is there a way of extending the solution to break out the chromosomes from this example I have a mass of data in this format that needs to be converted to a training set. Note the there are holes in the chromosome that the human ignores when segmenting. Doing this manually for hundreds of images is a challenge. Thanks in advance

• Please give a link to Yoshiaki's solution. – C. E. Jul 31 '17 at 17:30
• mathematica.stackexchange.com/questions/109154/… – Boris Jul 31 '17 at 17:47
• Are all your images aligned and roughly equally spaced like this one? – yohbs Jul 31 '17 at 18:55
• Yes, thats right. They are autogenerated. some are long and bent but all are essentially equidistant. – Boris Jul 31 '17 at 19:00
• This image looks pretty easy to divide, to the extent that a very straightforward algorithm can be written (basically - detecting columns that are almost totally white). It would be helpful if you could provide a few more samples so that we can check the robustness. – yohbs Jul 31 '17 at 19:14

Here's the very straightforward method I suggested in my comment:

tot = Total@ImageData[Binarize[im, 0.9]];
columnsSequence = Flatten@Position[tot, x_ /; x >= 103];
columns = Round@(Mean /@ Split[columnsSequence, #2 - #1 == 1 &]);
imd = ImageData[im];
imd[[All, columns, All]] = 0;
Image[imd]


This is just drawing the lines that separate the chromosomes. From here you can split the image or do any other further manipulation. For calculating the best separating "white column" I used this trick.

Here's how to divide the image to subimages:

imd=ImageData[ims]
images = Table[
imd[[All, columns[[i]] ;; columns[[i + 1]], All]], {i,
Length[columns] - 1}]
Image /@ images


• OK, its a good start - how do I generate a list of images from this? I'll post a page of data from one example in the morning. – Boris Jul 31 '17 at 22:31
• Hi yohbs, Ive tested your routine on other sets and find that I need to adjust the position parameter but thats OK I can automate that. Im having trouble breaking out the individual components due to my limited familiarity with array handling. I shall persevere. – Boris Aug 1 '17 at 10:28
• this seems to work- not elegant Im afraid: – Boris Aug 1 '17 at 13:59
• pt1 = Drop[Partition[Riffle[columns, 0], 2], -1]; pt2 = Drop[Partition[Riffle[columns, 116], 2], 1]; components = Partition[Partition[Flatten[Transpose[{pt1, pt2}]], 2], 2]; images = ImageTrim[im, #] & /@ components – Boris Aug 1 '17 at 14:00
• @Boris You can do it more cleanly. I'll edit my answer. – yohbs Aug 1 '17 at 14:04