Bitmap/Histogram: Difference between revisions

m
→‎{{header|Wren}}: ImageData.loadFromFile now deprecated, changed to ImageData.load
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: ImageData.loadFromFile now deprecated, changed to ImageData.load)
 
(4 intermediate revisions by 3 users not shown)
Line 11:
* Replace each pixel of luminance lesser than the median to black, and others to white.
Use [[read ppm file | read]]/[[write ppm file]], and [[grayscale image]] solutions.
 
=={{header|Action!}}==
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}}
<syntaxhighlight lang=Action"action!">INCLUDE "H6:LOADPPM5.ACT"
 
DEFINE HISTSIZE="256"
Line 126 ⟶ 125:
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bitmap_Histogram.png Screenshot from Atari 8-bit computer]
 
=={{header|Ada}}==
Histogram of an image:
<syntaxhighlight lang="ada">type Pixel_Count is mod 2**64;
type Histogram is array (Luminance) of Pixel_Count;
Line 147 ⟶ 145:
end Get_Histogram;</syntaxhighlight>
Median of a histogram:
<syntaxhighlight lang="ada">function Median (H : Histogram) return Luminance is
From : Luminance := Luminance'First;
To : Luminance := Luminance'Last;
Line 165 ⟶ 163:
end Median;</syntaxhighlight>
Conversion of an image to black and white art:
<syntaxhighlight lang="ada"> F1, F2 : File_Type;
begin
Open (F1, In_File, "city.ppm");
Line 187 ⟶ 185:
end;
Close (F2);</syntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
[[Image:greyscale_bbc.jpg|right]]
[[Image:histogram_bbc.gif|right]]
<syntaxhighlight lang="bbcbasic"> INSTALL @lib$+"SORTLIB"
Sort% = FN_sortinit(0,0)
Line 251 ⟶ 248:
SWAP ?^col%,?(^col%+2)
= col%</syntaxhighlight>
 
=={{header|C}}==
 
<syntaxhighlight lang="c">typedef unsigned int histogram_t;
typedef histogram_t *histogram;
 
Line 262 ⟶ 258:
luminance histogram_median(histogram h);</syntaxhighlight>
 
<syntaxhighlight lang="c">histogram get_histogram(grayimage im)
{
histogram t;
Line 287 ⟶ 283:
{{trans|Ada}}
 
<syntaxhighlight lang="c">luminance histogram_median(histogram h)
{
luminance From, To;
Line 309 ⟶ 305:
An example of usage is the following code.
 
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include "imglib.h"
Line 361 ⟶ 357:
 
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]])
 
=={{header|Common Lisp}}==
{{libheader|opticl}}
<syntaxhighlight lang="lisp">(defpackage #:histogram
(:use #:cl
#:opticl))
Line 410 ⟶ 405:
(bw-image (gray->black&white-image (color->gray-image image))))
(write-pbm-file "lenna-bw.pbm" bw-image)))</syntaxhighlight>
 
=={{header|D}}==
{{trans|Ada}}
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)
Line 461 ⟶ 455:
.savePGM("quantum_frog_bin.pgm");
}</syntaxhighlight>
 
=={{header|FBSL}}==
FBSL volatiles and function call concatenation used heavily for brevity.
Line 467 ⟶ 460:
'''24-bpp P.O.T.-size BMP solution:'''
[[File:FBSLHistogram.PNG|right]]
<syntaxhighlight lang="qbasic">#DEFINE WM_CLOSE 16
 
DIM colored = ".\LenaClr.bmp", grayscale = ".\LenaGry.bmp", blackwhite = ".\LenaBnw.bmp"
Line 517 ⟶ 510:
FILEPUT(FILEOPEN(blackwhite, BINARY_NEW), FILEGET): FILECLOSE(FILEOPEN) ' save b/w image
END SUB</syntaxhighlight>
 
=={{header|Forth}}==
<syntaxhighlight lang="forth">: histogram ( array gmp -- )
over 256 cells erase
dup bdim * over bdata + swap bdata
do 1 over i c@ cells + +! loop drop ;</syntaxhighlight>
\ will not work as far as bdim bdata are not ans forth words
\ do not forget to assign them yourself in your code
 
=={{header|GnuForth 0.7}}==
<syntaxhighlight lang="forth">
: bar ( v y x -- )
2dup at-xy .\" [" 100 spaces .\" ]" swap 1 + swap at-xy 0
DO .\" #"
LOOP
cr ;
: demo \ just demo to show it working in percentage 1% to 4%
5 1
DO i 10 i bar
LOOP
cr ;
\ will display :
[# ]
[## ]
[### ]
[#### ]
</syntaxhighlight>
Call it from an array
0 toto swap cells + @ 10 5 bar \ draws bar from first item of toto array
 
=={{header|Fortran}}==
Line 529 ⟶ 544:
'''Note''': ''luminance'' range is hard-encoded and is from 0 to 255. This could be enhanced.
 
<syntaxhighlight lang="fortran">module RCImageProcess
use RCImageBasic
implicit none
Line 572 ⟶ 587:
Example:
 
<syntaxhighlight lang="fortran">program BasicImageTests
use RCImageBasic
use RCImageIO
Line 614 ⟶ 629:
 
end program BasicImageTests</syntaxhighlight>
 
=={{header|Go}}==
Histogram and Threshold functions are be added to the Grmap type for this task:
<syntaxhighlight lang="go">package raster
 
import "math"
Line 642 ⟶ 656:
}</syntaxhighlight>
Demonstration program computes the median:
<syntaxhighlight lang="go">package main
 
// Files required to build supporting package raster are found in:
Line 687 ⟶ 701:
}
}</syntaxhighlight>
 
=={{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.
<syntaxhighlight lang="haskell">module Bitmap.BW(module Bitmap.BW) where
 
import Bitmap
Line 724 ⟶ 737:
 
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.BW
Line 751 ⟶ 764:
then (n + 1, left + l, right, ls, rL)
else (n, left, right + r, lL, rs)</syntaxhighlight>
 
=={{header|J}}==
'''Solution:'''
 
Using <code>toGray</code> from [[Grayscale image#J|Grayscale image]].
<syntaxhighlight lang="j">getImgHist=: ([: /:~ ~. ,. #/.~)@,
medianHist=: {."1 {~ [: (+/\ I. -:@(+/)) {:"1
toBW=: 255 * medianHist@getImgHist < toGray</syntaxhighlight>
Line 764 ⟶ 776:
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'
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.
<syntaxhighlight lang="j"> 'Lenna100BW.ppm' writeppm~ toColor toBW readppm 'Lenna100.ppm'
786447</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang=Java"java">import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
Line 836 ⟶ 847:
}
}</syntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<syntaxhighlight lang="julia">using Images, FileIO
 
ima = load("data/lenna50.jpg")
Line 849 ⟶ 859:
imb[imb .> medcol] = Gray(1.0)
save("data/lennaGray.jpg", imb)</syntaxhighlight>
 
=={{header|Kotlin}}==
Uses the image from the [[Percentage difference between images]] task as an example.
<syntaxhighlight lang="scala">// version 1.2.10
 
import java.io.File
Line 913 ⟶ 922:
ImageIO.write(image, "jpg", bwFile)
}</syntaxhighlight>
 
=={{header|Lua}}==
This solution uses functions defined at:
Line 920 ⟶ 928:
[[Basic bitmap storage#Lua]],
[[Grayscale image#Lua]].
<syntaxhighlight lang="lua">function Histogram( image )
local size_x, size_y = #image, #image[1]
Line 972 ⟶ 980:
bitmap = ConvertToColorImage( gray_im )
Write_PPM( "outputimage.ppm", bitmap )</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">ImageLevels[img]</syntaxhighlight>
 
=={{header|Nim}}==
<syntaxhighlight lang=Nim"nim">import bitmap
import grayscale_image
 
Line 1,029 ⟶ 1,035:
# Save image as a PPM file.
image.writePPM("house_bw.ppm")</syntaxhighlight>
 
=={{header|OCaml}}==
{{Trans|C}}
 
<syntaxhighlight lang="ocaml">type histogram = int array
 
let get_histogram ~img:gray_channel =
Line 1,048 ⟶ 1,053:
;;</syntaxhighlight>
 
<syntaxhighlight lang="ocaml">let histogram_median (h : histogram) =
 
let from = 0 and to_ = 255 in
Line 1,065 ⟶ 1,070:
 
main:
<syntaxhighlight lang="ocaml">let () =
let img = read_ppm ~filename:"/tmp/foo.ppm" in
 
Line 1,089 ⟶ 1,094:
output_ppm ~oc:stdout ~img:res;
;;</syntaxhighlight>
 
=={{header|Octave}}==
'''Using package''' [http://octave.sourceforge.net/image/index.html Image]
<syntaxhighlight lang="octave">function h = imagehistogram(imago)
if ( isgray(imago) )
for j = 0:255
Line 1,144 ⟶ 1,148:
ibw( img <= m ) = 0;
jpgwrite("lennamed.jpg", ibw, 100);</syntaxhighlight>
 
=={{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>
Uses demo\rosetta\lena.ppm, included in the distribution, results may be verified with demo\rosetta\viewppm.exw
<syntaxhighlight lang=Phix"phix">-- demo\rosetta\Bitmap_Histogram.exw (runnable version)
include ppm.e -- black, white, read_ppm(), write_ppm() (covers above requirements)
 
Line 1,187 ⟶ 1,190:
img = to_bw(img)
write_ppm("LenaBW.ppm",img)</syntaxhighlight>
 
=={{header|PHP}}==
 
<syntaxhighlight lang=PHP"php">
define('src_name', 'input.jpg'); // source image
define('dest_name', 'output.jpg'); // destination image
Line 1,259 ⟶ 1,261:
</i>
</div>
 
=={{header|PicoLisp}}==
{{trans|Forth}}
<syntaxhighlight lang=PicoLisp"picolisp">(de histogram (Pgm)
(let H (need 256 0)
(for L Pgm
Line 1,268 ⟶ 1,269:
(inc (nth H (inc G))) ) )
H ) )</syntaxhighlight>
 
=={{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]].
<syntaxhighlight lang=PureBasic"purebasic">Procedure getHistogram(image, Array histogram(1))
Protected w = ImageWidth(image) - 1
Protected h = ImageHeight(image) - 1
Line 1,334:
SaveImageAsPPM(image, outputFile, 1)
EndIf</syntaxhighlight>
 
=={{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.
<syntaxhighlight lang="python">from PIL import Image
 
# Open the image
Line 1,379 ⟶ 1,378:
bw_image.show()
bm_image.show()</syntaxhighlight>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket"> #lang racket
(require racket/draw math/statistics racket/require
(filtered-in
Line 1,451 ⟶ 1,449:
 
Sorry guys... I just give up on linking/displaying these images any other way!
 
=={{header|Raku}}==
(formerly Perl 6)
Line 1,457 ⟶ 1,454:
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.
 
<syntaxhighlight lang="raku" line>class Pixel { has UInt ($.R, $.G, $.B) }
class Bitmap {
has UInt ($.width, $.height);
Line 1,511 ⟶ 1,508:
 
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}}==
<syntaxhighlight lang="ruby">class Pixmap
def histogram
histogram = Hash.new(0)
Line 1,554 ⟶ 1,550:
 
Pixmap.open('file.ppm').save_as_blackandwhite('file_bw.ppm')</syntaxhighlight>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">extern crate image;
use image::{DynamicImage, GenericImageView, ImageBuffer, Rgba};
 
Line 1,625 ⟶ 1,620:
}
</syntaxhighlight>
 
=={{header|Scala}}==
See also
Line 1,632 ⟶ 1,626:
* [[Read_ppm_file#Scala|Read a PPM File]] image loading
 
<syntaxhighlight lang="scala">object BitmapOps {
def histogram(bm:RgbBitmap)={
val hist=new Array[Int](255)
Line 1,666 ⟶ 1,660:
 
Usage:
<syntaxhighlight lang="scala">val img=Pixmap.load("image.ppm").get
val hist=BitmapOps.histogram(img)
val mid=BitmapOps.histogram_median(hist);
Line 1,677 ⟶ 1,671:
}
}</syntaxhighlight>
 
=={{header|Tcl}}==
{{libheader|Tk}}
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
 
Line 1,731 ⟶ 1,724:
}
}</syntaxhighlight>
 
=={{header|Vedit macro language}}==
The input image is in edit buffer pointed by numeric register #15.
On return, #30 points to buffer containing histogram data.
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
for (#9=0; #9<256; #9++) {
Line 1,746 ⟶ 1,738:
}
Return</syntaxhighlight>
 
=={{header|Wren}}==
{{libheader|DOME}}
<syntaxhighlight lang=ecmascript"wren">import "dome" for Window
import "graphics" for Canvas, Color, ImageData
 
class ImageHistogram {
construct new(filename, filename2) {
_image = ImageData.loadFromFileload(filename)
Window.resize(_image.width, _image.height)
Canvas.resize(_image.width, _image.height)
Line 1,838 ⟶ 1,829:
{{trans|C}}
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
image.data.howza(0).pump(Void,'wrap(c){ hist[c]+=1 }); // byte by byte loop
Line 1,852 ⟶ 1,843:
from
}</syntaxhighlight>
<syntaxhighlight lang="zkl">img:=PPM.readPPMFile("lenaGrey.ppm"); // a grey scale image
median:=histogramMedian(histogram(img));
median.println();
Line 1,865 ⟶ 1,856:
See the BBC Basic entry or:
http://www.zenkinetic.com/Images/RosettaCode/lenaBW.jpg
 
{{omit from|AWK}}
{{omit from|PARI/GP}}
9,476

edits