rimageanalysistools 5.1.2-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,55 @@
1
+ #--
2
+ # /* ***** BEGIN LICENSE BLOCK *****
3
+ # *
4
+ # * Copyright (c) 2012 Colin J. Fuller
5
+ # *
6
+ # * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # * of this software and associated documentation files (the Software), to deal
8
+ # * in the Software without restriction, including without limitation the rights
9
+ # * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # * copies of the Software, and to permit persons to whom the Software is
11
+ # * furnished to do so, subject to the following conditions:
12
+ # *
13
+ # * The above copyright notice and this permission notice shall be included in
14
+ # * all copies or substantial portions of the Software.
15
+ # *
16
+ # * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ # * SOFTWARE.
23
+ # *
24
+ # * ***** END LICENSE BLOCK ***** */
25
+ #++
26
+
27
+ require 'rimageanalysistools/initjava'
28
+
29
+ java_import Java::edu.stanford.cfuller.imageanalysistools.image.io.ImageReader
30
+
31
+ module RImageAnalysisTools
32
+
33
+ def self.get_image(filename)
34
+
35
+ ir = ImageReader.new
36
+
37
+ ir.read(filename)
38
+
39
+ end
40
+
41
+ def self.read_image_directory(dirname)
42
+
43
+ ir = ImageReader.new
44
+
45
+ images = []
46
+
47
+ Dir.foreach(dirname) do |f|
48
+
49
+ images << ir.read(File.expand_path(f, dirname))
50
+
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,84 @@
1
+ #--
2
+ # /* ***** BEGIN LICENSE BLOCK *****
3
+ # *
4
+ # * Copyright (c) 2012 Colin J. Fuller
5
+ # *
6
+ # * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # * of this software and associated documentation files (the Software), to deal
8
+ # * in the Software without restriction, including without limitation the rights
9
+ # * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # * copies of the Software, and to permit persons to whom the Software is
11
+ # * furnished to do so, subject to the following conditions:
12
+ # *
13
+ # * The above copyright notice and this permission notice shall be included in
14
+ # * all copies or substantial portions of the Software.
15
+ # *
16
+ # * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ # * SOFTWARE.
23
+ # *
24
+ # * ***** END LICENSE BLOCK ***** */
25
+ #++
26
+
27
+
28
+ module RImageAnalysisTools
29
+
30
+ ##
31
+ # Thresholds the values in an array according to Otsu's method (Otsu, 1979, DOI: 10.1109/TSMC.1979.4310076).
32
+ # @param [Array] arr an array (1-D) containing the values to be thresholded.
33
+ # @return [Numeric] the threshold value; values <= to this number are below the threshold.
34
+ #
35
+ def self.graythresh(arr)
36
+
37
+ num_steps = 1000.0
38
+
39
+ min = arr.reduce(Float::MAX) { |a,e| e < a ? e : a }
40
+ max = arr.reduce(-1.0*Float::MAX) { |a,e| e > a ? e : a }
41
+ mean = arr.reduce(0.0) { |a,e| a + e } / arr.length
42
+
43
+ sorted = arr.sort
44
+
45
+ step_size = (max - min)/(num_steps)
46
+
47
+ best_eta = 0.0
48
+ best_threshold_value = 0.0
49
+
50
+ counts_upto_k = 0
51
+
52
+ mu = 0
53
+
54
+ min.step(max, step_size) do |k|
55
+
56
+ last_count_upto_k = counts_upto_k
57
+
58
+ last_count_upto_k.upto(sorted.length-1) do |i|
59
+
60
+ if sorted[i] > k then
61
+ break
62
+ end
63
+
64
+ counts_upto_k += 1
65
+ mu += sorted[i]*1.0/sorted.length
66
+
67
+ end
68
+
69
+ omega = counts_upto_k*1.0 / sorted.length
70
+
71
+ eta = omega*(1-omega) * ((mean - mu)/(1-omega) - mu/omega)**2
72
+
73
+ if eta > best_eta then
74
+ best_eta = eta
75
+ best_threshold_value = k
76
+ end
77
+
78
+ end
79
+
80
+ best_threshold_value
81
+
82
+ end
83
+
84
+ end
@@ -0,0 +1,94 @@
1
+ #--
2
+ # /* ***** BEGIN LICENSE BLOCK *****
3
+ # *
4
+ # * Copyright (c) 2012 Colin J. Fuller
5
+ # *
6
+ # * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # * of this software and associated documentation files (the Software), to deal
8
+ # * in the Software without restriction, including without limitation the rights
9
+ # * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # * copies of the Software, and to permit persons to whom the Software is
11
+ # * furnished to do so, subject to the following conditions:
12
+ # *
13
+ # * The above copyright notice and this permission notice shall be included in
14
+ # * all copies or substantial portions of the Software.
15
+ # *
16
+ # * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ # * SOFTWARE.
23
+ # *
24
+ # * ***** END LICENSE BLOCK ***** */
25
+ #++
26
+
27
+ module RImageAnalysisTools
28
+
29
+ class ImageWriter
30
+
31
+ java_import Java::loci.formats.ImageWriter
32
+ java_import Java::ome.xml.model.enums.DimensionOrder
33
+ java_import Java::ome.xml.model.enums.PixelType
34
+ java_import Java::loci.formats.FormatTools
35
+ java_import Java::java.nio.ByteBuffer
36
+
37
+
38
+ def initialize
39
+
40
+ end
41
+
42
+ def write(filename, image_to_write)
43
+
44
+ wr = ImageWriter.new
45
+
46
+ m = image_to_write.getMetadata
47
+
48
+ pixels = image_to_write.getPixelData
49
+
50
+ pixel_array = pixels.toImagePlus.getProcessor.getPixels
51
+
52
+ m.setPixelsDimensionOrder(DimensionOrder.fromString(pixels.getDimensionOrder.upcase), 0)
53
+
54
+ m.setPixelsType(PixelType.fromString(FormatTools.getPixelTypeString(pixels.getDataType)), 0)
55
+
56
+ wr.setMetadataRetrieve(m)
57
+
58
+ File.unlink(filename) if File.exist?(filename)
59
+
60
+ wr.setId(filename)
61
+
62
+ pixels.getNumPlanes.times do |i|
63
+
64
+ c = i % pixels::size_c
65
+ z = ((i - c)/pixels::size_c) % pixels::size_z
66
+ t = (i - c - pixels::size_c * z)/(pixels::size_z * pixels::size_c)
67
+
68
+ conv_index = pixels.toImagePlus.getStackIndex(c+1, z+1, t+1)
69
+
70
+ pixels.toImagePlus.setSliceWithoutUpdate(conv_index)
71
+ pixelData = pixels.toImagePlus.getProcessor.getPixels
72
+
73
+ size_to_allocate = pixels::size_x * pixels::size_y * FormatTools.getBytesPerPixel(pixels::dataType)
74
+
75
+ out = ByteBuffer.allocate(size_to_allocate)
76
+
77
+ out.asFloatBuffer.put(pixelData)
78
+
79
+ wr.savePlane(i, out.array)
80
+
81
+ end
82
+
83
+ wr.close
84
+
85
+ end
86
+
87
+ end
88
+
89
+ end
90
+
91
+
92
+
93
+
94
+
@@ -0,0 +1,70 @@
1
+ #--
2
+ # /* ***** BEGIN LICENSE BLOCK *****
3
+ # *
4
+ # * Copyright (c) 2012 Colin J. Fuller
5
+ # *
6
+ # * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # * of this software and associated documentation files (the Software), to deal
8
+ # * in the Software without restriction, including without limitation the rights
9
+ # * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # * copies of the Software, and to permit persons to whom the Software is
11
+ # * furnished to do so, subject to the following conditions:
12
+ # *
13
+ # * The above copyright notice and this permission notice shall be included in
14
+ # * all copies or substantial portions of the Software.
15
+ # *
16
+ # * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ # * SOFTWARE.
23
+ # *
24
+ # * ***** END LICENSE BLOCK ***** */
25
+ #++
26
+
27
+ require 'rimageanalysistools/initjava'
28
+
29
+ java_import Java::edu.stanford.cfuller.imageanalysistools.image.ReadOnlyImageImpl
30
+ java_import Java::edu.stanford.cfuller.imageanalysistools.image.WritableImageImpl
31
+ java_import Java::edu.stanford.cfuller.imageanalysistools.image.ImageCoordinate
32
+
33
+ class ReadOnlyImageImpl
34
+
35
+ def [](ic)
36
+ getValue(ic)
37
+ end
38
+
39
+ end
40
+
41
+ class WritableImageImpl
42
+
43
+ def []=(ic, value)
44
+ setValue(ic, value)
45
+ end
46
+
47
+ end
48
+
49
+ class ImageCoordinate
50
+
51
+ Lookups = {x: X, y: Y, z: Z, c: C, t: T}
52
+
53
+ def [](dim)
54
+ get(Lookups[dim])
55
+ end
56
+
57
+ def []=(dim, value)
58
+ set(Lookups[dim], value)
59
+ end
60
+
61
+ def self.[](*values)
62
+
63
+ createCoordXYZCT(*values)
64
+
65
+ end
66
+
67
+ end
68
+
69
+
70
+
@@ -0,0 +1,34 @@
1
+ #--
2
+ # /* ***** BEGIN LICENSE BLOCK *****
3
+ # *
4
+ # * Copyright (c) 2012 Colin J. Fuller
5
+ # *
6
+ # * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # * of this software and associated documentation files (the Software), to deal
8
+ # * in the Software without restriction, including without limitation the rights
9
+ # * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # * copies of the Software, and to permit persons to whom the Software is
11
+ # * furnished to do so, subject to the following conditions:
12
+ # *
13
+ # * The above copyright notice and this permission notice shall be included in
14
+ # * all copies or substantial portions of the Software.
15
+ # *
16
+ # * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ # * SOFTWARE.
23
+ # *
24
+ # * ***** END LICENSE BLOCK ***** */
25
+ #++
26
+
27
+ require 'java'
28
+ require 'ImageAnalysisTools.jar'
29
+
30
+
31
+
32
+
33
+
34
+
@@ -0,0 +1,114 @@
1
+ #--
2
+ # /* ***** BEGIN LICENSE BLOCK *****
3
+ # *
4
+ # * Copyright (c) 2012 Colin J. Fuller
5
+ # *
6
+ # * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # * of this software and associated documentation files (the Software), to deal
8
+ # * in the Software without restriction, including without limitation the rights
9
+ # * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # * copies of the Software, and to permit persons to whom the Software is
11
+ # * furnished to do so, subject to the following conditions:
12
+ # *
13
+ # * The above copyright notice and this permission notice shall be included in
14
+ # * all copies or substantial portions of the Software.
15
+ # *
16
+ # * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ # * SOFTWARE.
23
+ # *
24
+ # * ***** END LICENSE BLOCK ***** */
25
+ #++
26
+
27
+ require 'rimageanalysistools'
28
+ require 'rimageanalysistools/method/method'
29
+
30
+ java_import Java::edu.stanford.cfuller.imageanalysistools.image.ImageFactory
31
+ java_import Java::edu.stanford.cfuller.imageanalysistools.metric.IntensityPerPixelMetric
32
+ java_import Java::edu.stanford.cfuller.imageanalysistools.filter.LocalMaximumSeparabilityThresholdingFilter
33
+ java_import Java::edu.stanford.cfuller.imageanalysistools.filter.LabelFilter
34
+ java_import Java::edu.stanford.cfuller.imageanalysistools.filter.RecursiveMaximumSeparabilityFilter
35
+ java_import Java::edu.stanford.cfuller.imageanalysistools.filter.RelabelFilter
36
+ java_import Java::edu.stanford.cfuller.imageanalysistools.filter.SizeAbsoluteFilter
37
+ java_import Java::edu.stanford.cfuller.imageanalysistools.filter.RenormalizationFilter
38
+ java_import Java::edu.stanford.cfuller.imageanalysistools.filter.BandpassFilter
39
+
40
+ module RImageAnalysisTools
41
+
42
+ class CentromereFindingMethod < Method
43
+
44
+ def centromere_finding(im)
45
+
46
+ im = ImageFactory.createWritable(im)
47
+
48
+ filters = []
49
+
50
+ filters << LocalMaximumSeparabilityThresholdingFilter.new
51
+ filters << LabelFilter.new
52
+ filters << RecursiveMaximumSeparabilityFilter.new
53
+ filters << RelabelFilter.new
54
+ filters << SizeAbsoluteFilter.new
55
+ filters << RelabelFilter.new
56
+
57
+ filters.each do |f|
58
+
59
+ f.setParameters(parameters)
60
+ f.setReferenceImage(input_images.getMarkerImage)
61
+ f.apply(im)
62
+
63
+ end
64
+
65
+ im
66
+
67
+ end
68
+
69
+ def normalize_input_image(im)
70
+
71
+ rnf = RenormalizationFilter.new
72
+ rnf.setParameters(parameters)
73
+ rnf.apply(im)
74
+
75
+ end
76
+
77
+
78
+ def go
79
+
80
+ mask = ImageFactory.createWritable(input_images.getMarkerImage)
81
+
82
+ normalize_input_image(mask)
83
+
84
+ bf = BandpassFilter.new
85
+
86
+ bf.setParameters(parameters)
87
+
88
+ band_lower = 3.0
89
+ band_upper = 4.0
90
+
91
+ bf.setBand(band_lower, band_upper)
92
+
93
+ bf.apply(mask)
94
+
95
+ mask = centromere_finding(mask)
96
+
97
+ m = IntensityPerPixelMetric.new
98
+
99
+ q = m.quantify(mask, input_images)
100
+
101
+ self.quantification= q
102
+
103
+ mask
104
+
105
+ end
106
+
107
+
108
+ end
109
+
110
+ end
111
+
112
+
113
+
114
+