Bitmap/Histogram: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 15: | Line 15: | ||
In the following solution the input file [https://gitlab.com/amarok8bit/action-rosetta-code/-/blob/master/source/lena30g.PPM lena30g.PPM] is loaded from H6 drive. Altirra emulator automatically converts CR/LF character from ASCII into 155 character in ATASCII charset used by Atari 8-bit computer when one from H6-H10 hard drive under DOS 2.5 is used. |
In the following solution the input file [https://gitlab.com/amarok8bit/action-rosetta-code/-/blob/master/source/lena30g.PPM lena30g.PPM] is loaded from H6 drive. Altirra emulator automatically converts CR/LF character from ASCII into 155 character in ATASCII charset used by Atari 8-bit computer when one from H6-H10 hard drive under DOS 2.5 is used. |
||
{{libheader|Action! Bitmap tools}} |
{{libheader|Action! Bitmap tools}} |
||
< |
<syntaxhighlight lang=Action!>INCLUDE "H6:LOADPPM5.ACT" |
||
DEFINE HISTSIZE="256" |
DEFINE HISTSIZE="256" |
||
Line 123: | Line 123: | ||
DO UNTIL CH#$FF OD |
DO UNTIL CH#$FF OD |
||
CH=$FF |
CH=$FF |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bitmap_Histogram.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bitmap_Histogram.png Screenshot from Atari 8-bit computer] |
||
Line 129: | Line 129: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
Histogram of an image: |
Histogram of an image: |
||
< |
<syntaxhighlight lang=ada>type Pixel_Count is mod 2**64; |
||
type Histogram is array (Luminance) of Pixel_Count; |
type Histogram is array (Luminance) of Pixel_Count; |
||
Line 145: | Line 145: | ||
end loop; |
end loop; |
||
return Result; |
return Result; |
||
end Get_Histogram;</ |
end Get_Histogram;</syntaxhighlight> |
||
Median of a histogram: |
Median of a histogram: |
||
< |
<syntaxhighlight lang=ada>function Median (H : Histogram) return Luminance is |
||
From : Luminance := Luminance'First; |
From : Luminance := Luminance'First; |
||
To : Luminance := Luminance'Last; |
To : Luminance := Luminance'Last; |
||
Line 163: | Line 163: | ||
end loop; |
end loop; |
||
return From; |
return From; |
||
end Median;</ |
end Median;</syntaxhighlight> |
||
Conversion of an image to black and white art: |
Conversion of an image to black and white art: |
||
< |
<syntaxhighlight lang=ada> F1, F2 : File_Type; |
||
begin |
begin |
||
Open (F1, In_File, "city.ppm"); |
Open (F1, In_File, "city.ppm"); |
||
Line 186: | Line 186: | ||
Put_PPM (F2, X); |
Put_PPM (F2, X); |
||
end; |
end; |
||
Close (F2);</ |
Close (F2);</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
Line 192: | Line 192: | ||
[[Image:greyscale_bbc.jpg|right]] |
[[Image:greyscale_bbc.jpg|right]] |
||
[[Image:histogram_bbc.gif|right]] |
[[Image:histogram_bbc.gif|right]] |
||
< |
<syntaxhighlight lang=bbcbasic> INSTALL @lib$+"SORTLIB" |
||
Sort% = FN_sortinit(0,0) |
Sort% = FN_sortinit(0,0) |
||
Line 250: | Line 250: | ||
col% = TINT(x%*2,y%*2) |
col% = TINT(x%*2,y%*2) |
||
SWAP ?^col%,?(^col%+2) |
SWAP ?^col%,?(^col%+2) |
||
= col%</ |
= col%</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang=c>typedef unsigned int histogram_t; |
||
typedef histogram_t *histogram; |
typedef histogram_t *histogram; |
||
Line 260: | Line 260: | ||
histogram get_histogram(grayimage im); |
histogram get_histogram(grayimage im); |
||
luminance histogram_median(histogram h);</ |
luminance histogram_median(histogram h);</syntaxhighlight> |
||
< |
<syntaxhighlight lang=c>histogram get_histogram(grayimage im) |
||
{ |
{ |
||
histogram t; |
histogram t; |
||
Line 281: | Line 281: | ||
} |
} |
||
return t; |
return t; |
||
}</ |
}</syntaxhighlight> |
||
The given <tt>histogram</tt> must be freed with a simple <tt>free(histogram)</tt>. |
The given <tt>histogram</tt> must be freed with a simple <tt>free(histogram)</tt>. |
||
Line 287: | Line 287: | ||
{{trans|Ada}} |
{{trans|Ada}} |
||
< |
<syntaxhighlight lang=c>luminance histogram_median(histogram h) |
||
{ |
{ |
||
luminance From, To; |
luminance From, To; |
||
Line 305: | Line 305: | ||
} |
} |
||
return From; |
return From; |
||
}</ |
}</syntaxhighlight> |
||
An example of usage is the following code. |
An example of usage is the following code. |
||
< |
<syntaxhighlight lang=c>#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include "imglib.h" |
#include "imglib.h" |
||
Line 358: | Line 358: | ||
free_img((image)g_img); |
free_img((image)g_img); |
||
free_img(color_img); |
free_img(color_img); |
||
}</ |
}</syntaxhighlight> |
||
Which reads from the file specified from the command line and outputs to the standard out the PPM B/W version of the input image. The input image can be of any format handled by ImageMagick (see [[Read image file through a pipe]]) |
Which reads from the file specified from the command line and outputs to the standard out the PPM B/W version of the input image. The input image can be of any format handled by ImageMagick (see [[Read image file through a pipe]]) |
||
Line 364: | Line 364: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
{{libheader|opticl}} |
{{libheader|opticl}} |
||
< |
<syntaxhighlight lang=lisp>(defpackage #:histogram |
||
(:use #:cl |
(:use #:cl |
||
#:opticl)) |
#:opticl)) |
||
Line 409: | Line 409: | ||
(let* ((image (read-jpeg-file "lenna.jpg")) |
(let* ((image (read-jpeg-file "lenna.jpg")) |
||
(bw-image (gray->black&white-image (color->gray-image image)))) |
(bw-image (gray->black&white-image (color->gray-image image)))) |
||
(write-pbm-file "lenna-bw.pbm" bw-image)))</ |
(write-pbm-file "lenna-bw.pbm" bw-image)))</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Ada}} |
{{trans|Ada}} |
||
It uses the grayscale_image from the Grayscale image Task. The loaded frog image is from the Color quantization Task. |
It uses the grayscale_image from the Grayscale image Task. The loaded frog image is from the Color quantization Task. |
||
< |
<syntaxhighlight lang=d>import grayscale_image; |
||
Color findSingleChannelMedian(Color)(in Image!Color img) |
Color findSingleChannelMedian(Color)(in Image!Color img) |
||
Line 460: | Line 460: | ||
img.binarizeInPlace(img.findSingleChannelMedian()) |
img.binarizeInPlace(img.findSingleChannelMedian()) |
||
.savePGM("quantum_frog_bin.pgm"); |
.savePGM("quantum_frog_bin.pgm"); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|FBSL}}== |
=={{header|FBSL}}== |
||
Line 467: | Line 467: | ||
'''24-bpp P.O.T.-size BMP solution:''' |
'''24-bpp P.O.T.-size BMP solution:''' |
||
[[File:FBSLHistogram.PNG|right]] |
[[File:FBSLHistogram.PNG|right]] |
||
< |
<syntaxhighlight lang=qbasic>#DEFINE WM_CLOSE 16 |
||
DIM colored = ".\LenaClr.bmp", grayscale = ".\LenaGry.bmp", blackwhite = ".\LenaBnw.bmp" |
DIM colored = ".\LenaClr.bmp", grayscale = ".\LenaGry.bmp", blackwhite = ".\LenaBnw.bmp" |
||
Line 516: | Line 516: | ||
NEXT |
NEXT |
||
FILEPUT(FILEOPEN(blackwhite, BINARY_NEW), FILEGET): FILECLOSE(FILEOPEN) ' save b/w image |
FILEPUT(FILEOPEN(blackwhite, BINARY_NEW), FILEGET): FILECLOSE(FILEOPEN) ' save b/w image |
||
END SUB</ |
END SUB</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang=forth>: histogram ( array gmp -- ) |
||
over 256 cells erase |
over 256 cells erase |
||
dup bdim * over bdata + swap bdata |
dup bdim * over bdata + swap bdata |
||
do 1 over i c@ cells + +! loop drop ;</ |
do 1 over i c@ cells + +! loop drop ;</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Line 529: | Line 529: | ||
'''Note''': ''luminance'' range is hard-encoded and is from 0 to 255. This could be enhanced. |
'''Note''': ''luminance'' range is hard-encoded and is from 0 to 255. This could be enhanced. |
||
< |
<syntaxhighlight lang=fortran>module RCImageProcess |
||
use RCImageBasic |
use RCImageBasic |
||
implicit none |
implicit none |
||
Line 568: | Line 568: | ||
end function histogram_median |
end function histogram_median |
||
end module RCImageProcess</ |
end module RCImageProcess</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang=fortran>program BasicImageTests |
||
use RCImageBasic |
use RCImageBasic |
||
use RCImageIO |
use RCImageIO |
||
Line 613: | Line 613: | ||
call free_img(gray) |
call free_img(gray) |
||
end program BasicImageTests</ |
end program BasicImageTests</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
Histogram and Threshold functions are be added to the Grmap type for this task: |
Histogram and Threshold functions are be added to the Grmap type for this task: |
||
< |
<syntaxhighlight lang=go>package raster |
||
import "math" |
import "math" |
||
Line 640: | Line 640: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Demonstration program computes the median: |
Demonstration program computes the median: |
||
< |
<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 686: | Line 686: | ||
fmt.Println(err) |
fmt.Println(err) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
First, an implementation of a black-and-white instance of <tt>Color</tt>. For simplicty, we use ASCII PBM for output instead of the raw format. |
First, an implementation of a black-and-white instance of <tt>Color</tt>. For simplicty, we use ASCII PBM for output instead of the raw format. |
||
< |
<syntaxhighlight lang=haskell>module Bitmap.BW(module Bitmap.BW) where |
||
import Bitmap |
import Bitmap |
||
Line 721: | Line 721: | ||
toBWImage' darkestWhite = mapImage $ f . luminance |
toBWImage' darkestWhite = mapImage $ f . luminance |
||
where f x | x < darkestWhite = black |
where f x | x < darkestWhite = black |
||
| otherwise = white</ |
| otherwise = white</syntaxhighlight> |
||
Every instance of <tt>Color</tt> has a <tt>luminance</tt> method, so we don't need to convert an image to <tt>Gray</tt> to calculate its histogram. |
Every instance of <tt>Color</tt> has a <tt>luminance</tt> method, so we don't need to convert an image to <tt>Gray</tt> to calculate its histogram. |
||
< |
<syntaxhighlight lang=haskell>import Bitmap |
||
import Bitmap.RGB |
import Bitmap.RGB |
||
import Bitmap.BW |
import Bitmap.BW |
||
Line 750: | Line 750: | ||
if left < right |
if left < right |
||
then (n + 1, left + l, right, ls, rL) |
then (n + 1, left + l, right, ls, rL) |
||
else (n, left, right + r, lL, rs)</ |
else (n, left, right + r, lL, rs)</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
Line 756: | Line 756: | ||
Using <code>toGray</code> from [[Grayscale image#J|Grayscale image]]. |
Using <code>toGray</code> from [[Grayscale image#J|Grayscale image]]. |
||
< |
<syntaxhighlight lang=j>getImgHist=: ([: /:~ ~. ,. #/.~)@, |
||
medianHist=: {."1 {~ [: (+/\ I. -:@(+/)) {:"1 |
medianHist=: {."1 {~ [: (+/\ I. -:@(+/)) {:"1 |
||
toBW=: 255 * medianHist@getImgHist < toGray</ |
toBW=: 255 * medianHist@getImgHist < toGray</syntaxhighlight> |
||
'''Example Usage:''' |
'''Example Usage:''' |
||
Line 764: | Line 764: | ||
Use [http://rosettacode.org/mw/images/b/b6/Lenna100.jpg Lenna100.jpg] for testing (read using the [[j:Addons/media/platimg|media/platimg]] addon and convert to ppm file). |
Use [http://rosettacode.org/mw/images/b/b6/Lenna100.jpg Lenna100.jpg] for testing (read using the [[j:Addons/media/platimg|media/platimg]] addon and convert to ppm file). |
||
< |
<syntaxhighlight lang=j> require 'media/platimg' |
||
'Lenna100.ppm' writeppm~ 256#.inv readimg 'Lenna100.jpg' |
'Lenna100.ppm' writeppm~ 256#.inv readimg 'Lenna100.jpg' |
||
786447</ |
786447</syntaxhighlight> |
||
Read ppm file, convert to black and white and write to a new ppm file using <code>writeppm</code>, <code>readppm</code> and <code>toColor</code> from the [[read ppm file#J | read]]/[[write ppm file#J|write ppm file]], and [[grayscale image#J|grayscale image]] solutions. |
Read ppm file, convert to black and white and write to a new ppm file using <code>writeppm</code>, <code>readppm</code> and <code>toColor</code> from the [[read ppm file#J | read]]/[[write ppm file#J|write ppm file]], and [[grayscale image#J|grayscale image]] solutions. |
||
< |
<syntaxhighlight lang=j> 'Lenna100BW.ppm' writeppm~ toColor toBW readppm 'Lenna100.ppm' |
||
786447</ |
786447</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang=Java>import java.awt.image.BufferedImage; |
||
import java.io.File; |
import java.io.File; |
||
import java.io.IOException; |
import java.io.IOException; |
||
Line 835: | Line 835: | ||
return median; |
return median; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{works with|Julia|0.6}} |
{{works with|Julia|0.6}} |
||
< |
<syntaxhighlight lang=julia>using Images, FileIO |
||
ima = load("data/lenna50.jpg") |
ima = load("data/lenna50.jpg") |
||
Line 848: | Line 848: | ||
imb[imb .≤ medcol] = Gray(0.0) |
imb[imb .≤ medcol] = Gray(0.0) |
||
imb[imb .> medcol] = Gray(1.0) |
imb[imb .> medcol] = Gray(1.0) |
||
save("data/lennaGray.jpg", imb)</ |
save("data/lennaGray.jpg", imb)</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Uses the image from the [[Percentage difference between images]] task as an example. |
Uses the image from the [[Percentage difference between images]] task as an example. |
||
< |
<syntaxhighlight lang=scala>// version 1.2.10 |
||
import java.io.File |
import java.io.File |
||
Line 912: | Line 912: | ||
val bwFile = File("Lenna_bw.jpg") |
val bwFile = File("Lenna_bw.jpg") |
||
ImageIO.write(image, "jpg", bwFile) |
ImageIO.write(image, "jpg", bwFile) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Line 920: | Line 920: | ||
[[Basic bitmap storage#Lua]], |
[[Basic bitmap storage#Lua]], |
||
[[Grayscale image#Lua]]. |
[[Grayscale image#Lua]]. |
||
< |
<syntaxhighlight lang=lua>function Histogram( image ) |
||
local size_x, size_y = #image, #image[1] |
local size_x, size_y = #image, #image[1] |
||
Line 971: | Line 971: | ||
bitmap = ConvertToColorImage( gray_im ) |
bitmap = ConvertToColorImage( gray_im ) |
||
Write_PPM( "outputimage.ppm", bitmap )</ |
Write_PPM( "outputimage.ppm", bitmap )</syntaxhighlight> |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
<lang |
<syntaxhighlight lang=Mathematica>ImageLevels[img]</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang=Nim>import bitmap |
||
import grayscale_image |
import grayscale_image |
||
Line 1,028: | Line 1,028: | ||
# Save image as a PPM file. |
# Save image as a PPM file. |
||
image.writePPM("house_bw.ppm")</ |
image.writePPM("house_bw.ppm")</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
{{Trans|C}} |
{{Trans|C}} |
||
< |
<syntaxhighlight lang=ocaml>type histogram = int array |
||
let get_histogram ~img:gray_channel = |
let get_histogram ~img:gray_channel = |
||
Line 1,046: | Line 1,046: | ||
done; |
done; |
||
(t: histogram) |
(t: histogram) |
||
;;</ |
;;</syntaxhighlight> |
||
< |
<syntaxhighlight lang=ocaml>let histogram_median (h : histogram) = |
||
let from = 0 and to_ = 255 in |
let from = 0 and to_ = 255 in |
||
Line 1,062: | Line 1,062: | ||
in |
in |
||
aux from to_ left right |
aux from to_ left right |
||
;;</ |
;;</syntaxhighlight> |
||
main: |
main: |
||
< |
<syntaxhighlight lang=ocaml>let () = |
||
let img = read_ppm ~filename:"/tmp/foo.ppm" in |
let img = read_ppm ~filename:"/tmp/foo.ppm" in |
||
Line 1,088: | Line 1,088: | ||
output_ppm ~oc:stdout ~img:res; |
output_ppm ~oc:stdout ~img:res; |
||
;;</ |
;;</syntaxhighlight> |
||
=={{header|Octave}}== |
=={{header|Octave}}== |
||
'''Using package''' [http://octave.sourceforge.net/image/index.html Image] |
'''Using package''' [http://octave.sourceforge.net/image/index.html Image] |
||
< |
<syntaxhighlight lang=octave>function h = imagehistogram(imago) |
||
if ( isgray(imago) ) |
if ( isgray(imago) ) |
||
for j = 0:255 |
for j = 0:255 |
||
Line 1,143: | Line 1,143: | ||
ibw( img > m ) = 255; |
ibw( img > m ) = 255; |
||
ibw( img <= m ) = 0; |
ibw( img <= m ) = 0; |
||
jpgwrite("lennamed.jpg", ibw, 100);</ |
jpgwrite("lennamed.jpg", ibw, 100);</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Requires read_ppm() from [[Bitmap/Read_a_PPM_file#Phix|Read_a_PPM_file]], write_ppm() from [[Bitmap/Write_a_PPM_file#Phix|Write_a_PPM_file]]. <br> |
Requires read_ppm() from [[Bitmap/Read_a_PPM_file#Phix|Read_a_PPM_file]], write_ppm() from [[Bitmap/Write_a_PPM_file#Phix|Write_a_PPM_file]]. <br> |
||
Uses demo\rosetta\lena.ppm, included in the distribution, results may be verified with demo\rosetta\viewppm.exw |
Uses demo\rosetta\lena.ppm, included in the distribution, results may be verified with demo\rosetta\viewppm.exw |
||
< |
<syntaxhighlight lang=Phix>-- demo\rosetta\Bitmap_Histogram.exw (runnable version) |
||
include ppm.e -- black, white, read_ppm(), write_ppm() (covers above requirements) |
include ppm.e -- black, white, read_ppm(), write_ppm() (covers above requirements) |
||
Line 1,186: | Line 1,186: | ||
sequence img = read_ppm("Lena.ppm") |
sequence img = read_ppm("Lena.ppm") |
||
img = to_bw(img) |
img = to_bw(img) |
||
write_ppm("LenaBW.ppm",img)</ |
write_ppm("LenaBW.ppm",img)</syntaxhighlight> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang=PHP> |
||
define('src_name', 'input.jpg'); // source image |
define('src_name', 'input.jpg'); // source image |
||
define('dest_name', 'output.jpg'); // destination image |
define('dest_name', 'output.jpg'); // destination image |
||
Line 1,245: | Line 1,245: | ||
echo 'Image not saved! Check permission!'; |
echo 'Image not saved! Check permission!'; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Example: <br> |
Example: <br> |
||
<div> |
<div> |
||
Line 1,262: | Line 1,262: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
{{trans|Forth}} |
{{trans|Forth}} |
||
< |
<syntaxhighlight lang=PicoLisp>(de histogram (Pgm) |
||
(let H (need 256 0) |
(let H (need 256 0) |
||
(for L Pgm |
(for L Pgm |
||
(for G L |
(for G L |
||
(inc (nth H (inc G))) ) ) |
(inc (nth H (inc G))) ) ) |
||
H ) )</ |
H ) )</syntaxhighlight> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
Also requires PureBasic solutions for [[Bitmap/Read_a_PPM_file#PureBasic|Read a PPM file]], [[Grayscale_image#PureBasic|Grayscale image]], and [[Bitmap/Write_a_PPM_file#PureBasic|Write a PPM file]]. |
Also requires PureBasic solutions for [[Bitmap/Read_a_PPM_file#PureBasic|Read a PPM file]], [[Grayscale_image#PureBasic|Grayscale image]], and [[Bitmap/Write_a_PPM_file#PureBasic|Write a PPM file]]. |
||
< |
<syntaxhighlight lang=PureBasic>Procedure getHistogram(image, Array histogram(1)) |
||
Protected w = ImageWidth(image) - 1 |
Protected w = ImageWidth(image) - 1 |
||
Protected h = ImageHeight(image) - 1 |
Protected h = ImageHeight(image) - 1 |
||
Line 1,333: | Line 1,333: | ||
outputFile = Left(sourceFile, Len(sourceFile) - Len(GetExtensionPart(sourceFile))) + "_bw." + GetExtensionPart(sourceFile) |
outputFile = Left(sourceFile, Len(sourceFile) - Len(GetExtensionPart(sourceFile))) + "_bw." + GetExtensionPart(sourceFile) |
||
SaveImageAsPPM(image, outputFile, 1) |
SaveImageAsPPM(image, outputFile, 1) |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Makes use of the Pillow library (PIL) you can install it using pip. The code is probably not the fastest or the image I used (1960x1960) is just too big. |
Makes use of the Pillow library (PIL) you can install it using pip. The code is probably not the fastest or the image I used (1960x1960) is just too big. |
||
< |
<syntaxhighlight lang=python>from PIL import Image |
||
# Open the image |
# Open the image |
||
Line 1,378: | Line 1,378: | ||
bw_image.show() |
bw_image.show() |
||
bm_image.show()</ |
bm_image.show()</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang=racket> #lang racket |
||
(require racket/draw math/statistics racket/require |
(require racket/draw math/statistics racket/require |
||
(filtered-in |
(filtered-in |
||
Line 1,442: | Line 1,442: | ||
(send (bitmap->monochrome 1/4 bm) save-file "histogram-racket-0.25.png" 'png) |
(send (bitmap->monochrome 1/4 bm) save-file "histogram-racket-0.25.png" 'png) |
||
(send (bitmap->monochrome 1/2 bm) save-file "histogram-racket-0.50.png" 'png) ; median |
(send (bitmap->monochrome 1/2 bm) save-file "histogram-racket-0.50.png" 'png) ; median |
||
(send (bitmap->monochrome 3/4 bm xs ws) save-file "histogram-racket-0.75.png" 'png)))</ |
(send (bitmap->monochrome 3/4 bm xs ws) save-file "histogram-racket-0.75.png" 'png)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,457: | Line 1,457: | ||
Uses pieces from [[Bitmap#Raku| Bitmap]], [[Bitmap/Write_a_PPM_file#Raku| Write a PPM file]] and [[Grayscale_image#Raku| Grayscale image]] tasks. Included here to make a complete, runnable program. |
Uses pieces from [[Bitmap#Raku| Bitmap]], [[Bitmap/Write_a_PPM_file#Raku| Write a PPM file]] and [[Grayscale_image#Raku| Grayscale image]] tasks. Included here to make a complete, runnable program. |
||
<lang |
<syntaxhighlight lang=raku line>class Pixel { has UInt ($.R, $.G, $.B) } |
||
class Bitmap { |
class Bitmap { |
||
has UInt ($.width, $.height); |
has UInt ($.width, $.height); |
||
Line 1,508: | Line 1,508: | ||
histogram($b); |
histogram($b); |
||
'./Lenna-bw.pbm'.IO.open(:bin, :w).write: $b.P4;</ |
'./Lenna-bw.pbm'.IO.open(:bin, :w).write: $b.P4;</syntaxhighlight> |
||
See [https://github.com/thundergnat/rc/blob/master/img/Lenna.png Lenna], and [https://github.com/thundergnat/rc/blob/master/img/Lenna-bw.png Lenna-bw] images. (converted to .png as .ppm format is not widely supported). |
See [https://github.com/thundergnat/rc/blob/master/img/Lenna.png Lenna], and [https://github.com/thundergnat/rc/blob/master/img/Lenna-bw.png Lenna-bw] images. (converted to .png as .ppm format is not widely supported). |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang=ruby>class Pixmap |
||
def histogram |
def histogram |
||
histogram = Hash.new(0) |
histogram = Hash.new(0) |
||
Line 1,553: | Line 1,553: | ||
end |
end |
||
Pixmap.open('file.ppm').save_as_blackandwhite('file_bw.ppm')</ |
Pixmap.open('file.ppm').save_as_blackandwhite('file_bw.ppm')</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang=rust>extern crate image; |
||
use image::{DynamicImage, GenericImageView, ImageBuffer, Rgba}; |
use image::{DynamicImage, GenericImageView, ImageBuffer, Rgba}; |
||
Line 1,624: | Line 1,624: | ||
img.save("lena-mono.png").expect("could not save result image"); |
img.save("lena-mono.png").expect("could not save result image"); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
Line 1,632: | Line 1,632: | ||
* [[Read_ppm_file#Scala|Read a PPM File]] image loading |
* [[Read_ppm_file#Scala|Read a PPM File]] image loading |
||
< |
<syntaxhighlight lang=scala>object BitmapOps { |
||
def histogram(bm:RgbBitmap)={ |
def histogram(bm:RgbBitmap)={ |
||
val hist=new Array[Int](255) |
val hist=new Array[Int](255) |
||
Line 1,663: | Line 1,663: | ||
image |
image |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Usage: |
Usage: |
||
< |
<syntaxhighlight lang=scala>val img=Pixmap.load("image.ppm").get |
||
val hist=BitmapOps.histogram(img) |
val hist=BitmapOps.histogram(img) |
||
val mid=BitmapOps.histogram_median(hist); |
val mid=BitmapOps.histogram_median(hist); |
||
Line 1,676: | Line 1,676: | ||
icon=new ImageIcon(BitmapOps.monochrom(img, mid).image) |
icon=new ImageIcon(BitmapOps.monochrom(img, mid).image) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{libheader|Tk}} |
{{libheader|Tk}} |
||
Uses [[read ppm file#Tcl|readPPM]], [[grayscale image#Tcl|grayscale]] and [[write ppm file#Tcl|output_ppm]] from other pages. |
Uses [[read ppm file#Tcl|readPPM]], [[grayscale image#Tcl|grayscale]] and [[write ppm file#Tcl|output_ppm]] from other pages. |
||
< |
<syntaxhighlight lang=tcl>package require Tcl 8.5 |
||
package require Tk |
package require Tk |
||
Line 1,730: | Line 1,730: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
Line 1,736: | Line 1,736: | ||
On return, #30 points to buffer containing histogram data. |
On return, #30 points to buffer containing histogram data. |
||
The histogram data is given as ASCII decimal values, one value per line. |
The histogram data is given as ASCII decimal values, one value per line. |
||
< |
<syntaxhighlight lang=vedit>:HISTOGRAM: |
||
#30 = Buf_Free // #30 = buffer to store histogram data |
#30 = Buf_Free // #30 = buffer to store histogram data |
||
for (#9=0; #9<256; #9++) { |
for (#9=0; #9<256; #9++) { |
||
Line 1,745: | Line 1,745: | ||
Num_Ins(#8, FILL) // store count |
Num_Ins(#8, FILL) // store count |
||
} |
} |
||
Return</ |
Return</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|DOME}} |
{{libheader|DOME}} |
||
< |
<syntaxhighlight lang=ecmascript>import "dome" for Window |
||
import "graphics" for Canvas, Color, ImageData |
import "graphics" for Canvas, Color, ImageData |
||
Line 1,833: | Line 1,833: | ||
} |
} |
||
var Game = ImageHistogram.new("Lenna100.jpg", "Lenna100_B&W.png")</ |
var Game = ImageHistogram.new("Lenna100.jpg", "Lenna100_B&W.png")</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|C}} |
{{trans|C}} |
||
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl |
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl |
||
< |
<syntaxhighlight lang=zkl>fcn histogram(image){ |
||
hist:=List.createLong(256,0); // array[256] of zero |
hist:=List.createLong(256,0); // array[256] of zero |
||
image.data.howza(0).pump(Void,'wrap(c){ hist[c]+=1 }); // byte by byte loop |
image.data.howza(0).pump(Void,'wrap(c){ hist[c]+=1 }); // byte by byte loop |
||
Line 1,851: | Line 1,851: | ||
} |
} |
||
from |
from |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang=zkl>img:=PPM.readPPMFile("lenaGrey.ppm"); // a grey scale image |
||
median:=histogramMedian(histogram(img)); |
median:=histogramMedian(histogram(img)); |
||
median.println(); |
median.println(); |
||
Line 1,861: | Line 1,861: | ||
img.data.pump(bw.data.clear(),'wrap(c){ if(c>median) 0xff else 0 }); |
img.data.pump(bw.data.clear(),'wrap(c){ if(c>median) 0xff else 0 }); |
||
bw.write(File("foo.ppm","wb"));</ |
bw.write(File("foo.ppm","wb"));</syntaxhighlight> |
||
{{out}}<pre>101</pre> |
{{out}}<pre>101</pre> |
||
See the BBC Basic entry or: |
See the BBC Basic entry or: |