Class: RImageAnalysisTools::Drawing

Inherits:
Object
  • Object
show all
Defined in:
lib/rimageanalysistools/drawing.rb

Overview

A collection of methods for drawing into an existing image.

Instance Method Summary collapse

Instance Method Details

#circle(im, center, radius) ⇒ void

This method returns an undefined value.

Draws a circle into an image.

Parameters:

  • im (WritableImage)

    the image into which to draw

  • center (Array)

    an array containing the x,y coordinates of the circle’s center

  • radius (Numeric)

    the radius of the circle in pixels



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/rimageanalysistools/drawing.rb', line 142

def circle(im, center, radius)

  lower = ImageCoordinate[center[0]-radius-1, center[1]-radius-1, 0,0,0]
  upper = ImageCoordinate[center[0]+radius+1, center[1]+radius+1, 0,0,0]

  im.box_conservative(lower, upper, [:x, :y])

  draw(im) do |ic|
    
    xdiff = ic[:x] - center[0]
    ydiff = ic[:y] - center[1]

    if (Math.hypot(xdiff, ydiff) - radius).abs <= Math.sqrt(2) then
      true
    else
      false
    end

  end

  lower.recycle
  upper.recycle
    
end

#draw(im, draw_value = 255.0) ⇒ void

This method returns an undefined value.

A basic drawing method that iterates through an entire image. At each coordinate, an attached block is evaluated for a boolean response that determines whether that coordinate is overwritten with a specified value. The attached block will be given a single parameter, which is the current ImageCoordinate.

Parameters:

  • im (WritableImage)

    the image being drawn on

  • draw_value (Numeric) (defaults to: 255.0)

    the value to which a pixel will be set if the block evaluates to a true value at that coordinate



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rimageanalysistools/drawing.rb', line 96

def draw(im, draw_value = 255.0)

  im.each do |ic|

    if (yield ic) then

      im.setValue(ic, draw_value)

    end

  end

end

#draw_shape(im, location, shape_name = :circle, shape_parameters = 10) ⇒ void

This method returns an undefined value.

Draws a specified shape into an image.

Parameters:

  • im (WritableImage)

    the image into which to draw

  • location (Array)

    an array containing the coordinates of the shape to draw. The exact meaning of this parameter depends on which shape is being drawn.

  • shape_name (Symbol) (defaults to: :circle)

    the name of the shape to draw. This should correspond to one of the methods of this class.

  • shape_parameters (defaults to: 10)

    the parameters for drawing the specified shape; the meaning of this parameter depends on the shape.



123
124
125
126
127
128
129
130
131
# File 'lib/rimageanalysistools/drawing.rb', line 123

def draw_shape(im, location, shape_name=:circle, shape_parameters= 10)

  if self.respond_to?(shape_name) then

    self.send(shape_name, im, location, shape_parameters)

  end

end

#ellipse(im, foci, radius_inc) ⇒ void

This method returns an undefined value.

Draws an ellipse into an image.

Parameters:

  • im (WritableImage)

    the image into which to draw

  • foci (Array)

    an array containing two arrays, each of which contains the x,y coordinates of one focus of the ellipse

  • radius_inc (Numeric)

    the extra amount of distance (in pixels) beyond the distance between the foci that the ellipse should be drawn. (2*radius_inc + dist between foci = major axis length)



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/rimageanalysistools/drawing.rb', line 179

def ellipse(im, foci, radius_inc)

  min_x = foci[0][0]
  max_x = foci[1][0]
  min_y = foci[0][1]
  max_y = foci[1][1]

  if foci[1][0] < min_x then
    min_x = foci[1][0]
    max_x = foci[0][0]
  end

  if foci[1][1] < min_y then
    min_y = foci[1][1]
    max_y = foci[0][1]
  end

  radius = radius_inc + Math.hypot(max_x-min_x, max_y-min_y)
  
  lower = ImageCoordinate[min_x-radius-1, min_y-radius-1, 0,0,0]
  upper = ImageCoordinate[max_x+radius+1, max_y+radius+1, 0,0,0]

  im.box_conservative(lower, upper, [:x, :y])
  
  draw(im) do |ic|
    
    xdiff0 = ic[:x] - foci[0][0]
    ydiff0 = ic[:y] - foci[0][1]
    xdiff1 = ic[:x] - foci[1][0]
    ydiff1 = ic[:y] - foci[1][1]

    if (Math.hypot(xdiff0, ydiff0) + Math.hypot(xdiff1, ydiff1) - radius).abs <= Math.sqrt(2) then
      true
    else
      false
    end

  end


end