Image convolution: Difference between revisions

Content added Content deleted
(→‎{{header|J}}: bugfixes (incidentally adding a j903 version requirement), and make kernels more easily generalizable (to 5x5, 7x7, etc.))
m (syntax highlighting fixup automation)
Line 17: Line 17:
=={{header|Action!}}==
=={{header|Action!}}==
{{libheader|Action! Bitmap tools}}
{{libheader|Action! Bitmap tools}}
<lang Action!>INCLUDE "H6:LOADPPM5.ACT"
<syntaxhighlight lang="action!">INCLUDE "H6:LOADPPM5.ACT"


DEFINE HISTSIZE="256"
DEFINE HISTSIZE="256"
Line 109: Line 109:
DO UNTIL CH#$FF OD
DO UNTIL CH#$FF OD
CH=$FF
CH=$FF
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Image_convolution.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Image_convolution.png Screenshot from Atari 8-bit computer]
Line 115: Line 115:
=={{header|Ada}}==
=={{header|Ada}}==
First we define floating-point stimulus and color pixels which will be then used for filtration:
First we define floating-point stimulus and color pixels which will be then used for filtration:
<lang ada>type Float_Luminance is new Float;
<syntaxhighlight lang="ada">type Float_Luminance is new Float;


type Float_Pixel is record
type Float_Pixel is record
Line 149: Line 149:
begin
begin
return (To_Luminance (X.R), To_Luminance (X.G), To_Luminance (X.B));
return (To_Luminance (X.R), To_Luminance (X.G), To_Luminance (X.B));
end To_Pixel;</lang>
end To_Pixel;</syntaxhighlight>
Float_Luminance is an unconstrained equivalent of Luminance. Float_Pixel is one to Pixel. Conversion operations To_Luminance and To_Pixel saturate the corresponding values. The operation + is defined per channels. The operation * is defined as multiplying by a scalar. (I.e. Float_Pixel is a vector space.)
Float_Luminance is an unconstrained equivalent of Luminance. Float_Pixel is one to Pixel. Conversion operations To_Luminance and To_Pixel saturate the corresponding values. The operation + is defined per channels. The operation * is defined as multiplying by a scalar. (I.e. Float_Pixel is a vector space.)


Now we are ready to implement the filter. The operation is performed in memory. The access to the image array is minimized using a slid window. The filter is in fact a triplet of filters handling each image channel independently. It can be used with other color models as well.
Now we are ready to implement the filter. The operation is performed in memory. The access to the image array is minimized using a slid window. The filter is in fact a triplet of filters handling each image channel independently. It can be used with other color models as well.
<lang ada>type Kernel_3x3 is array (-1..1, -1..1) of Float_Luminance;
<syntaxhighlight lang="ada">type Kernel_3x3 is array (-1..1, -1..1) of Float_Luminance;


procedure Filter (Picture : in out Image; K : Kernel_3x3) is
procedure Filter (Picture : in out Image; K : Kernel_3x3) is
Line 198: Line 198:
Above (Picture'Last (2)) := W21;
Above (Picture'Last (2)) := W21;
end loop;
end loop;
end Filter;</lang>
end Filter;</syntaxhighlight>
Example of use:
Example of use:
<lang ada> F1, F2 : File_Type;
<syntaxhighlight lang="ada"> F1, F2 : File_Type;
begin
begin
Open (F1, In_File, "city.ppm");
Open (F1, In_File, "city.ppm");
Line 211: Line 211:
Put_PPM (F2, X);
Put_PPM (F2, X);
end;
end;
Close (F2);</lang>
Close (F2);</syntaxhighlight>


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
Line 217: Line 217:
[[Image:original_bbc.jpg|right]]
[[Image:original_bbc.jpg|right]]
[[Image:sharpened_bbc.jpg|right]]
[[Image:sharpened_bbc.jpg|right]]
<lang bbcbasic> Width% = 200
<syntaxhighlight lang="bbcbasic"> Width% = 200
Height% = 200
Height% = 200
Line 262: Line 262:
REPEAT
REPEAT
WAIT 1
WAIT 1
UNTIL FALSE</lang>
UNTIL FALSE</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
Line 268: Line 268:
Interface:
Interface:


<lang c>image filter(image img, double *K, int Ks, double, double);</lang>
<syntaxhighlight lang="c">image filter(image img, double *K, int Ks, double, double);</syntaxhighlight>


The implementation (the <tt>Ks</tt> argument is so that 1 specifies a 3&times;3 matrix, 2 a 5&times;5 matrix ...
The implementation (the <tt>Ks</tt> argument is so that 1 specifies a 3&times;3 matrix, 2 a 5&times;5 matrix ...
N a (2N+1)&times;(2N+1) matrix).
N a (2N+1)&times;(2N+1) matrix).


<lang c>#include "imglib.h"
<syntaxhighlight lang="c">#include "imglib.h"


inline static color_component GET_PIXEL_CHECK(image img, int x, int y, int l) {
inline static color_component GET_PIXEL_CHECK(image img, int x, int y, int l) {
Line 311: Line 311:
}
}
return NULL;
return NULL;
}</lang>
}</syntaxhighlight>


Usage example:
Usage example:
Line 317: Line 317:
The <tt>read_image</tt> function is from [[Read image file through a pipe|here]].
The <tt>read_image</tt> function is from [[Read image file through a pipe|here]].


<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include "imglib.h"
#include "imglib.h"


Line 377: Line 377:
free_img(ii);
free_img(ii);
} else { fprintf(stderr, "err reading %s\n", input); }
} else { fprintf(stderr, "err reading %s\n", input); }
}</lang>
}</syntaxhighlight>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Uses the RGB pixel buffer package defined here [[Basic bitmap storage#Common Lisp]]. Also the PPM file IO functions defined in
Uses the RGB pixel buffer package defined here [[Basic bitmap storage#Common Lisp]]. Also the PPM file IO functions defined in
[[Bitmap/Read a PPM file#Common_Lisp]] and [[Bitmap/Write a PPM file#Common_Lisp]] merged into one package.
[[Bitmap/Read a PPM file#Common_Lisp]] and [[Bitmap/Write a PPM file#Common_Lisp]] merged into one package.
<lang lisp>(load "rgb-pixel-buffer")
<syntaxhighlight lang="lisp">(load "rgb-pixel-buffer")
(load "ppm-file-io")
(load "ppm-file-io")


Line 450: Line 450:
(loop for pars being the hash-values of convolve::*cnv-lib*
(loop for pars being the hash-values of convolve::*cnv-lib*
do (princ (convolve::convolve "lena_color.ppm" pars)) (terpri)))
do (princ (convolve::convolve "lena_color.ppm" pars)) (terpri)))
</syntaxhighlight>
</lang>


=={{header|D}}==
=={{header|D}}==
This requires the module from the Grayscale Image Task.
This requires the module from the Grayscale Image Task.
<lang d>import std.string, std.math, std.algorithm, grayscale_image;
<syntaxhighlight lang="d">import std.string, std.math, std.algorithm, grayscale_image;


struct ConvolutionFilter {
struct ConvolutionFilter {
Line 569: Line 569:
img.convolve(filter)
img.convolve(filter)
.savePGM(format("lenna_gray_%s.ppm", filter.name));
.savePGM(format("lenna_gray_%s.ppm", filter.name));
}</lang>
}</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
Using standard image library:
Using standard image library:
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 663: Line 663:
fmt.Println(err)
fmt.Println(err)
}
}
}</lang>
}</syntaxhighlight>
Alternative version, building on code from bitmap task.
Alternative version, building on code from bitmap task.


New function for raster package:
New function for raster package:
<lang go>package raster
<syntaxhighlight lang="go">package raster


import "math"
import "math"
Line 738: Line 738:
}
}
return r
return r
}</lang>
}</syntaxhighlight>
Demonstration program:
Demonstration program:
<lang go>package main
<syntaxhighlight lang="go">package main


// Files required to build supporting package raster are found in:
// Files required to build supporting package raster are found in:
Line 779: Line 779:
fmt.Println(err)
fmt.Println(err)
}
}
}</lang>
}</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==


<lang J>NB. pad the edges of an array with border pixels
<syntaxhighlight lang="j">NB. pad the edges of an array with border pixels
NB. (increasing the first two dimensions by 1 less than the kernel size)
NB. (increasing the first two dimensions by 1 less than the kernel size)
pad=: {{
pad=: {{
Line 793: Line 793:
kernel_filter=: {{
kernel_filter=: {{
[: (0 >. 255 <. <.@:+&0.5) (1,:$m)+/ .*~&(,/)&m;._3 m pad
[: (0 >. 255 <. <.@:+&0.5) (1,:$m)+/ .*~&(,/)&m;._3 m pad
}}</lang>
}}</syntaxhighlight>




Line 800: Line 800:
Example use:
Example use:


<lang J>NB. kernels borrowed from C and TCL implementations
<syntaxhighlight lang="j">NB. kernels borrowed from C and TCL implementations
id_kernel=: (=&i.-)3 3
id_kernel=: (=&i.-)3 3
sharpen_kernel=: ({ _1,#@,)id_kernel
sharpen_kernel=: ({ _1,#@,)id_kernel
Line 807: Line 807:
sobel_emboss_kernel=: (i:-:<:3)*/1+(<.|.)i.3
sobel_emboss_kernel=: (i:-:<:3)*/1+(<.|.)i.3


'blurred.ppm' writeppm~ blur_kernel kernel_filter readppm 'original.ppm'</lang>
'blurred.ppm' writeppm~ blur_kernel kernel_filter readppm 'original.ppm'</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==


'''Code:'''
'''Code:'''
<lang Java>import java.awt.image.*;
<syntaxhighlight lang="java">import java.awt.image.*;
import java.io.File;
import java.io.File;
import java.io.IOException;
import java.io.IOException;
Line 952: Line 952:
return;
return;
}
}
}</lang>
}</syntaxhighlight>




Line 967: Line 967:


'''Code:'''
'''Code:'''
<lang javascript>// Image imageIn, Array kernel, function (Error error, Image imageOut)
<syntaxhighlight lang="javascript">// Image imageIn, Array kernel, function (Error error, Image imageOut)
// precondition: Image is loaded
// precondition: Image is loaded
// returns loaded Image to asynchronous callback function
// returns loaded Image to asynchronous callback function
Line 1,042: Line 1,042:
imageOut.src = can.toDataURL('image/png');
imageOut.src = can.toDataURL('image/png');
}</lang>
}</syntaxhighlight>


'''Example Usage:'''
'''Example Usage:'''
Line 1,070: Line 1,070:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>
<syntaxhighlight lang="julia">
using FileIO, Images
using FileIO, Images


Line 1,080: Line 1,080:


save("imagesharper.png", imfilt)
save("imagesharper.png", imfilt)
</syntaxhighlight>
</lang>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Java}}
{{trans|Java}}
<lang scala>// version 1.2.10
<syntaxhighlight lang="scala">// version 1.2.10


import kotlin.math.round
import kotlin.math.round
Line 1,199: Line 1,199:
}
}
writeOutputImage(args[1], dataArrays)
writeOutputImage(args[1], dataArrays)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,212: Line 1,212:
<br>
<br>
NB Things like convolution would be best done by combining LB with ImageMagick, which is easily called from LB.
NB Things like convolution would be best done by combining LB with ImageMagick, which is easily called from LB.
<syntaxhighlight lang="lb">
<lang lb>
dim result( 300, 300), image( 300, 300), mask( 100, 100)
dim result( 300, 300), image( 300, 300), mask( 100, 100)
w =128
w =128
Line 1,308: Line 1,308:
CallDLL #user32, "ReleaseDC", hw as ulong, hdc as ulong
CallDLL #user32, "ReleaseDC", hw as ulong, hdc as ulong
end
end
</syntaxhighlight>
</lang>
Screenview is available at [[http://www.diga.me.uk/convolved.gif]]
Screenview is available at [[http://www.diga.me.uk/convolved.gif]]


=={{header|Maple}}==
=={{header|Maple}}==
Builtin command ImageTools:-Convolution()
Builtin command ImageTools:-Convolution()
<lang Maple>pic:=Import("smiling_dog.jpg"):
<syntaxhighlight lang="maple">pic:=Import("smiling_dog.jpg"):
mask := Matrix([[1,2,3],[4,5,6],[7,8,9]]);
mask := Matrix([[1,2,3],[4,5,6],[7,8,9]]);
pic := ImageTools:-Convolution(pic, mask);</lang>
pic := ImageTools:-Convolution(pic, mask);</syntaxhighlight>


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Most image processing functions introduced in Mathematica 7
Most image processing functions introduced in Mathematica 7
<lang mathematica>img = Import[NotebookDirectory[] <> "Lenna50.jpg"];
<syntaxhighlight lang="mathematica">img = Import[NotebookDirectory[] <> "Lenna50.jpg"];
kernel = {{0, -1, 0}, {-1, 4, -1}, {0, -1, 0}};
kernel = {{0, -1, 0}, {-1, 4, -1}, {0, -1, 0}};
ImageConvolve[img, kernel]
ImageConvolve[img, kernel]
ImageConvolve[img, GaussianMatrix[35] ]
ImageConvolve[img, GaussianMatrix[35] ]
ImageConvolve[img, BoxMatrix[1] ]</lang>
ImageConvolve[img, BoxMatrix[1] ]</syntaxhighlight>


=={{header|MATLAB}}==
=={{header|MATLAB}}==
The built-in function [http://www.mathworks.com/help/matlab/ref/conv2.html conv2] handles the basic convolution. Below is a program that has several more options that may be useful in different image processing applications (see comments under convImage for specifics).
The built-in function [http://www.mathworks.com/help/matlab/ref/conv2.html conv2] handles the basic convolution. Below is a program that has several more options that may be useful in different image processing applications (see comments under convImage for specifics).
<lang MATLAB>function testConvImage
<syntaxhighlight lang="matlab">function testConvImage
Im = [1 2 1 5 5 ; ...
Im = [1 2 1 5 5 ; ...
1 2 7 9 9 ; ...
1 2 7 9 9 ; ...
Line 1,494: Line 1,494:
% Convert back to former image data type
% Convert back to former image data type
ImOut = cast(ImOut, classIm);
ImOut = cast(ImOut, classIm);
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>Original image:
<pre>Original image:
Line 1,548: Line 1,548:
As in the D version, we use the modules built for the "bitmap" and "grayscale image" tasks. But we have chosen to read and write PNG files rather than PPM files, using for this purpose the "nimPNG" third party module.
As in the D version, we use the modules built for the "bitmap" and "grayscale image" tasks. But we have chosen to read and write PNG files rather than PPM files, using for this purpose the "nimPNG" third party module.


<lang Nim>import math, lenientops, strutils
<syntaxhighlight lang="nim">import math, lenientops, strutils
import nimPNG, bitmap, grayscale_image
import nimPNG, bitmap, grayscale_image


Line 1,665: Line 1,665:
let output = Output2.format(filter.name)
let output = Output2.format(filter.name)
if savePNG24(output, data, result.w, result.h).isOk:
if savePNG24(output, data, result.w, result.h).isOk:
echo "Saved: ", output</lang>
echo "Saved: ", output</syntaxhighlight>


=={{header|OCaml}}==
=={{header|OCaml}}==


<lang ocaml>let get_rgb img x y =
<syntaxhighlight lang="ocaml">let get_rgb img x y =
let _, r_channel,_,_ = img in
let _, r_channel,_,_ = img in
let width = Bigarray.Array2.dim1 r_channel
let width = Bigarray.Array2.dim1 r_channel
Line 1,723: Line 1,723:
done;
done;
done;
done;
(res)</lang>
(res)</syntaxhighlight>


<lang ocaml>let emboss img =
<syntaxhighlight lang="ocaml">let emboss img =
let kernel = [|
let kernel = [|
[| -2.; -1.; 0. |];
[| -2.; -1.; 0. |];
Line 1,759: Line 1,759:
|] in
|] in
convolve_value ~img ~kernel ~divisor:9.0 ~offset:0.0;
convolve_value ~img ~kernel ~divisor:9.0 ~offset:0.0;
;;</lang>
;;</syntaxhighlight>


=={{header|Octave}}==
=={{header|Octave}}==
Line 1,765: Line 1,765:
'''Use package''' [http://octave.sourceforge.net/image/index.html Image]
'''Use package''' [http://octave.sourceforge.net/image/index.html Image]


<lang octave>function [r, g, b] = rgbconv2(a, c)
<syntaxhighlight lang="octave">function [r, g, b] = rgbconv2(a, c)
r = im2uint8(mat2gray(conv2(a(:,:,1), c)));
r = im2uint8(mat2gray(conv2(a(:,:,1), c)));
g = im2uint8(mat2gray(conv2(a(:,:,2), c)));
g = im2uint8(mat2gray(conv2(a(:,:,2), c)));
Line 1,787: Line 1,787:
jpgwrite("LennaSobel.jpg", r, g, b, 100);
jpgwrite("LennaSobel.jpg", r, g, b, 100);
[r, g, b] = rgbconv2(im, sharpen);
[r, g, b] = rgbconv2(im, sharpen);
jpgwrite("LennaSharpen.jpg", r, g, b, 100);</lang>
jpgwrite("LennaSharpen.jpg", r, g, b, 100);</syntaxhighlight>


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;


Line 1,800: Line 1,800:
my $image = rpic 'pythagoras_tree.png';
my $image = rpic 'pythagoras_tree.png';
my $smoothed = conv2d $image, $kernel, {Boundary => 'Truncate'};
my $smoothed = conv2d $image, $kernel, {Boundary => 'Truncate'};
wpic $smoothed, 'pythagoras_convolution.png';</lang>
wpic $smoothed, 'pythagoras_convolution.png';</syntaxhighlight>
Compare offsite images: [https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/frog.png frog.png] vs.
Compare offsite images: [https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/frog.png frog.png] vs.
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/frog_convolution.png frog_convolution.png]
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/frog_convolution.png frog_convolution.png]
Line 1,806: Line 1,806:
=={{header|Phix}}==
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
{{libheader|Phix/pGUI}}
<!--<lang Phix>(notonline)-->
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #000080;font-style:italic;">--
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Image_convolution.exw
-- demo\rosetta\Image_convolution.exw
Line 1,924: Line 1,924:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(scl 3)
<syntaxhighlight lang="picolisp">(scl 3)


(de ppmConvolution (Ppm Kernel)
(de ppmConvolution (Ppm Kernel)
Line 1,951: Line 1,951:
(* (get X K L C) (get Kernel K L)) ) ) )
(* (get X K L C) (get Kernel K L)) ) ) )
(link (min 255 (max 0 (*/ Val 1.0)))) ) ) ) )
(link (min 255 (max 0 (*/ Val 1.0)))) ) ) ) )
(map pop X) ) ) ) ) ) ) )</lang>
(map pop X) ) ) ) ) ) ) )</syntaxhighlight>
Test using 'ppmRead' from [[Bitmap/Read a PPM file#PicoLisp]] and 'ppmWrite'
Test using 'ppmRead' from [[Bitmap/Read a PPM file#PicoLisp]] and 'ppmWrite'
from [[Bitmap/Write a PPM file#PicoLisp]]:
from [[Bitmap/Write a PPM file#PicoLisp]]:
Line 1,971: Line 1,971:
Image manipulation is normally done using an image processing library. For PIL/Pillow do:
Image manipulation is normally done using an image processing library. For PIL/Pillow do:


<lang python>#!/bin/python
<syntaxhighlight lang="python">#!/bin/python
from PIL import Image, ImageFilter
from PIL import Image, ImageFilter


Line 1,982: Line 1,982:
im2 = im.filter(kernel)
im2 = im.filter(kernel)


im2.show()</lang>
im2.show()</syntaxhighlight>


Alternatively, SciPy can be used but programmers need to be careful about the colors being clipped since they are normally limited to the 0-255 range:
Alternatively, SciPy can be used but programmers need to be careful about the colors being clipped since they are normally limited to the 0-255 range:


<lang python>#!/bin/python
<syntaxhighlight lang="python">#!/bin/python
import numpy as np
import numpy as np
from scipy.ndimage.filters import convolve
from scipy.ndimage.filters import convolve
Line 2,002: Line 2,002:
im3 = np.array(np.clip(im2, 0, 255), dtype=np.uint8) #Apply color clipping
im3 = np.array(np.clip(im2, 0, 255), dtype=np.uint8) #Apply color clipping
imshow(im3)</lang>
imshow(im3)</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==
Line 2,012: Line 2,012:




<lang racket>#lang typed/racket
<syntaxhighlight lang="racket">#lang typed/racket
(require images/flomap racket/flonum)
(require images/flomap racket/flonum)


Line 2,061: Line 2,061:
(save-image
(save-image
(flomap->bitmap (flomap-convolve flmp (flvector -1. -1. -1. -1. 4. -1. -1. -1. -1.)))
(flomap->bitmap (flomap-convolve flmp (flvector -1. -1. -1. -1. 4. -1. -1. -1. -1.)))
"out/convolve-etch-3x3.png"))</lang>
"out/convolve-etch-3x3.png"))</syntaxhighlight>


=={{header|Raku}}==
=={{header|Raku}}==
Line 2,067: Line 2,067:
===Perl 5 PDL library===
===Perl 5 PDL library===


<lang perl6>use PDL:from<Perl5>;
<syntaxhighlight lang="raku" line>use PDL:from<Perl5>;
use PDL::Image2D:from<Perl5>;
use PDL::Image2D:from<Perl5>;


Line 2,074: Line 2,074:
my $image = rpic 'frog.png';
my $image = rpic 'frog.png';
my $smoothed = conv2d $image, $kernel, {Boundary => 'Truncate'};
my $smoothed = conv2d $image, $kernel, {Boundary => 'Truncate'};
wpic $smoothed, 'frog_convolution.png';</lang>
wpic $smoothed, 'frog_convolution.png';</syntaxhighlight>
Compare offsite images: [https://github.com/SqrtNegInf/Rosettacode-Perl6-Smoke/blob/master/ref/frog.png frog.png] vs.
Compare offsite images: [https://github.com/SqrtNegInf/Rosettacode-Perl6-Smoke/blob/master/ref/frog.png frog.png] vs.
[https://github.com/SqrtNegInf/Rosettacode-Perl6-Smoke/blob/master/ref/frog_convolution.png frog_convolution.png]
[https://github.com/SqrtNegInf/Rosettacode-Perl6-Smoke/blob/master/ref/frog_convolution.png frog_convolution.png]


===Imagemagick library===
===Imagemagick library===
<syntaxhighlight lang="raku" line>
<lang perl6>
# Note: must install version from github NOT version from CPAN which needs to be updated.
# Note: must install version from github NOT version from CPAN which needs to be updated.
# Reference:
# Reference:
Line 2,109: Line 2,109:
$original.cleanup if $original.defined;
$original.cleanup if $original.defined;
$o.cleanup if $o.defined;
$o.cleanup if $o.defined;
}</lang>
}</syntaxhighlight>


=={{header|Ruby}}==
=={{header|Ruby}}==
{{trans|Tcl}}
{{trans|Tcl}}
<lang ruby>class Pixmap
<syntaxhighlight lang="ruby">class Pixmap
# Apply a convolution kernel to a whole image
# Apply a convolution kernel to a whole image
def convolute(kernel)
def convolute(kernel)
Line 2,169: Line 2,169:
savefile = 'teapot_' + label.downcase + '.ppm'
savefile = 'teapot_' + label.downcase + '.ppm'
teapot.convolute(kernel).save(savefile)
teapot.convolute(kernel).save(savefile)
end</lang>
end</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
{{works with|Tcl|8.6}}
{{libheader|Tk}}
{{libheader|Tk}}
<lang tcl>package require Tk
<syntaxhighlight lang="tcl">package require Tk


# Function for clamping values to those that we can use with colors
# Function for clamping values to those that we can use with colors
Line 2,250: Line 2,250:
pack [labelframe .$name -text $label] -side left
pack [labelframe .$name -text $label] -side left
pack [label .$name.l -image [convolve teapot $kernel]]
pack [label .$name.l -image [convolve teapot $kernel]]
}</lang>
}</syntaxhighlight>


=={{header|Wren}}==
=={{header|Wren}}==
{{libheader|DOME}}
{{libheader|DOME}}
Based on the Java/Kotlin solutions, though input details are hard-coded rather than read in as command line arguments and the input and output images are displayed side by side with the latter also being saved to a file.
Based on the Java/Kotlin solutions, though input details are hard-coded rather than read in as command line arguments and the input and output images are displayed side by side with the latter also being saved to a file.
<lang ecmascript>import "graphics" for Canvas, Color, ImageData
<syntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color, ImageData
import "dome" for Window
import "dome" for Window


Line 2,401: Line 2,401:
for (x in 0...k.count) kernel[x, y] = k[x][y]
for (x in 0...k.count) kernel[x, y] = k[x][y]
}
}
var Game = ImageConvolution.new(700, 300, image1, image2, kernel, divisor)</lang>
var Game = ImageConvolution.new(700, 300, image1, image2, kernel, divisor)</syntaxhighlight>


{{out}}
{{out}}