Skip to content

Commit

Permalink
docs: reformat text and fix inaccuracies
Browse files Browse the repository at this point in the history
  • Loading branch information
EscapedGibbon committed Jun 28, 2024
1 parent dd4d43c commit 52fe08e
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions docs/Tutorials/Find ROI average in Stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,91 +2,112 @@ The point of this tutorial is to show how to decode a stack of images and how to

## Synopsis

This tutorial demonstrates how to use ImageJS to decode a TIFF stack of images and analyze frame-by-frame changes, particularly focusing on regions of interest (ROIs) in pulsar images. The process involves:
Using the fs library we read and decode the TIFF stack.
Then,utilizing the `maxImage()` function to get the image with the maximum pixel values across the stack.
We identify regions of interest from the maximum value image using a threshold mask and obtaining their coordinates.
Finally we compute the average pixel value for each ROI across all images in the stack, storing the results in a map.
We can use this data to analyze changes in intensity over time.
This tutorial demonstrates how to use ImageJS to decode a TIFF stack of images and analyze frame-by-frame changes, particularly focusing on regions of interest (ROIs) in pulsar images.

- Using the `fs` library we read and decode the TIFF stack.
- Then,utilizing the `maxImage()` function, we get the image with the maximum pixel values across the stack. Since we need to use `threshold()`to find ROI, we must check if its `colorModel` is `"GREY"`. If not, image must be grayscaled.
- We identify regions of interest from the maximum value image using a threshold algorithm and obtain their coordinates.
- Finally we compute the average pixel value for each ROI across all images in the stack, storing the results in a map.
We can use this data to analyze changes in intensity over time or compare changes in its position.

```ts
const buffer = fs.readFileSync('/path/to/file.tiff');
const stack = decodeStack(stack);
const maxValueImage = stack.maxImage();

let maxValueImage = stack.maxImage();
//We will use `threshold()` to find ROIs
//therefore color model has to be "GREY".
if (maxValueImage.colorModel !== 'GREY') {
maxValueImage = maxValueImage.grey();
}
const maxValueMask = maxValueImage.threshold();
const roiMap = fromMask(maxValueMask);
//Provides all the regions of interest.
const rois = roiMap.getRois();
const stackGrays = new Map<number, number[]>();
const stackGrays = new Map<number, number[][]>();
for (const roi of rois) {
const stackAvgs = [];

Check warning on line 29 in docs/Tutorials/Find ROI average in Stack.md

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (Avgs)
const roiPoints = roi.absolutePoints;
for (const image of stack) {
const avgValue = image.mean({ points: roiPoints });
//Gets value from one channel since it is grayscaled.
stackAvgs.push(avgValue[0]);
stackAvgs.push(avgValue);

Check warning on line 33 in docs/Tutorials/Find ROI average in Stack.md

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (Avgs)
}
stackGrays.set(roi.id, stackAvgs);

Check warning on line 35 in docs/Tutorials/Find ROI average in Stack.md

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (Avgs)
}
```

Here is a more detailed review of these steps.

## Decode the Stack

ImageJS has the ability to decode a `tiff` stack of images. Images in stack can represent frame-by-frame successive changes. This way we can take a look dynamically at changes that happen to regions of interest.
ImageJS has the ability to decode a TIFF stack of images. Images in stack can represent frame-by-frame successive changes. This way we can take a look at dynamic changes that happen to regions of interest.
In our specific case here, we have a stack of pulsar kind of images. We can use ImageJS to figure out when the region is shown in the image and when it isn't by looking at the average value of said region.

Just like any image, after getting our stack needs to be parsed fo us to work with data. ImageJS doesn't have a built-in function to parse tiff stack, so you will have to use
`fs` library.
Just like any image, after getting our stack needs to be parsed fo us to work with data.

```ts
//ImageJS doesn't have a built-in function to parse TIFF stack,
//so use `fs` library.
const buffer = fs.readFileSync('/path/to/file.tiff');
const stack = decodeStack(stack);
```

## Find the image with maximum values:

Stack class has a function called `maxImage()`. It will give us the maximum value of each pixel throughout the stack. We will use this image as a reference for all other images, to locate all possible regions of interest.
Stack class has a function called `maxImage()`. It will give us the maximum value of each pixel throughout the stack. We will use this image as a reference for all other images to locate their ROIs.

```ts
const maxValueImage = stack.maxImage();
//We will use `threshold()` to find ROIs
//therefore color model has to be "GREY".
if (maxValueImage.colorModel !== 'GREY') {
maxValueImage = maxValueImage.grey();
}
```

![Image](./images/stackAvg/maxImage.png);

## Locate ROIs

From our "maxValues" image we can find all the regions of interest. To be precise we need their coordinates to apply them to other images.
From our `maxValueImage` image we can find all the regions of interest. To be precise we need their coordinates to apply them to other images.

```ts
const maxValueMask = maxValueImage.threshold();
const roiMap = fromMask(maxValueMask);
```

![Mask](./images/stackAvg/maxMask.png)

```ts
//Provides all the regions of interest.
const rois = roiMap.getRois();
```

![Mask](./images/stackAvg/maxMask.png);
![Painted ROIs](./images/stackAvg/paintedROIs.png)

## Find average value of each ROI on each image

After we found all the ROIs on the "maxValue" image, we take each region and check its average value on every image in the stack.
After we found all the ROIs on the `maxValueImage`, we take each region and check its average intensity on every image in the stack.
It will look something like this:

```ts
const stackGrays = new Map<number, number[]>();
const stackGrays = new Map<number, number[][]>();
for (const roi of rois) {
const stackAvgs = [];

Check warning on line 96 in docs/Tutorials/Find ROI average in Stack.md

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (Avgs)
const roiPoints = roi.absolutePoints;
for (const image of stack) {
const avgValue = image.mean({ points: roiPoints });
//Gets value from one channel since it is grayscaled.
stackAvgs.push(avgValue[0]);
stackAvgs.push(avgValue);

Check warning on line 101 in docs/Tutorials/Find ROI average in Stack.md

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (Avgs)
}
stackGrays.set(roi.id, stackAvgs);
}
```

This will create a map where the key is the ID of each ROI, and the values are an array of average values of ROI coordinates across all the images in the stack.
This will create a map where keys are IDs of each ROI, and values are an array of pixels with average intensity across all the images in the stack.
This way we can take a look at the changes in intensity of ROI from one image to another.
This will give us the graph of intensities across all the images in the stack for each ROI.
For instance here we have a graph of intensity of region with ID of 9. We can see that there is a rising pulse between images 1 and 4, but then it disappears.

### Add graph image
![ROI 9](./images/stackAvg/ROI9.png)
![ROI 9 graph](./images/stackAvg/graphROI9.svg)

0 comments on commit 52fe08e

Please sign in to comment.