2値化

2値化とは、画像のある範囲のピクセル値を0か1など2つの値に変換することを指す
物体の輪郭を明確にしたい時などに使用する
2値のどちらに振り分けるかを決定する値を閾値といいう

例:
モノクロモデルで色が0〜255までの256階調の画像を0〜127の値は0に、128〜255の値は255に変換する

OpenCVおよびPythonはUbuntuで開発したいので、18.04を起動してlampを構築し、php-gdを入れます。

$ vagrant init ubuntu/bionic64
$ vagrant up
$ vagrant ssh
$ uname -a
Linux ubuntu-bionic 4.15.0-96-generic #97-Ubuntu SMP Wed Apr 1 03:25:46 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

// install tasksel
$ sudo apt install tasksel
// lamp install
$ sudo tasksel install lamp-server
$ apachectl -v
Server version: Apache/2.4.29 (Ubuntu)
$ mysql --version
mysql  Ver 14.14 Distrib 5.7.29, for Linux (x86_64) using  EditLine wrapper
$ php --version
PHP 7.2.24-0ubuntu0.18.04.3 (cli) (built: Feb 11 2020 15:55:52) ( NTS )
$ sudo service apache2 start

// gd install
$ apt-cache search gd | grep php
$ sudo apt-get install php7.2-gd
header ("content-type: image/png");

$filename = "chicks.jpg";
$image = ImageCreateFromJPEG ( $filename );

imagefilter ( $image , IMG_FILTER_EDGEDETECT );
imagefilter ( $image , IMG_FILTER_GRAYSCALE );
imagefilter ( $image , IMG_FILTER_SMOOTH , 8 );
imagefilter ( $image , IMG_FILTER_BRIGHTNESS , 20 );
imagefilter ( $image , IMG_FILTER_CONTRAST , -255 );

imagepng ( $image );
imagedestroy ( $image );

IMG_FILTER_EDGEDETECT: 画像の輪郭抽出
IMG_FILTER_GRAYSCALE:REC.601 luma (Y’) の計算と同じ係数を使い、 重み付けを赤、緑、青のコンポーネントごとに変えることで、 画像を白黒
IMG_FILTER_SMOOTH:画像を滑らかにする
IMG_FILTER_BRIGHTNESS:画像の輝度
IMG_FILTER_CONTRAST:画像のコントラスト

EDGEDETECTをなくすと、こうなる

BRIGHTNESSとCONTRASTで2値化している。

grayscaleのアルゴリズム

線形RGB空間において重み合計(英語版)を計算しなければならない。それはつまり、ガンマ圧縮関数は最初にガンマ拡張によって取り除かれるということである
...
光度は3つの線形的な光の強さの値の重み合計として計算

REC.601 luma

The Rec. 601 signal can be regarded as if it is a digitally encoded analog component video signal, and thus the sampling includes data for the horizontal and vertical sync and blanking intervals. Regardless of the frame rate, the luminance sampling frequency is 13.5 MHz. The samples are uniformly quantized using 8 or 10 bit PCM codes in the YCbCr domain.

For each 8 bit luminance sample, the nominal value to represent black is 16 and the value for white is 235. Eight-bit code values from 1 through 15 provide footroom, and can be used to accommodate transient signal content such as filter undershoots. Similarly, code values 236 through 254 provide headroom and can be used to accommodate transient signal content such as filter overshoots. The values 0 and 255 are used to encode the sync pulses and are forbidden within the visible picture area. The Cb and Cr samples are unsigned and use the value 128 to encode the neutral color difference value, as used when encoding a white, grey or black area.

REC 601は色変換の規格ってことか?