请按照如下过程,完成指定工作任务
1. 读取并显示人体骨骼图像 A。
2. 基于 LAPLACE 运算模型的图像 A 的增强(这是一种增强方式) (1)对人体的骨骼图像 A 进行 LAPLACE 运算得图像 B,显示图像 B; 并对其求绝对值,得绝对值图像 B1. (2)生成 LAPLACE 增强后的图像:C=A+B,并可视化图像 C
3.结合一阶微分、二阶微分、幂律运算进行输入图像 A 的混合增强(这是一种增强方式)
(1)对输入图像的 LAPLACE 运算结果 B 计算绝对值,得到图像 B1
(2)对输入图像 A 采用 SOBEL 算子,计算图像的梯度幅值,得到梯度幅值图像 D,对其可视化
(3)采用 5*5 的盒式滤波(即:均值滤波)对梯度幅值图像 D 进行平滑,得到图像 E,对其可视化
(4)模板图像的生成:图像 B1 与图像 E 的对应元素计算乘积,得到乘积图像 F,以此作为模板图像, 并进行可视化。
(5)生成增强的图像 G: G=A+F。对其可视化。
(6)采用幂律运算(gamma=0.5)对增强后的图像 G 进行暗区的对比度增强,得到最终的增强结果 H,并可视化
代码实现
from PIL import Image
from pylab import *
import cv2
import numpy as np
import math
import imutils
from skimage import exposure
img1 = array(Image.open(r'./任务4_人体骨骼图像.tif').convert('L'))
cv2.imshow('A',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
#Laplace算子
laplace = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]])
# 新创建一个图像,给图像边缘填充0
img2 = np.zeros((img1.shape[0] + 2, img1.shape[1] + 2), np.uint8)
high, wide = img2.shape
# 将旧图像复制到新图像中
for i in range(1, high-1):
for j in range(1, wide-1):
img2[i, j] = img1[i - 1, j - 1]
# 创建结果图像
result_image1 = np.zeros((high, wide), np.uint8)
result_image2 = np.zeros((high, wide), np.uint8)
# 卷积运算
for i in range(0, high - 2):
for j in range(0, wide - 2):
# 计算结果图像的每个像素点
sum = 0
for row_i in range(3):
for col_j in range(3):
sum = laplace[row_i, col_j] * img2[i + row_i, j + col_j] + sum
result_image1[i + 1, j + 1] = sum
result_image2[i + 1, j + 1] = abs(sum)
cv2.imshow("B", result_image1)
cv2.imshow("B1", result_image2)
cv2.waitKey(0)
cv2.destroyAllWindows()
tmp = result_image1[1:801,1:501]
img3 = cv2.bitwise_and(img1,tmp)
cv2.imshow("C", img3)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = img1
sobelx = cv2.Sobel(img, cv2.CV_64F, dx=1, dy=0)
sobelx = cv2.convertScaleAbs(sobelx)
sobely = cv2.Sobel(img, cv2.CV_64F, dx=0, dy=1)
sobely = cv2.convertScaleAbs(sobely)
result = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
#cv2.imshow("Original", img)
#cv2.imshow("sobelx", sobelx)
#cv2.imshow("sobely", sobely)
cv2.imshow("D", result)
cv2.waitKey()
cv2.destroyAllWindows()
E = cv2.blur(result, (5,5))
cv2.imshow('E', E)
cv2.waitKey(0)
cv2.destroyAllWindows()
tmp = result_image2[1:801,1:501]
F = cv2.multiply(E,tmp)
cv2.imshow('F', F)
cv2.waitKey(0)
cv2.destroyAllWindows()
G = cv2.bitwise_and(img1,F)
cv2.imshow('G', G)
cv2.waitKey(0)
cv2.destroyAllWindows()
image = G
gamma_img2= exposure.adjust_gamma(image, 0.5)
gamma_img2 = cv2.convertScaleAbs(gamma_img2)
cv2.imshow('H', gamma_img2)
if cv2.waitKey(0) == 27:
cv2.destroyAllWindows()