Child of Canny Edge Detector

No preview image

1 collaborator

Default-person Bryan Head (Author)

Tags

(This model has yet to be categorized with any tags)
Child of model Canny Edge Detector preview imageCanny Edge Detector
Model group EECS 372/472 2013 | Visible to everyone | Changeable by everyone
Model was written in NetLogo 5.0.4 • Viewed 145 times • Downloaded 11 times • Run 0 times
Download the 'Child of Canny Edge Detector' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

extensions [gst-video bitmap matrix]

globals [cam-is-inited? xconv yconv]

patches-own [
  bw
  grad
  chemical             ;; amount of chemical on this patch
  food                 ;; amount of food on this patch (0, 1, or 2)
  nest?                ;; true on nest patches, false elsewhere
  nest-scent           ;; number that is higher closer to the nest
  food-source-number   ;; number (1, 2, or 3) to identify the food sources
  edge?
]

to start-webcam
  if (not (cam-is-inited? = true)) [
    gst-video:camera-init
    set cam-is-inited? true
  ]
  gst-video:camera-start
end 

to stop-webcam
  gst-video:camera-stop
end 

to display-mirror-to-patches
  cam-mirror-to-patches
  display
end 

to cam-mirror-to-patches
  if (gst-video:camera-is-rolling?) [
    bitmap:copy-to-pcolors gst-video:camera-image false
  ]
end 

to go
  no-display
  cam-mirror-to-patches
  ifelse detection-method != "None" [
    detect-edges
  ] [ ask patches [ set edge? false ] ]
  ant-go
  display
end 

to detect-edges
  ask patches [set bw brightness]
  ask patches [set grad gradient]
  let max-grad max [grad] of patches
  let normalizer 255 / max-grad
  ask patches with ([not nest? and food = 0]) [ set edge? normalizer * grad > thresh ]
end 

to-report brightness
  report (sum pcolor / 3) / 255
end 

to-report gradient
  let p1 [bw] of patch-at -1 1
  let p2 [bw] of patch-at 0 1
  let p3 [bw] of patch-at 1 1
  let p4 [bw] of patch-at -1 0
  let p5 [bw] of patch-at 0 0
  let p6 [bw] of patch-at 1 0
  let p7 [bw] of patch-at -1 -1
  let p8 [bw] of patch-at 0 -1
  let p9 [bw] of patch-at 1 -1
  if detection-method = "Scharr" [
    ; Scharr
    report abs((3 * p1 + 10 * p2 + 3 * p3) - (3 * p7 + 10 * p8 + 3 * p9)) + abs((3 * p3 + 10 * p6 + 3 * p9) - (3 * p1 + 10 * p4 + 3 * p7))
  ] 
  if detection-method = "Sobel" [ 
    ; Sobel
    report abs((p1 + 2 * p2 + p3) - (p7 + 2 * p8 + p9)) + abs((p3 + 2 * p6 + p9) - (p1 + 2 * p4 + p7))
  ]
  if detection-method = "Prewitt" [
    report abs((p1 + p2 + p3) - (p7 + p8 + p9)) + abs((p3 + p6 + p9) - (p1 + p4 + p7))
  ]
end 

;;;;;;;;;;;;;;;;;;;;;;;;
;;; Setup procedures ;;;
;;;;;;;;;;;;;;;;;;;;;;;;

to setup
  clear-all
  set-default-shape turtles "bug"
  crt population
  [ set size 2         ;; easier to see
    set color red  ]   ;; red = not carrying food
  setup-patches
  reset-ticks
end 

to setup-patches
  ask patches
  [ set edge? false
    setup-nest
    setup-food
    recolor-patch ]
end 

to setup-nest  ;; patch procedure
  ;; set nest? variable to true inside the nest, false elsewhere
  set nest? (distancexy 0 0) < 5
  ;; spread a nest-scent over the whole world -- stronger near the nest
  set nest-scent 200 - distancexy 0 0
end 

to setup-food  ;; patch procedure
  ;; setup food source one on the right
  if (distancexy (0.6 * max-pxcor) 0) < 5
  [ set food-source-number 1 ]
  ;; setup food source two on the lower-left
  if (distancexy (-0.6 * max-pxcor) (-0.6 * max-pycor)) < 5
  [ set food-source-number 2 ]
  ;; setup food source three on the upper-left
  if (distancexy (-0.8 * max-pxcor) (0.8 * max-pycor)) < 5
  [ set food-source-number 3 ]
  ;; set "food" at sources to either 1 or 2, randomly
  if food-source-number > 0
  [ set food one-of [1 2] ]
end 

to recolor-patch  ;; patch procedure
  ;; give color to nest and food sources
  ifelse nest?
  [ set pcolor violet ]
  [ ifelse food > 0
    [ if food-source-number = 1 [ set pcolor cyan ]
      if food-source-number = 2 [ set pcolor sky  ]
      if food-source-number = 3 [ set pcolor blue ] ]
    ;; scale color to show chemical concentration
    
    [ ifelse edge? [ set pcolor brown ][ set pcolor scale-color green chemical 0.1 5 ] ]]
end 

;;;;;;;;;;;;;;;;;;;;;
;;; Go procedures ;;;
;;;;;;;;;;;;;;;;;;;;;

to ant-go  ;; forever button
  ask turtles
  [ if who >= ticks [ stop ] ;; delay initial departure
    ifelse color = red
    [ look-for-food  ]       ;; not carrying food? look for it
    [ return-to-nest ]       ;; carrying food? take it back to nest
    wiggle
    let max-tries 100
    while [[edge?] of patch-ahead 1 and max-tries > 0] [
      wiggle
      set max-tries max-tries - 1
    ]
    if [not edge?] of patch-ahead 1 [ fd 1 ]
  ]
  diffuse chemical (diffusion-rate / 100)
  ask patches
  [ set chemical chemical * (100 - evaporation-rate) / 100  ;; slowly evaporate chemical
    recolor-patch ]
  tick
end 

to return-to-nest  ;; turtle procedure
  ifelse nest?
  [ ;; drop food and head out again
    set color red
    rt 180 ]
  [ set chemical chemical + 60  ;; drop some chemical
    uphill-nest-scent ]         ;; head toward the greatest value of nest-scent
end 

to look-for-food  ;; turtle procedure
  if food > 0
  [ set color orange + 1     ;; pick up food
    set food food - 1        ;; and reduce the food source
    rt 180                   ;; and turn around
    stop ]
  ;; go in the direction where the chemical smell is strongest
  if (chemical >= 0.05) and (chemical < 2)
  [ uphill-chemical ]
end 

;; sniff left and right, and go where the strongest smell is

to uphill-chemical  ;; turtle procedure
  let scent-ahead chemical-scent-at-angle   0
  let scent-right chemical-scent-at-angle  45
  let scent-left  chemical-scent-at-angle -45
  if (scent-right > scent-ahead) or (scent-left > scent-ahead)
  [ ifelse scent-right > scent-left
    [ rt 45 ]
    [ lt 45 ] ]
end 

;; sniff left and right, and go where the strongest smell is

to uphill-nest-scent  ;; turtle procedure
  let scent-ahead nest-scent-at-angle   0
  let scent-right nest-scent-at-angle  45
  let scent-left  nest-scent-at-angle -45
  if (scent-right > scent-ahead) or (scent-left > scent-ahead)
  [ ifelse scent-right > scent-left
    [ rt 45 ]
    [ lt 45 ] ]
end 

to wiggle  ;; turtle procedure
  rt random 40
  lt random 40
  if not can-move? 1 [ rt 180 ]
end 

to-report nest-scent-at-angle [angle]
  let p patch-right-and-ahead angle 1
  if p = nobody [ report 0 ]
  report [nest-scent] of p
end 

to-report chemical-scent-at-angle [angle]
  let p patch-right-and-ahead angle 1
  if p = nobody [ report 0 ]
  report [chemical] of p
end 


; Copyright 1997 Uri Wilensky.
; See Info tab for full copyright and license.

There is only one version of this model, created about 6 years ago by Bryan Head.

Attached files

No files

Parent: Canny Edge Detector

This model does not have any descendants.

Graph of models related to 'Child of Canny Edge Detector'