0

我正在尝试使用 imagemagick 调整图像的平均亮度。我已经转换了图像的大小和颜色,因此它们现在处于灰度状态,如下所示:

body_heavy_female_gray_resize

接下来我需要调整每个图像的亮度,使其匹配(用于研究)。目标亮度平均值为 189。

我使用此代码来获取亮度值:

$ convert image -colorspace LAB -channel r -separate +channel -format "%[mean]\n" info:  

这给出了 65535 的值(来自这篇文章

我使用公式 x/65535 = 189/255 来了解我的高质量图像目标是:48,573。

上图目前是29319.5

有没有办法调整这个值并在命令行中将其设置为 48573?

我试过了:

convert image -colorspace LAB -channel r -evaluate set 48573

convert image -colorspace LAB -channel r -evaluate set "48573"

每次错误出现在所列数字中时,我都尝试将最终数字更改为 189、89 和 .89(以防我的尺寸错误)。

> convert:  `.89' @ error/convert.c/ConvertImageCommand/3272

我一直在努力解决这个问题,并根据下面留下的评论进行了调整,所以现在我在这里:

目标图像:

我运行了以下脚本:

target_percent_luminance=74.12
hundred=100
echo "working on ${target_pic}"
gray_mean_val=$(magick identify -verbose      ${target_pic} | grep mean | awk '{print $2}' | sed -n '1p')
percent_gray_mean_val=$(echo     $hundred\*$gray_mean_val/255 | bc)
echo $percent_gray_mean_val 
difference=$(echo 74.12-$percent_gray_mean_val | bc)
echo $difference
magick convert ${target_pic} -modulate ${difference}% ${target_pic}_luminance.jpg

每一行都有效——输出:

casey$ target_percent_luminance=74.12
casey$ hundred=100
casey$ echo "working on ${target_pic}"
working on F201_background_gray_resized.jpg
casey$ gray_mean_val=$(magick identify -verbose  ${target_pic} | grep mean | awk '{print $2}' | sed -n '1p')
casey$ percent_gray_mean_val=$(echo $hundred\*$gray_mean_val/255 | bc)
casey$ echo $percent_gray_mean_val 
40
casey$ difference=$(echo 74.12-$percent_gray_mean_val | bc)
casey$ echo $difference 
34.12
casey$ magick convert ${target_pic} -modulate ${difference}% ${target_pic}_luminance.jpg

但这是输出图像,看起来太暗了。任何人都可以看到错误吗?

使用下面的 GeeMac 的答案,我写道:

casey$ input=F201_background_gray_resized.jpg
casey$ magick $input -brightness-contrast "%[fx:${lumin}-(mean*100)]" ${input}_lumintwo.jpg

并得到了这张看起来更好的图像!

4

1 回答 1

1

如果您使用的是 IM7,您可以直接在“magick ...”命令中进行大量计算。例如,此命令读取输入图像,并调整亮度,使输出图像的平均值为 74.12%...

lumin=74.12

magick input.jpg -brightness-contrast "%[fx:${lumin}-(mean*100)]" output.jpg

我不知道这与使用“-modulate N”进行调整相比如何,但是当我用这个检查输出时......

magick output.jpg -format "%[fx:mean*100]\n" info:

...结果是“74.1219”,或者 ${lumin} 的值是什么。它可能会给你另一种方法来考虑。

于 2018-10-23T23:47:36.910 回答