玩过单反相机的人应该都知道图像直方图(Image Histogram),简单点说,它通过计算每个色阶在总像素中所占的比例来反映图像的曝光情况。
我并不打算详细解释专业名词,有兴趣的读者可以查阅文章结尾处的参考链接,那里有通俗易懂的解释:
我们先找一个例子图像(用Canon 550D拍的):
例子图片:butterfly.jpg
下面看看如何使用Imagick实现图像直方图:
代码如下:
$file = 'butterfly.jpg';
$size = array(
'width' => 256,
'height' => 100,
);
$image = new Imagick($file);
$histogram = array_fill_keys(range(0, 255), 0);
foreach ($image->getImageHistogram() as $pixel) {
$rgb = $pixel->getColor();
$histogram[$rgb['r']] += $pixel->getColorCount();
$histogram[$rgb['g']] += $pixel->getColorCount();
$histogram[$rgb['b']] += $pixel->getColorCount();
}
$max = max($histogram);
$threshold = ($image->getImageWidth() * $image->getImageHeight()) / 256 * 12;
if ($max > $threshold) {
$max = $threshold;
}
$image = new Imagick();
$draw = new ImagickDraw();
$image->newImage($size['width'], $size['height'], 'white');
foreach ($histogram as $x => $count) {
if ($count == 0) {
continue;
}
$draw->setStrokeColor('black');
$height = min($count, $max) / $max * $size['height'];
$draw->line($x, $size['height'], $x, $size['height'] - $height);
$image->drawImage($draw);
$draw->clear();
}
$image->setImageFormat('png');
$image->writeImage('histogram.png');
?>
注:代码中之所以加入$threshold这个阀值,是因为有时候某些色阶的值可能会非常大,如果不做处理会干扰最终的生成效果。至于为什么要先除256,接着又乘12,没有什么道理可言,都是我一拍脑袋决定的,你也可以使用别的方法。
最终生成的直方图和Photoshop的效果基本一样,这里就贴一下Photoshop的:
Photoshop生成的直方图
注:使用Photoshop打开图片后,选择窗口,然后选择直方图即可。
本文说的实际上只是RGB通道的直方图绘制方法,原理上,RGB直方图是红绿蓝直方图累加的结果,至于红绿蓝三原色各自的直方图,上面代码稍加修改即可。
注:XARG.ORG上有一个HTML5实现的图像直方图开源项目,效果不错,值得学习。
最后顺便说一下,如果你对摄影知识感兴趣,可参考:。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
How to fix KB5055612 fails to install in Windows 10?
4 weeks agoByDDD
Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Roblox: Grow A Garden - Complete Mutation Guide
3 weeks agoByDDD
Nordhold: Fusion System, Explained
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
