Canny edge detector: Difference between revisions

Line 690:
partition =: 4 : '2 0 3 1 |: ((1{x) ([+\]) (2 0 1|: ((0{x) ([+\]) y)))'
apply =: 4 : '+/"1 (+/"2 (x *"2 y))'
max3x3 =: 3 : '(1{1{y>0) * (>./,y)'"2
addborder =: (0&,@|:@|.)^:4
normalize =: ]%+/@,
Line 697 ⟶ 698:
|: w{. w0}. |: h{. h0}. y
)
max3x3 =: 3 : '(1{1{y>0) * (>./,y)'"2
attach =: 3 : 'max3x3 (3 3 partition (addborder y))'
unique =: 3 : 'y*i.$y'
connect =: 43 : 'attach^:x_ unique y'
 
canny =: 3 : 0
Line 755:
NB. Detect the edges (sets of connected points)
 
image5 =: 1000 connect image4
)
 
)</lang>
The above implementation solves the 'inner problem' of Canny Edge Detection in the J, with no external dependencies. Standard libraries provide additional support including interfaces to image file formats and graphic displays.
Image file libraries for different image formats import into and export from a generalized data structure, an array of pixels with three or four color channels. The following code invokes these standard libraries, and also converts between the import format and the monochromatic representation used here for edge detection.
Line 772:
NB. image =: readbmp 'chess.bmp'
NB. image =: readpng 'boat1.png'
NB. image =: readpng 'valve.png'
NB. image =: readjpeg 'lena.jpg'
image =: readpng 'valve.png'
 
NB. image converted from 'Parrot: red macaw' - https://commons.wikimedia.org/wiki/File:Parrot.red.macaw.1.arp.750pix.jpg
image =: readpng 'bird.png'
 
NB. select or combine from color channels
NB. if original image is grayscale, take any color channel
red =: <. image % 65538(256^2)
green =: 256 | <. image % 256
blue =: 256 | image
Line 789 ⟶ 785:
load'canny.ijs'
edges =: canny image
viewmat edges
 
</lang>
Image uploads [temporarily?] blocked; herefall isback upperto leftascii corner of detected edgesart:
<lang j>
NB. toaget =:identifiers 3of : 0edges
 
min =: <./(,y)
max =: >~./(,y)edges
0 160 1317 1328 1186 484 932 941 369 950 526 713 1151 288 1257 3577 878 1100 2586 1875 760 1537 1422 3766 1463 3595 1685 16553 2597 2542 2412 2487 2693 3003 2449 2562 3079 3490 3113 3455 4033 4890 6351 8262 3820 3807 3728 3898 4511 4010 4260 4038 25776 412...
palette =: ' .*:o&8#@'
img =: (#palette)*(y-min)%(max-min)
img =: <."0 img
img =: 9|"0 img
img ({"0 1) palette
)
40{. 80{."1 toa image5
...........
.... ......
... ..... .....
... ............
.... ...... ..... .
... .. . . .. ... &
... .. . .. .. &
.. .. .... & &&&
... . .. . & &
.. . ...... & &&
.. . && && &&&
. . .. . &&&&&& &&&&
. .. . . && &&&&&&&&&
. . ... . &
. .. . . &
. * . .. .. **** &
. ** . ... .. **** ** ...
. ** ** . ** *** . ..
. .. * * .. ****** . **** .. .
. ... ** ** ... ***
. . * * @@@@ * ... .. **
*** @@ @@ * . . *
*** * @ @ * . .. * .. ......
** ** **** @@ @@ * .... * . .... ...
* **** ** @ @@ . * * . . .
** ** ** @ @@ * .. * . ..
* * * @ @ * .. * ... ..
* * ** * @ @ * . * . .
* *** * * @ @ * * ..
* ** **** @ @ * * ...
* ** * @ @ * * ..
. * ** @ @ . * * ....
 
NB. pick one to display
(edges=16553){' #'
#
# ###
# ####
# ### ##
# # ##
# ## ## #
## ## ### ## ##
###### #### ## ####
## ######### # ## ##
# # ###### ###
# ## # # ##
# ## # ## ##
# # ##
## # ##
########## ### ##
## ## ### ##
### ## ## #
## ## # #
## #
# ##
## #
#
####
## ##
## ##
## ###
# # ##
## ##
# ##
# ######
## # #####
# ## ## ##
# #### ### # ##
# # ## # # #
## # # # # ##
## # ## # ## ##
# ## # ## # ##
## # # ### ##
## ## # ## #
## ## # # ##
### # #### ## ##
## ## # # #
## ## ## ##
# ### # #
## ## ##### #
#### ## # #
## # # #
# # ## #
# ## ## #
## ### #
# # #### #
### # #
# ## #
## ## # #
## ## ### #
## ## # ### ## #
# # #### ##
##### # ## #
# # #### # #
## ## ## #
# #### # #
### # # #
## ## #
### ## # ##
# # ## ### #
### ## ## # #
## ## # #
### ## ## #
# # ## #
# ###### #
# ## # #
# # ## #
# # #
## # # #
## # ## #
## # ###### #
## # # ## #
## # # # #
### # # # #
#### # ## #
## # # #
# # #
# # #
# # #
# ## #
# ## #
# # # # #
## # # #
## ## # #
####### # #
# # ##
# #####
### # ##
# # ## #
# # ##
## ## ##
## ## #
## # # #
#### #
# ##
# ##
## ###
#
</lang>