题目

If we obtain and observe the histogram of an image, we can see that, typically, pixels are not filled all ranges of its gray scale values. More specifically, there are no pixels near gray scale 0, or gray scale 255. Of course, this varies from image to image. From what we learn in class, an image with such a histogram will demonstrate itself with low contrast. Besides histogram equalization, we can define an adaptive gray scale transform to make this kind of images with higher contrast. Assume l and h, respectively, the lowest and highest pixel value in the image. we can define the gray scale transform as: s = 255*(r-l)/(h-l), where r is input gray scale value, and s is transformed. In the above equation, when the resulting s is not integer, rounding to its nearest integer should be applied. For example, 0.4 should be rounded to 0, and 0.5 to 1 This image adaptive gray scale transform is called autolevel, which one can find in most popular photo toching software, such as photoshop, gimp and so forth. The purpose of this project is to implement autolevel. and gives processing result of a sample image, lena.bmp One should read in Lenna.jpg and convert it to grayscale (for this, one can use readimg.py listed in ppt). Implement the proto-type function: GenAutolevelMapping, and then implement the proto-type function ImageAutolevel, In the prototype function Main, one should accomplish the whole task. proto-type functions are already provided in the autolevel.py script, for which, one should not modify any of the first two prototype functions presented above!upload code and resulting image to your repository, the resulted image should be named to aLenna.bmp ( a file with not exact the same name will not be accepted! )Also note, never ever modify the name and parameters of the prototype functions provided in the py files

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import numpy as np
from PIL import Image


def GenAutolevelMapping(hist):

non_zero_indices = np.where(hist > 0)[0]
l = non_zero_indices.min()
h = non_zero_indices.max()

# 创建一个映射函数,将原始灰度值r映射到新的灰度值s
def mapping(r):
if l == h: # 如果最低和最高灰度值相同(例如,所有像素值都相同),则不进行映射
return r
else:
return int(255 * (r - l) / (h - l))

return mapping


def ImageAutolevel(f):
# 将输入图像f转换为灰度图像(如果它还不是灰度图像的话)
if len(f.shape) == 3 and f.shape[2] == 3: # 假设f是一个形状为(height, width, 3)的RGB图像
f = np.dot(f[..., :3], [0.299, 0.587, 0.114]) # 转换为灰度图像

# 计算图像的直方图
hist, _ = np.histogram(f.flatten(), bins=256, range=(0, 256))

# 生成自动色阶映射函数
mapping = GenAutolevelMapping(hist)

# 应用映射函数到图像上
g = np.array([mapping(pixel) for pixel in f.flatten()]).reshape(f.shape)

# 将结果转换为图像对象(如果需要的话)
g = Image.fromarray(g.astype('uint8'))

return g


def Main():
# 读取输入图像
input_image_path = 'Lenna.jpg'
f = Image.open(input_image_path).convert('RGB') # 确保图像是RGB格式
f = np.array(f)

# 应用自动色阶处理
g = ImageAutolevel(f)

# 保存处理后的图像
output_image_path = 'aLenna.bmp'
g.save(output_image_path)

print(f"Processed image saved as {output_image_path}")