Canny edge detector: Difference between revisions

m
syntax highlighting fixup automation
(added Perl programming solution)
m (syntax highlighting fixup automation)
Line 16:
=={{header|C}}==
The following program reads an 8 bits per pixel grayscale [[wp:BMP file format|BMP]] file and saves the result to `out.bmp'. Compile with `-lm'.
<langsyntaxhighlight lang=c>#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
Line 468:
free((pixel_t*)out_bitmap_data);
return 0;
}</langsyntaxhighlight>
 
=={{header|D}}==
{{trans|C}}
This version retains some of the style of the original C version. This code is faster than the C version, even with the DMD compiler. This version loads and saves PGM images, using the module of the Grayscale image Task.
<langsyntaxhighlight lang=d>import core.stdc.stdio, std.math, std.typecons, std.string, std.conv,
std.algorithm, std.ascii, std.array, bitmap, grayscale_image;
 
Line 672:
printf("Image size: %d x %d\n", imIn.nx, imIn.ny);
imIn.cannyEdgeDetection(45, 50, 1.0f).savePGM("lena_canny.pgm");
}</langsyntaxhighlight>
 
=={{header|Go}}==
Line 681:
 
Note that on Linux the extension of the example image file name needs to be changed from .PNG to .png in order for the library used to recognize it.
<langsyntaxhighlight lang=go>package main
 
import (
Line 704:
log.Fatal("Could not write Canny image to disk")
}
}</langsyntaxhighlight>
 
=={{header|J}}==
<p>In this solution images are represented as 2D arrays of pixels, with first and second axes representing down and right respectively. Each processing step has a specific pixel representation. In the original and Gaussian-filtered images, array elements represent monochromatic intensity values as numbers ranging from 0 (black) to 255 (white). In the intensity gradient image, gradient values are vectors, and are represented as complex numbers, with real and imaginary components representing down and right respectively. </p>
<p>Detected edge and non-edge points are represented as ones and zeros respectively. An edge is a set of connected edge points (points adjacent horizontally, vertically, or diagonally are considered to be connected). In the final image, each edge is represented by assigning its set of points a common unique value. </p>
<langsyntaxhighlight lang=J>NB. 2D convolution, filtering, ...
 
convolve =: 4 : 'x apply (($x) partition y)'
Line 798:
canny =: step5 @ step4 @ step3 @ step2 @ step1
 
</syntaxhighlight>
</lang>
<p>The above implementation solves the 'inner problem' of Canny Edge Detection in the J language, with no external dependencies. J's Qt IDE provides additional support including interfaces to image file formats, graphic displays, and the user. The following code exercises these features</p>
 
<p>The file 'valve.png' referenced in this code is from one of several Wikipedia articles on edge detection. It can be viewed at [https://upload.wikimedia.org/wikipedia/commons/2/2e/Valve_gaussian_%282%29.PNG[https://upload.wikimedia.org/wikipedia/commons/2/2e/Valve_gaussian_%282%29.PNG]]</p>
<syntaxhighlight lang=J>
<lang J>
require 'gl2'
coclass 'edge'
Line 860:
form_close=: exit bind 0
 
run''</langsyntaxhighlight>
 
 
Line 867:
 
Se implementa utilizando una sola clase Java. [It is implemented using a single Java class.]
<langsyntaxhighlight lang=Java>import java.awt.image.BufferedImage;
import java.util.Arrays;
 
Line 1,430:
}
}</langsyntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
<langsyntaxhighlight lang=julia>using Images
 
canny_edges = canny(img, sigma = 1.4, upperThreshold = 0.80, lowerThreshold = 0.20)</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight lang=Mathematica>Export["out.bmp", EdgeDetect[Import[InputString[]]]];</langsyntaxhighlight>
Mathematica uses canny edge detection by default. This seems so cheaty next to all of these giant answers...
 
=={{header|MATLAB}} / {{header|Octave}}==
There is a function in the image processing toolbox [http://www.mathworks.com/help/images/ref/edge.html edge], that has Canny Edge Detection as one of its options.
<langsyntaxhighlight lang=MATLAB>BWImage = edge(GrayscaleImage,'canny');</langsyntaxhighlight>
 
=={{header|Nim}}==
Line 1,451:
We use the PNG image present on Wikipedia article as input and produce a PNG grayscale image as result.
 
<langsyntaxhighlight lang=Nim>import lenientops
import math
import nimPNG
Line 1,641:
echo "File ", Input, " processed. Result is available in file ", Output
else:
echo "Error: ", status.error</langsyntaxhighlight>
 
=={{header|Perl}}==
Used a [https://raw.githubusercontent.com/abend/Image-EdgeDetect/master/lib/Image/EdgeDetect.pm non-CPAN module] by [https://github.com/abend Sasha Kovar]
<langsyntaxhighlight lang=perl># 20220120 Perl programming solution
 
use strict;
Line 1,654:
 
my $detector = Image::EdgeDetect->new();
$detector->process('./input.jpg', './output.jpg') or die; # na.cx/i/pHYdUrV.jpg</langsyntaxhighlight>
Output: [https://drive.google.com/file/d/1ww21DHz-IQTaFKNLC2I2No_fHUehYooG/view (Offsite image file) ]
 
=={{header|PHP}}==
PHP implementation
<langsyntaxhighlight lang=PHP>
// input: r,g,b in range 0..255
function RGBtoHSV($r, $g, $b) {
Line 1,741:
imagedestroy($im);
 
</syntaxhighlight>
</lang>
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
Ported from demo\Arwen32dibdemo\manip.exw (menu entry Manipulate/Filter/Detect Edges, windows-32-bit only) to pGUI.
<!--<langsyntaxhighlight lang=Phix>(notonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Canny_Edge_Detection.exw
Line 1,809:
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Python}}==
Line 1,815:
In Python, Canny edge detection would normally be done using [http://scikit-image.org/docs/dev/auto_examples/plot_canny.html scikit-image] or OpenCV-Python. Here is an approach using numpy/scipy:
 
<langsyntaxhighlight lang=python>#!/bin/python
import numpy as np
from scipy.ndimage.filters import convolve, gaussian_filter
Line 1,900:
im = imread("test.jpg", mode="L") #Open image, convert to greyscale
finalEdges = CannyEdgeDetector(im)
imshow(finalEdges)</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 1,906:
 
cannyedge.c
<langsyntaxhighlight lang=c>#include <stdio.h>
#include <string.h>
#include <magick/MagickCore.h>
Line 1,935:
 
return 0;
}</langsyntaxhighlight>
cannyedge.raku
<syntaxhighlight lang=raku perl6line># 20220103 Raku programming solution
use NativeCall;
Line 1,948:
CArray[uint8].new( 'output.jpg'.encode.list, 0),
0e0, 2e0, 0.05e0, 0.05e0
)</langsyntaxhighlight>
{{out}}
<pre>export PKG_CONFIG_PATH=/usr/lib/pkgconfig
Line 1,957:
=={{header|Tcl}}==
{{libheader|crimp}}
<langsyntaxhighlight lang=tcl>package require crimp
package require crimp::pgm
 
Line 1,973:
writePGM $outputFile [crimp filter canny sobel [readPGM $inputFile]]
}
cannyFilterFile {*}$argv</langsyntaxhighlight>
 
=={{header|Wren}}==
Line 1,979:
{{libheader|DOME}}
{{libheader|Wren-check}}
<langsyntaxhighlight lang=ecmascript>import "dome" for Window
import "graphics" for Canvas, Color, ImageData
import "math" for Math
Line 2,177:
draw(alpha) {}
}
var Game = Canny.new("Valve_original.png", "Valve_monchrome_canny.png")</langsyntaxhighlight>
 
=={{header|Yabasic}}==
{{trans|Phix}}
<langsyntaxhighlight lang=Yabasic>// Rosetta Code problem: http://rosettacode.org/wiki/Canny_edge_detector
// Adapted from Phix to Yabasic by Galileo, 01/2022
 
Line 2,240:
dot x, y
next
next</langsyntaxhighlight>
10,333

edits