在Matplolib中使用百分比累计计数增强对比度 [英] Contrast enhancement using a percentage cumulative count in Matplolib

查看:43
本文介绍了在Matplolib中使用百分比累计计数增强对比度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小型程序,用于根据手动选择的点对齐图像,并且将两个图像彼此相邻显示.为了使相似的栅格值具有相似的颜色,我想将颜色条拉伸为两个图像的相同值.在QGIS中,可以使用累积计数削减,您可以在其中输入百分比以增强栅格的对比度.我想使用matplotlib在我的单波段栅格图像中找到相应的栅格值(分别为2%和98%).然后,我将使用最小的2%值和最大的98%值作为图像的vmin和vmax.

我似乎在matplotlib中找不到该功能,所以有人有建议吗?

解决方案

您正在寻找的是

输出图像:

I am writing a small program to align images based on manually selected points and I display both images next to each other. To have similar colors for similar raster values I'd like to stretch the colorbar to the same values for both images. In QGIS there is a possibility to use a cumulative count cut where you can input percentages to enhance the contrast of a raster. I would like to find the corresponding (2% and 98%) raster values in my singleband raster image using matplotlib. Then I'd use the smallest 2% value and the largest 98% value as the vmin and vmax for my images.

I can't seem to find the function in matplotlib so does anyone have a suggestion?

解决方案

What you are looking for is percentile function.

Instead of using matplotlib, you may use some math for getting the desired result.

You can find lower and upper percentile using percentile function, and "stretch" pixel range linearly between lower and upper percentiles.

Here is a code sample:

import cv2
import numpy as np

img = cv2.imread('chelsea.png', cv2.IMREAD_GRAYSCALE)  # Read input image for testing

min_percent = 2   # Low percentile
max_percent = 98  # High percentile
lo, hi = np.percentile(img, (min_percent, max_percent))

# Apply linear "stretch" - lo goes to 0, and hi goes to 1
res_img = (img.astype(float) - lo) / (hi-lo)

#Multiply by 255, clamp range to [0, 255] and convert to uint8
res_img = np.maximum(np.minimum(res_img*255, 255), 0).astype(np.uint8)

#Display images before and after linear "stretch":
cv2.imshow('img', img)
cv2.imshow('res_img', res_img)
cv2.waitKey(0)
cv2.destroyAllWindows()


Result:

Input image:

Output image:

这篇关于在Matplolib中使用百分比累计计数增强对比度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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