Python程序设计,OpenCV的使用,灰度直方图

编写Python程序,用OpenCV工具包中的函数读取、显示并保存一幅彩色图像。

import cv2

img = cv2.imread('source.jpg') 

cv2.imshow('image', img) cv2.waitKey(0)  

cv2.imwrite('output.jpg', img)  

cv2.destroyAllWindows()

14. (简答题,)编写Python程序,用OpenCV读入一幅图像(原图像可以是彩色或灰度图像)后,用Matplotlib工具包中的函数显示并保存一幅灰度图像。

import cv2

from matplotlib import pyplot as plt

# 读取图像

img = cv2.imread('source.jpg')  

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 使用Matplotlib显示图像

plt.imshow(gray_img, cmap='gray')

plt.show()

# 保存图像

plt.imsave('output_gray.jpg', gray_img, cmap='gray')  

 (简答题,)

计算下图的平均灰度值,并编写Python程序计算器平均灰度。

平均灰度值:

(2 + 4 + 5 + 6 + 3 + 1 + 5 + 3 + 6 + 2 + 2 + 2) / 12 = 3.5

import numpy as np

image = np.array([[2, 4, 5, 6],

                  [3, 1, 5, 3],

                  [6, 2, 2, 2]])

average_gray = np.mean(image)

print("The average gray level is", average_gray)

(简答题)

编写python程序,生成一幅大小为512*512的黑底(灰度为0),中央为200*200大小白色矩形(灰度为255)的图像。


import cv2
import numpy as np

# 创建一个512*512的黑色图像
img = np.zeros((512, 512), dtype=np.uint8)

# 在中央创建一个200*200的白色矩形
start_point = (int(512/2 - 200/2), int(512/2 - 200/2))  # 矩形左上角的坐标
end_point = (int(512/2 + 200/2), int(512/2 + 200/2))  # 矩形右下角的坐标
color = 255  # 白色
thickness = -1  # 填充矩形
img = cv2.rectangle(img, start_point, end_point, color, thickness)

# 显示图像
cv2.imshow('image', img)
cv2.waitKey(0)  # 等待用户关闭窗口

# 保存图像
cv2.imwrite('output.jpg', img)  # 在这里替换为你想要保存的图像路径

# 关闭所有窗口
cv2.destroyAllWindows()

 (简答题, )编写Python程序,读取一幅彩色图像,使用OpenCV的cvtColor函数将其转为灰度图像,生成其直方图并显示(可使用OpenCV函数或Matplotlib函数或自行计算后用Matplotlib绘制)。

我的答案:

0

import cv2
from matplotlib import pyplot as plt

# 读取图像并转换为灰度图像
img = cv2.imread('source.jpg', cv2.IMREAD_GRAYSCALE)  # 在这里替换为你的源图像路径

# 计算直方图
hist = cv2.calcHist([img], [0], None, [256], [0,256])

# 使用matplotlib绘制直方图
plt.figure()
plt.title("Grayscale Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")
plt.plot(hist)
plt.xlim([0, 256])
plt.show()

计算题

某图像如下图所示,若图像灰度级为8(3bit),请绘制该图像的灰度直方图。

import numpy as np

import matplotlib.pyplot as plt

# 创建一个二维数组来代表图像

image = np.array([[1, 3, 2, 6],

                  [1, 2, 3, 4],

                  [2, 4, 0, 2],

                  [4, 2, 2, 3]])

# 将二维图像数组转换为一维

flattened_image = image.flatten()

# 设置bins的数量为8,因为灰度级为8

bins = np.arange(9) - 0.5

# 绘制直方图

plt.hist(flattened_image, bins, edgecolor='black')

plt.title("Gray Level Histogram")

plt.xlabel("Gray Levels")

plt.ylabel("Frequency")

plt.xticks(range(8))

plt.show()

若图像灰度级L=4,求估计图像g与原图像f的相似性指标:MSE、PSNR、SSIM。

 

import numpy as np

from skimage.metrics import mean_squared_error, peak_signal_noise_ratio, structural_similarity

# 创建两个二维数组来代表原图像和估计图像

f = np.array([[1, 1, 1, 1],

              [1, 1, 1, 1],

              [1, 1, 1, 1],

              [1, 1, 1, 1]])

g = np.array([[1, 1, 1, 1],

              [1, 0, 2, 1],

              [1, 2, 0, 1],

              [1, 1, 1, 1]])

# 计算MSE

mse = mean_squared_error(f, g)

print("MSE: ", mse)

# 计算PSNR,需要提供数据范围,因为灰度级为4,所以数据范围是3(从0到3)

psnr = peak_signal_noise_ratio(f, g, data_range=3)

print("PSNR: ", psnr)

# 计算SSIM,需要提供数据范围

ssim = structural_similarity(f, g, data_range=3)

print("SSIM: ", ssim)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值