Canny edge detector: Difference between revisions

Content added Content deleted
Line 690: Line 690:
partition =: 4 : '2 0 3 1 |: ((1{x) ([+\]) (2 0 1|: ((0{x) ([+\]) y)))'
partition =: 4 : '2 0 3 1 |: ((1{x) ([+\]) (2 0 1|: ((0{x) ([+\]) y)))'
apply =: 4 : '+/"1 (+/"2 (x *"2 y))'
apply =: 4 : '+/"1 (+/"2 (x *"2 y))'
max3x3 =: 3 : '(1{1{y>0) * (>./,y)'"2
addborder =: (0&,@|:@|.)^:4
addborder =: (0&,@|:@|.)^:4
normalize =: ]%+/@,
normalize =: ]%+/@,
Line 697: Line 698:
|: w{. w0}. |: h{. h0}. y
|: w{. w0}. |: h{. h0}. y
)
)
max3x3 =: 3 : '(1{1{y>0) * (>./,y)'"2
attach =: 3 : 'max3x3 (3 3 partition (addborder y))'
attach =: 3 : 'max3x3 (3 3 partition (addborder y))'
unique =: 3 : 'y*i.$y'
unique =: 3 : 'y*i.$y'
connect =: 4 : 'attach^:x unique y'
connect =: 3 : 'attach^:_ unique y'


canny =: 3 : 0
canny =: 3 : 0
Line 755: Line 755:
NB. Detect the edges (sets of connected points)
NB. Detect the edges (sets of connected points)


image5 =: 1000 connect image4
image5 =: connect image4
)

)</lang>
</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.
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.
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: Line 772:
NB. image =: readbmp 'chess.bmp'
NB. image =: readbmp 'chess.bmp'
NB. image =: readpng 'boat1.png'
NB. image =: readpng 'boat1.png'
NB. image =: readpng 'valve.png'
NB. image =: readjpeg 'lena.jpg'
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. select or combine from color channels
NB. if original image is grayscale, take any color channel
NB. if original image is grayscale, take any color channel
red =: <. image % 65538
red =: <. image % (256^2)
green =: 256 | <. image % 256
green =: 256 | <. image % 256
blue =: 256 | image
blue =: 256 | image
Line 789: Line 785:
load'canny.ijs'
load'canny.ijs'
edges =: canny image
edges =: canny image
viewmat edges

</lang>
</lang>
Image uploads [temporarily?] blocked; here is upper left corner of detected edges:
Image uploads [temporarily?] blocked; fall back to ascii art:
<lang j>
<lang j>
toa =: 3 : 0
NB. get identifiers of edges

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>
</lang>