Write a python script to open the “lena.png” file using OpenCV. • Display the opened image in a new window named “Display Lena” • Save the image to a new file named “lena_resaved.png”,其中lena.png存放在D盘data_test文件夹下
时间: 2024-04-08 13:30:48 浏览: 17
下面是一个使用OpenCV打开并显示图像,并将其保存为新文件的Python脚本:
```python
import cv2
# 图像文件路径
image_path = 'D:/data_test/lena.png'
# 使用OpenCV打开图像
image = cv2.imread(image_path)
# 显示图像
cv2.imshow('Display Lena', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 保存图像到新文件
save_path = 'D:/data_test/lena_resaved.png'
cv2.imwrite(save_path, image)
```
在这个脚本中,首先指定了要打开的图像文件的路径 `image_path`,然后使用 `cv2.imread()` 函数读取图像并存储在 `image` 变量中。然后使用 `cv2.imshow()` 显示图像,窗口的名称为 "Display Lena"。通过 `cv2.waitKey(0)` 等待用户按下任意键来关闭显示窗口。最后,使用 `cv2.imwrite()` 函数将图像保存为新文件,保存路径为 `save_path`。
请确保将图像文件 "lena.png" 放在指定的路径 "D:/data_test/" 下,并运行以上代码来打开、显示和保存图像。
相关问题
python中实现以下任务:(a) Load the image 'lena.jpg', convert it to grayscale. (b) Perform sharpening spatial filtering to enhance the image with different parameters (at least 3 kinds). (Tips: skimage.filters.laplace)
可以使用以下代码实现:
```python
import skimage.io as io
import skimage.color as color
import skimage.filters as filters
# Load the image and convert it to grayscale
img = io.imread('lena.jpg')
gray_img = color.rgb2gray(img)
# Perform sharpening spatial filtering with different parameters
sharpened_img1 = gray_img - filters.laplace(gray_img, ksize=3)
sharpened_img2 = gray_img - filters.laplace(gray_img, ksize=5)
sharpened_img3 = gray_img - filters.laplace(gray_img, ksize=7)
```
其中,`ksize`参数表示Laplace算子的大小,越大的值会导致更强的锐化效果。
用OpenCV提高lena.jpg图像的对比度的代码
可以使用OpenCV的equalizeHist函数来提高lena.jpg图像的对比度。以下是示例代码:
```python
import cv2
img = cv2.imread('lena.jpg', 0)
equ = cv2.equalizeHist(img)
cv2.imshow('Original Image', img)
cv2.imshow('Equalized Image', equ)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在这个例子中,我们首先使用cv2.imread函数读取lena.jpg图像并将其转换为灰度图像。然后,我们使用equalizeHist函数来提高图像的对比度,并将结果存储在变量equ中。最后,我们使用cv2.imshow函数显示原始图像和增强后的图像,并使用cv2.waitKey和cv2.destroyAllWindows函数等待用户操作和关闭窗口。