How to crop image for every element in the grid

Dear expert,

I have 12 images. And I made grid (using Analyze -> Tools -> Grid) of size 2923779 ( i.e. Area per point __ pixels ^2) for all images uniformly.
It looks like this:

What I want to do is to crop the each of the image above according to the grid and save every crop as a file.
How can I do that?

One of the file shown above can be downloaded here (160MB).

CIao @Peverall_Dubois,

I wrote this plugin that does exactly what you are asking, it is a python script.


'''
Created on Aug 17, 2015

subdivide images using an user defined grid 

@author: emartini
'''
from javax.swing import JFileChooser
from ij import IJ
from ij.plugin import Duplicator
import math
from ij.io import DirectoryChooser
from ij.gui import GenericDialog
from ij.plugin.frame import RoiManager
from ij.gui import Roi
from javax.swing.filechooser import FileNameExtensionFilter

def getOptions():  #get images and grid rown and columns    
    fc=JFileChooser()
    file_all_filter=fc.getAcceptAllFileFilter()
    file_img_filter=FileNameExtensionFilter("images (jpg,jpeg,tiff,tif)",["jpg","jpeg","tiff","tif"])
    fc.removeChoosableFileFilter(file_all_filter)
    fc.addChoosableFileFilter(file_img_filter)
    fc.addChoosableFileFilter(file_all_filter)
    fc.setMultiSelectionEnabled(True)
    fc.showDialog(None, "Choose Image To Crop")
    file_list=fc.getSelectedFiles()             
    gd = GenericDialog("GRID DEFINE")  
    gd.addNumericField("number of rows", 3,0)  
    gd.addNumericField("number of column", 3,0)  
    gd.showDialog()  
    n_row = gd.getNextNumber()  
    n_col = gd.getNextNumber()
    n_sub_image=(n_col)*(n_row)  
    return n_row,n_col,n_sub_image,file_list,gd


n_row,n_col,n_sub_image,file_list,gd=getOptions()
if gd.wasOKed():
    dc=DirectoryChooser("SAVE path...")
    save_path = dc.getDirectory()
    roiM=RoiManager()
    for elem in file_list:            
        img_path=str(elem)
        DAB=IJ.openImage(str(img_path))
        IJ.log(img_path)
        #get img and grid parameters
        tit_img=DAB.getTitle()
        h_img=DAB.getHeight()      
        w_img=DAB.getWidth()
        span_w=w_img/n_col
        span_w=math.floor(span_w)
        span_h=h_img/n_row
        span_h=math.floor(span_h)
        #subdivision
        for r in range(1,n_col+1,1):
            for c in range(1,n_row+1,1):      
                curr_roi=Roi(span_w*(r-1),span_h*(c-1),span_w,span_h)
                curr_roi.setName(tit_img[0:len(tit_img)-4]+"_R"+str(c)+"_C"+str(r))        
                DAB.setRoi(curr_roi)
                roiM.addRoi(curr_roi)               
                dup = Duplicator().run(DAB);
                dup.setTitle(curr_roi.getName())
                #dup.show()
                IJ.save(dup, save_path+curr_roi.getName()+".tif")        
        roiM.runCommand("Deselect"); # deselect ROIs to save them all
        roiM.runCommand("Save", save_path + tit_img[0:len(tit_img)-4]+ "_RoiSet.zip")
        roiM.reset()
    roiM.reset()
    DAB.close()    
IJ.run("CloseAll Images noSAVE", "");        
roiM.close()

If you have some question about it works, please ask.
Have a nice day,
Emanuele M.

2 Likes

Hi,

Thank you for your reply. But in your code the shape of the diced/discretized images
is relative to the input full image dimension. What I want is the core majority of the grid element size is fixed for all given input image. (e.g. square with size width x height = 1000x1000 pixels). The shape of the surrounding elements of the grid is not squared, that’s ok.

So exactly like the grid scheme in FIJI as shown in my earlier post.
Is there a way I can modify your code?

P.D.