I have the image seen below and I would like be able to automatically crop out everything around each red section - not enough to overlap but enough that you can see how connected to the green parts they are. Is there an easy way to do this, ideally in python/R? In case it matters, I'll be doing some image recognition on the resulting images.

The top image is an example of what I'd like them to look like, the bottom is what I start with.

Sorry if this is stupidly easy or really general, I've never done any work with images so don't even know where to start.

Example of one that's been cropped

Tree of neurons

  • $\begingroup$ Can you manually crop one as an example? $\endgroup$ – havakok Apr 16 at 13:31

I can't give you a detailed workflow for it right now, but I'll tell you how I'd go about it:

A simple solution would be to color-threshold the image. Your image only uses red and green so it would work quite nicely. I used a red value of 122-255 and a green value of 0-122. This way you obtain a b/w image with the red regions white and the rest of the image black / or the other way around.

On this segmented image you can perform particle analysis. Particle here refers to one white / black region that is separated from the rest (in your case one red blob). It returns the x/y location as well as the extent etc. of the region. this you can use to automatically crop the image to the region of the particle +/- x pixels to obtain the cropped images.

I guess this method won't spare you of cross checking the automated results and deleting falsely detected noise etc though.

I alwaya use ImageJ/FiJi to explore workflows like this, and once I roughly know how I want to proceed, I look for python solutions. If oyu want to do this in python I suggest you either look into OpenCV or scikit-image.

Stuff I'd read into for your case:

Once you got the coordinates of the red regions, cropping it is as easy as slicing a numpy array: img_cropped = img[x_coord - extent : x_coord + extent, y_coord - extent : y_coord + extent]

  • $\begingroup$ Thanks for the advice, I'll give it a go! $\endgroup$ – Sethzard Apr 24 at 11:02
  • $\begingroup$ No problem! Unfortunately I don't have time to look for a solution in detail, but I h ope this will get you in he right direction! $\endgroup$ – cripcate Apr 24 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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