如何使用OpenCV使用最小值和最大值调整亮度和对比度 [英] How to adjust brightness and contrast using min and max values using OpenCV

查看:316
本文介绍了如何使用OpenCV使用最小值和最大值调整亮度和对比度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ImageJ中,您可以使用最小值和最大值来调整图像的亮度和对比度.您也可以使用setMinAndMax()宏功能.该对话框如下所示:

In ImageJ you can adjust the brightness and contrast of an image using minimum and maximum values. You can also use the setMinAndMax() macro function. The dialog looks like this:

它将每个像素映射为适合最小值和最大值之间.

It maps each pixel to fit between the minimum and maximum values.

我正在尝试在OpenCV(C ++)中做同样的事情.我可以使用Mat::convertTo()alphabeta参数更改对比度和亮度,但是我不知道如何使用最小值和最大值来实现对比度.

I'm trying to do the same thing in OpenCV (C++). I can change the contrast and brightness using the alpha and beta parameters of Mat::convertTo(), but I don't know how to do it with minimum and maximum values.

在我的情况下,我使用的是12位图像,因此像素值的范围是0到4095.我不确定这是否重要.

In my case I'm using a 12-bit image so the pixel values range from 0 to 4095. I'm not sure if that matters.

推荐答案

您要这样做.

首先,找到当前的最大值和最小值.

First, find the current maximum and minimum.

假设最暗和最亮分别是80和220.现在,您需要将该范围80..220扩展到整个范围0..4095.

Let's say the darkest and brightest are 80 and 220 respectively. Now you need to stretch this range 80..220 onto the full range 0..4095.

因此,您从图像中的每个像素减去80,以在直方图的左端向下移至零,因此您的范围现在为0..140.

So you subtract 80 from every pixel in your image to shift down to zero at the left end of the histogram, so your range is now 0..140.

现在,您需要将每个像素乘以4095/140,以将右端拉伸到4095.

Now you need to multiply every pixel by 4095/140 to stretch the right end out to 4095.

有效地,您需要的公式是这样:

Effectively, the formula you need is this:

newvalue = int((current value - darkest)*4095/(brightest-darkest))

这篇关于如何使用OpenCV使用最小值和最大值调整亮度和对比度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆