Splitting single image in four

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Splitting single image in four

akp19
Hi!
I am quite new to ImageJ and not able to perform a relative simple task. I need to split a single image into 4 sub-images. Eg. a 1000*1000 image into four 250*250 images. How do I go about this? Will appreciate any help! Thank you!
Reply | Threaded
Open this post in threaded view
|

Re: Splitting single image in four

Krs5
Dear akp19

The macro code below is doing what you want.

title = getTitle();
w = getWidth();
h = getHeight();
makeRectangle(0, 0, 0.5*w, 0.5*h);
run("Duplicate...", " ");
selectWindow(title);
makeRectangle((0.5*w), 0, 0.5*w, 0.5*h);
run("Duplicate...", " ");
selectWindow(title);
makeRectangle(0, (0.5*h), 0.5*w, 0.5*h);
run("Duplicate...", " ");
selectWindow(title);
makeRectangle((0.5*w), (0.5*h), 0.5*w, 0.5*h);
run("Duplicate...", " ");
selectWindow(title);
close();

Best wishes

Kees


Dr Ir K.R. Straatman
Senior Experimental Officer
Advanced Imaging Facility
Centre for Core Biotechnology Services
University of Leicester
http://www2.le.ac.uk/colleges/medbiopsych/facilities-and-services/cbs/lite/aif



-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of akp19
Sent: 01 July 2016 16:54
To: [hidden email]
Subject: Splitting single image in four

Hi!
I am quite new to ImageJ and not able to perform a relative simple task. I need to split a single image into 4 sub-images. Eg. a 1000*1000 image into four 250*250 images. How do I go about this? Will appreciate any help! Thank you!



--
View this message in context: http://imagej.1557.x6.nabble.com/Splitting-single-image-in-four-tp5016797.html
Sent from the ImageJ mailing list archive at Nabble.com.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Splitting single image in four

Swayne, Theresa C.
In reply to this post by akp19
Hello and welcome!

There was a recent thread on the forum (http://forum.imagej.net/t/macro-for-automatic-saving-and-filename-designation/1966/7?u=tswayne)
 that included a macro that does essentially what you’re asking.

Below, I’ve included the macro that resulted from the forum discussion. This is by Thomas E. Sharp (Tgoose on the forum) with some additions by me.

To use the macro, copy the text, then open the script editor (in Fiji, File > New > Script), and paste the text in.
Under the Language menu choose IJ1 Macro. Save the file with the .ijm extension.
To run it, just open the image you want to crop and then click Run at the bottom of the script window.

More about macros in the User Guide: http://imagej.net/docs/guide/146-14.html#toc-Section-14

Hope this helps.

===================
n = getNumber("How many divisions (e.g., 2 means quarters)?", 2);
id = getImageID();
title = getTitle();
dotIndex = indexOf(title, ".");
basename = substring(title, 0, dotIndex);
path = getDirectory("image");
getLocationAndSize(locX, locY, sizeW, sizeH);
width = getWidth();
height = getHeight();
tileWidth = width / n;
tileHeight = height / n;
for (y = 0; y < n; y++) {
  offsetY = y * height / n;
  for (x = 0; x < n; x++) {
    offsetX = x * width / n;
    selectImage(id);
    call("ij.gui.ImageWindow.setNextLocation", locX + offsetX, locY + offsetY);
    tileTitle = basename + "[" + x + "," + y + "].tif";
    // using the ampersand allows spaces in the tileTitle to be handled correctly
    run("Duplicate...", "title=&tileTitle");
    makeRectangle(offsetX, offsetY, tileWidth, tileHeight);
    run("Crop");
    selectWindow(tileTitle);
    saveAs("tiff",path+tileTitle);
    }
  }
selectImage(id);
close();

==================

> On Jul 1, 2016, at 11:53 AM, akp19 <[hidden email]> wrote:
>
> Hi!
> I am quite new to ImageJ and not able to perform a relative simple task. I
> need to split a single image into 4 sub-images. Eg. a 1000*1000 image into
> four 250*250 images. How do I go about this? Will appreciate any help! Thank
> you!
>
>
>
> --
> View this message in context: http://imagej.1557.x6.nabble.com/Splitting-single-image-in-four-tp5016797.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

------------------------------------
Theresa Swayne, Ph.D.
Manager
Confocal and Specialized Microscopy Shared Resource

Herbert Irving Comprehensive Cancer Center
Columbia University Medical Center
1130 St. Nicholas Ave., Room 222A
New York, NY 10032
Phone: 212-851-4613
[hidden email]


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Splitting single image in four

akp19
In reply to this post by Krs5
Thank you so much Dr Kees!
This helps a lot!
Reply | Threaded
Open this post in threaded view
|

Re: Splitting single image in four

akp19
In reply to this post by Swayne, Theresa C.
Dear Theresa,
Thanks a ton for your macro! it works! Really appreciate the help! :) thank you!