Module: RImageAnalysisTools::Centroids

Defined in:
lib/rimageanalysistools/centroids.rb

Overview

Methods for calculating the geometric centroids of regions in an image

Class Method Summary collapse

Class Method Details

.calculate_centroids_2d(mask) ⇒ Hash

Calculates the centroid of all 2d regions in a mask. A region is defined as all pixels having the same nonzero greylevel in an image.

Parameters:

  • mask (Image)

    the mask containing the regions

Returns:

  • (Hash)

    a hash mapping the greylevel of each region to its 2d centroid stored as an array [x,y]



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rimageanalysistools/centroids.rb', line 48

def self.calculate_centroids_2d(mask)

  centroids = {}
  counts = {}

  mask.each do |ic|

    next unless mask[ic] > 0

    unless centroids[mask[ic]] then
      
      centroids[mask[ic]]= [0.0,0.0]
      counts[mask[ic]]= 0

    end

    centroids[mask[ic]][0]+= ic.get(ImageCoordinate::X)
    centroids[mask[ic]][1]+= ic.get(ImageCoordinate::Y)

    counts[mask[ic]]+= 1

  end

  centroids.each_key do |k|

    centroids[k].map! { |e| e/counts[k] }

  end

  centroids

end