Skip to content

Commit 57577fe

Browse files
committed
docs wip add code examples
1 parent 38d0127 commit 57577fe

File tree

1 file changed

+65
-5
lines changed

1 file changed

+65
-5
lines changed

docs/Tutorials/ROI analysis.md

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ for (const roi of biggestRois) {
8787
This provides us with a code like this:
8888

8989
```ts
90-
const rois = roiMap.getRois({ kind: 'black' }); // In this example we specifically calculate the average surface of rois,
91-
//so we don't use minSurface option here.
90+
const rois = roiMap.getRois({ kind: 'black' });
91+
// In this example we want to specifically calculate
92+
//the average surface of rois, so we don't use
93+
// minSurface option here.
9294

9395
let surfaceSum = 0;
9496
for (const roi of rois) {
@@ -129,17 +131,75 @@ There you will have other two parts: one part will be comprised of a map with fi
129131

130132
### Getting extra data
131133

132-
Within metadata, you might be wondering what is this huge mix of letters and numbers:
134+
Within metadata, you might be wondering what this huge mix of letters and numbers represents:
133135

134136
![](./images/roiAnalysis/extraData.jpg)
135137

136138
These are custom fields added with additional information about an image that the user. For instance, in this case you can get information about the microscope that was used, or the magnification level or the electrometric tension that was used while the image was taken. However, this data needs to be parsed, because it difficult to decipher it in its raw format.
137-
To do so you need to split the lines first:
139+
To do so you need to identify what is the key id of this text. In our case it is 34682, but it might not always be the case so check it beforehand.
138140

139-
```
141+
Next thing we need to do is to parse this text.
140142

143+
```ts
144+
let metaMisc = [];
145+
146+
let lines = image.meta.tiff.fields.get(34682).split('</Data><Data>');
147+
//We split each line into three elements:
148+
//key(name of the tag)
149+
//value(value of the tag)
150+
//unit(units in which the value is measured).
151+
lines.forEach((a) => {
152+
var fields = a.split(/<\/Label><Value>|<\/Value><Unit>/);
153+
fields[0] = fields[0].replace(/^.*>/, '');
154+
fields[2] = fields[2].replace(/<.*$/, '');
155+
metaMisc.push({
156+
key: fields[0],
157+
value: fields[1],
158+
unit: fields[2],
159+
});
160+
});
141161
```
142162

163+
With this the data should be parsed.
164+
143165
![](./images/roiAnalysis/parsedExtraData.png)
144166

145167
### Getting pixel size
168+
169+
In this specific scenario we would also like to tell you about the way to calculate image's pixel size. Pixel size can be one of metadata fields but if this isn't the case we would like to show you how you can calculate it from existing data.
170+
171+
If there is no such field as "Pixel size" you can calculate DPI resolution and apply it with magnification.
172+
DPI resolution represents the number of dots per inch. To calculate it we need to look at three lines in our parsed extra data: XResolution, YResolution and ResolutionUnit.
173+
X and Y resolutions are the number of dots per inch on X and Y axes. So, if they are equal, then DPI resolution equals to one of these values. However, this value might not be measured in inches. To check that we need to look at ResolutionUnit.
174+
If its value equals to 2 then the X and Y resolutions are measured in inches.If it's 3 then it's in centimeters and has to be converted.
175+
176+
**image of three extra data fields**
177+
178+
```ts
179+
const DPIResolution = 0;
180+
const metaTags = image.meta.tiff.tags;
181+
if (metaTags.XResolution == metaTags.YResolution && metaTags.XResolution) {
182+
switch(metaTags.ResolutionUnit)
183+
case 2:
184+
DPIResolution = metaTags.XResolution;
185+
break;
186+
case 3:
187+
//converted from centimeters to inches
188+
DPIResolution = metaTags.Xresolution*2.54;
189+
break;
190+
default:
191+
break;
192+
}
193+
```
194+
195+
After that we need to get the magnification. In our case it is already known.
196+
**get magnification image**
197+
198+
All is left is to calculate it through the formula.
199+
200+
```ts
201+
const newPixelSize = 30000 / magnification[0].value / 1e9;
202+
//equals 2.7272727272727273e-10
203+
//We already have an object that stores extra data, so let's add pixel size there.
204+
metaMisc.push({ key: 'Pixel Size', value: newPixelSize, unit: 'nm' });
205+
```

0 commit comments

Comments
 (0)