Home php教程 php手册 php中使用Imagick实现图像直方图的实现代码

php中使用Imagick实现图像直方图的实现代码

Jun 06, 2016 pm 08:38 PM
imagick Histogram

玩过单反相机的人应该都知道图像直方图(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 of this Website
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

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1270
29
C# Tutorial
1250
24
Image transparency through php and Imagick Image transparency through php and Imagick Jul 29, 2023 am 09:45 AM

Introduction to image transparency through PHP and Imagick: Image transparency is a common image processing requirement. By making a certain color or area in the image transparent, various special effects can be achieved. This article will introduce how to use php and Imagick library to achieve image transparency processing, and provide code examples for reference. Imagick is a powerful image processing library that provides a wealth of image processing functions, including image reading, editing, saving, etc. With Imagick we

Use php and Imagick to achieve color conversion of images Use php and Imagick to achieve color conversion of images Jul 29, 2023 pm 04:49 PM

Using PHP and Imagick to realize color conversion of images Introduction: In web development, we often need to process images, and one of the common needs is to modify the color of images. This article will introduce how to use PHP and Imagick extensions to achieve color conversion of images. Imagick is a powerful image processing extension for PHP that provides many feature-rich methods, including image cutting, scaling, rotation, and more. In terms of color conversion, Imagick also provides a series of methods to achieve

Best practices for image resizing using php and Imagick Best practices for image resizing using php and Imagick Jul 29, 2023 pm 05:57 PM

Best Practices for Image Resizing Using PHP and Imagick Quote: In the modern Internet era, images are an integral part of web pages and applications. In order to improve user experience and speed up web page loading, images usually need to be resized to adapt to different display devices and resolutions. This article will introduce how to use php and the Imagick library to implement best practices for image resizing, and provide code examples. 1. Install the Imagick extension. Before starting, we first need to ensure that the server

Combine multiple images into one via php and Imagick Combine multiple images into one via php and Imagick Jul 28, 2023 pm 08:39 PM

Combining multiple images into one through php and Imagick In web development, sometimes we need to combine multiple images into one to facilitate display and save page loading time. In this article, we will introduce how to use php and the Imagick library to achieve this functionality. Imagick is a powerful image processing library that provides a wealth of image processing methods and functions. First, we need to install the Imagick extension in php. Next, we will demonstrate how to convert multiple

How to color adjust images using php and Imagick How to color adjust images using php and Imagick Jul 28, 2023 pm 01:57 PM

How to use PHP and Imagick to color adjust pictures Introduction: In web development, sometimes we need to color adjust pictures to meet design requirements or optimize picture effects. PHP provides a rich image processing library, among which Imagick is a powerful and easy-to-use extension that can easily adjust the color of pictures. This article will introduce how to use PHP and Imagick to realize color adjustment of pictures, and give corresponding code examples. 1. Install the Imagick extension: To use

Image sharpening through php and Imagick Image sharpening through php and Imagick Jul 29, 2023 pm 01:33 PM

Image sharpening through php and Imagick In modern image processing, sharpening is a common technology, which can improve the details and clarity of images and make them more vivid. In this article, we will introduce how to use php and the Imagick library to achieve image sharpening. First, make sure you have the Imagick library installed on your server. If it is not installed, you can install it with the following command: sudoapt-getinstallphp-imagick

Use php and Imagick to implement special effects processing of images Use php and Imagick to implement special effects processing of images Jul 28, 2023 pm 06:33 PM

Use PHP and Imagick to implement special effects processing of pictures Summary: Special effects processing of pictures can add some artistic effects to the pictures or change the appearance of the pictures. PHP and Imagick can implement many common image special effects processing. This article will introduce some common special effects processing and provide corresponding code examples. Install the Imagick extension Before you begin, make sure you have the Imagick extension installed. If it is not installed, you can install it through the following steps: #Install Imagick extension $pec

Cropping and scaling of images through php and Imagick Cropping and scaling of images through php and Imagick Jul 28, 2023 pm 06:18 PM

Image cropping and scaling through PHP and Imagick Summary: In web development, images often need to be cropped and scaled to suit various needs. This article will introduce how to use PHP and the Imagick library to achieve image cropping and scaling, and provide code examples for readers' reference. Introduction: With the rapid development of the Internet, images play an increasingly important role in web pages. However, since each web page has its own layout and size requirements, images often need to be cropped and scaled to adapt to different scenarios. P

See all articles