openc用numpy数组创建掩模,在图像中间取出大小为80×120的图像
时间: 2023-03-28 21:03:40 浏览: 213
可以使用以下代码实现:
import cv2
import numpy as np
# 读取图像
img = cv2.imread('image.jpg')
# 创建掩模
mask = np.zeros(img.shape[:2], dtype=np.uint8)
mask[img.shape[]//2-40:img.shape[]//2+40, img.shape[1]//2-60:img.shape[1]//2+60] = 255
# 应用掩模
masked_img = cv2.bitwise_and(img, img, mask=mask)
# 取出图像
cropped_img = masked_img[img.shape[]//2-40:img.shape[]//2+40, img.shape[1]//2-60:img.shape[1]//2+60]
# 显示图像
cv2.imshow('Cropped Image', cropped_img)
cv2.waitKey()
cv2.destroyAllWindows()
相关问题
多种掩模对图像进行修复代码
以下是基于Python和OpenCV库实现的多种掩模图像修复代码:
```python
import cv2
import numpy as np
# 读取图像
img = cv2.imread('image.jpg')
# 创建掩模
mask1 = np.zeros(img.shape[:2], np.uint8)
mask2 = cv2.imread('mask.jpg', 0)
# 使用矩形掩模修复图像
rect = (100, 100, 300, 300)
cv2.rectangle(mask1, rect, 255, -1)
result1 = cv2.inpaint(img, mask1, 3, cv2.INPAINT_TELEA)
# 使用椭圆形掩模修复图像
mask2 = cv2.threshold(mask2, 220, 255, cv2.THRESH_BINARY)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
mask2 = cv2.erode(mask2, kernel, iterations=4)
result2 = cv2.inpaint(img, mask2, 3, cv2.INPAINT_TELEA)
# 显示结果图像
cv2.imshow('Original Image', img)
cv2.imshow('Rectangular Mask Result', result1)
cv2.imshow('Elliptical Mask Result', result2)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在上述代码中,我们使用了两个不同的掩模,矩形和椭圆形,分别对图像进行修复。在使用矩形掩模时,我们使用cv2.rectangle函数创建一个矩形,将其填充为白色,并将其传递给cv2.inpaint函数进行修复。在使用椭圆形掩模时,我们首先将掩模二值化并腐蚀,以便更好地定义需要修复的区域,然后使用cv2.getStructuringElement函数构造一个椭圆形内核,并使用cv2.erode函数对掩模进行形态学变换。最后,我们将掩模传递给cv2.inpaint函数,使用cv2.INPAINT_TELEA算法对图像进行修复。
OpenCV 图像暗角
OpenCV中可以通过添加暗角(vignette)的方法来使图像看起来更加吸引人。暗角是指图像边缘比中央区域更暗的效果。
以下是一个简单的例子:
```python
import cv2
import numpy as np
def add_vignette(img):
rows, cols = img.shape[:2]
kernel_x = cv2.getGaussianKernel(cols, 200)
kernel_y = cv2.getGaussianKernel(rows, 200)
kernel = kernel_y * kernel_x.T
mask = 255 * kernel / np.linalg.norm(kernel)
output = np.copy(img)
for i in range(3):
output[:,:,i] = output[:,:,i] * mask
return output
# 读取图像
img = cv2.imread('test.jpg')
# 添加暗角
img_with_vignette = add_vignette(img)
# 显示图像
cv2.imshow('Original', img)
cv2.imshow('Vignette', img_with_vignette)
cv2.waitKey()
cv2.destroyAllWindows()
```
在这个例子中,我们使用高斯内核来创建一个掩模(mask),然后将其应用于原始图像的每个通道。通过调整内核大小和标准差,可以控制暗角的大小和强度。
注意,这个例子只是一个简单的示例,实际应用中可能需要根据具体情况进行调整。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)