图像处理与分析 实验二

163次阅读
没有评论

共计 1658 个字符,预计需要花费 5 分钟才能阅读完成。

实验内容:打开一幅低对比度图像,拉伸其图像,打开一幅过度曝光图像,拉伸其图像,观察图像变换,对图像直方图均衡算法

打开一幅低对比度图像

import cv2 as cv
import matplotlib.pyplot as plt

img1 = cv.imread('4.png')
img1 = cv.cvtColor(img1, cv.COLOR_BGR2GRAY)
plt.imshow(img1, cmap='gray', vmin=0, vmax=255)
img1.shape
(1024, 1024)

图像处理与分析 实验二

拉伸对比度

import numpy as np

rows, cols = img1.shape
flat_gray = img1.reshape((cols * rows,)).tolist()
min1 = min(flat_gray)
max1 = max(flat_gray)
output1 = np.uint8(255 / (max1 - min1) * (img1 - min1) + 0.5)
# output1 = np.uint8(img1  + 100)
# img1 = output1
# plt.imshow(output1, cmap='gray', vmin=0, vmax=255)
import math
def show_all(*arg):
    index = 1
    myLen = len(arg)
    row = math.ceil(math.sqrt(myLen))
    myCol = myLen // row
    for i in range(myCol + 1):
        for j in range(row):
            #print(myCol + 1, row)
            if i * row + j < myLen :
                if len(arg[i * row + j].shape) == 3:
                    b,g,r = cv.split(arg[i * row + j])  
                    t_img = cv.merge([r,g,b])
                else:
                    t_img = arg[i * row + j]
                plt.subplot(myCol + 1, row, i * row + j + 1)
                plt.imshow(t_img.astype('uint8'), cmap='gray', vmin=0, vmax=255)
                plt.title(index)
                index += 1
                plt.xticks([]), plt.yticks([])
            else:
                plt.show()
                return
show_all(img1, output1)

图像处理与分析 实验二

打开一副过曝图像

img2 = cv.imread('7.png')
img2 = cv.cvtColor(img2, cv.COLOR_BGR2GRAY)
plt.imshow(img2, cmap='gray', vmin=0, vmax=255)
img2.shape
(1024, 1024)

图像处理与分析 实验二

拉伸

rows, cols = img2.shape
flat_gray = img2.reshape((cols * rows,)).tolist()
min2 = min(flat_gray)
max2 = max(flat_gray)
output2 = np.uint8(255 / (max2 - min2) * (img2 - min2) + 0.5)
# output1 = np.uint8(img1  + 100)
# img1 = output1
# plt.imshow(output2, cmap='gray', vmin=0, vmax=255)
show_all(img2, output2)

图像处理与分析 实验二

直方图均衡算法

以低对比度图像为例

output3 = cv.equalizeHist(img1)
show_all(img1, output3)

图像处理与分析 实验二

def show_histogram(fx, gx):
    plt.figure(1)
    plt.hist(fx.ravel(), 256, [0, 256])
    plt.figure(2)
    plt.hist(gx.ravel(), 256, [0, 256])
    plt.show()
show_histogram(img1, output3)

图像处理与分析 实验二

图像处理与分析 实验二

正文完
 0
icvuln
版权声明:本站原创文章,由 icvuln 于2024-09-24发表,共计1658字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)