请用Python写出如何在一张图像上绘制mask的代码
时间: 2024-05-11 20:15:32 浏览: 121
涉及技术问题,可以直接回答:
```python
import cv2
import numpy as np
# 加载图像
img = cv2.imread("test.jpg")
# 创建 mask
mask = np.zeros(img.shape[:2], dtype=np.uint8)
# 绘制 mask
cv2.circle(mask, center=(300,300), radius=200, color=255, thickness=-1)
# 将 mask 应用于图像
img_masked = cv2.bitwise_and(img, img, mask=mask)
# 显示图像和 mask
cv2.imshow("image", img)
cv2.imshow("mask", mask)
cv2.imshow("masked_image", img_masked)
cv2.waitKey(0)
```
这个代码展示了如何在一张图像上绘制一个圆形的 mask,并将其应用于图像上,最终生成一个带 mask 效果的新图像。
相关问题
请用Python与matplotlib写出如何在一张图像上绘制mask的代码
下面是使用Python与matplotlib在一张图像上绘制mask的代码:
```python
import matplotlib.pyplot as plt
import numpy as np
# 加载图像
img = plt.imread('image.png')
# 创建一个黑色的遮罩
mask = np.zeros_like(img[:,:,0])
# 绘制矩形区域的遮罩
mask[100:250, 100:300] = 1
# 将遮罩应用于原始图像
masked_img = np.copy(img)
masked_img[:,:,0] = np.where(mask==1, 255, masked_img[:,:,0])
masked_img[:,:,1] = np.where(mask==1, 0, masked_img[:,:,1])
masked_img[:,:,2] = np.where(mask==1, 0, masked_img[:,:,2])
# 绘制原始图像和遮罩后的图像
fig, axs = plt.subplots(1, 2)
axs[0].imshow(img)
axs[0].set_title('Original Image')
axs[1].imshow(masked_img)
axs[1].set_title('Masked Image')
plt.show()
```
这段代码假设你要绘制一个长宽为200x200,左上角坐标为(100, 100)的矩形区域的遮罩,并将其应用于名为"image.png"的原始图像。运行后,将在一个窗口中呈现出原始图像和遮罩后的图像。
用python写图像二值化,形态学处理,轮廓提取,抠图,抠出来的图为主体对其更换背景在进行高斯模糊的代码
### 回答1:
以下是Python的代码示例,包含图像二值化、形态学处理、轮廓提取、图像抠图并更换背景并进行高斯模糊的过程:
```
import cv2
# 读取原始图像
image = cv2.imread('image.jpg')
# 转换为灰度图像
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 图像二值化
ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# 形态学处理
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
# 轮廓提取
contours, hierarchy = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 查找主体轮廓
main_contour = max(contours, key=cv2.contourArea)
# 绘制主体轮廓
contour_image = cv2.drawContours(image.copy(), [main_contour], -1, (0, 255, 0), 2)
# 图像抠图并更换背景
mask = cv2.drawContours(np.zeros_like(gray), [main_contour], 0, 255, -1)
background = cv2.imread('background.jpg')
background = cv2.resize(background, image.shape[:2][::-1])
background_mask = cv2.bitwise_not(mask)
foreground = cv2.bitwise_and(image, image, mask=mask)
background = cv2.bitwise_and(background, background, mask=background_mask)
result = cv2.add(foreground, background)
# 高斯模糊
result = cv2.GaussianBlur(result, (11, 11), 0)
# 显示结果图像
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
以上代码中,image为原始图像,background为新背景图像,分别调用cv2.imread进行读取。接下来依次进行图像二值化、形态学处理、轮廓提取,通过查找主体轮廓,得到二值化后的主体部分,然后通过对主体部分进行图像抠图操作,并将其与新背景图像进行合并,得到抠出来的主体带新背景的图像。最后进行高斯模糊。注:该代码适用于单一主体的图像。
### 回答2:
下面是实现图像二值化、形态学处理、轮廓提取、抠图、更换背景并进行高斯模糊的Python代码:
```python
import cv2
import numpy as np
# 读取图像
image = cv2.imread("image.png")
# 图像二值化
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 形态学处理
kernel = np.ones((3, 3), np.uint8)
dilation = cv2.dilate(binary, kernel, iterations=1)
erosion = cv2.erode(dilation, kernel, iterations=1)
# 轮廓提取
contours, _ = cv2.findContours(erosion, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contoured_image = cv2.drawContours(image.copy(), contours, -1, (0, 255, 0), 2)
# 抠图
mask = np.zeros(image.shape[:2], np.uint8)
cv2.drawContours(mask, contours, -1, (255), -1)
foreground = cv2.bitwise_and(image, image, mask=mask)
# 更换背景
background = cv2.imread("background.png")
background = cv2.resize(background, (image.shape[1], image.shape[0]))
background = cv2.bitwise_and(background, background, mask=cv2.bitwise_not(mask))
# 合并图像
result = cv2.add(foreground, background)
# 高斯模糊
result = cv2.GaussianBlur(result, (5, 5), 0)
# 显示结果
cv2.imshow("Result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
以上代码中,需要将`image.png`替换为待处理的图像文件,`background.png`替换为更换的背景图像文件。其中,二值化阈值为127,形态学处理中膨胀和腐蚀的迭代次数均为1,高斯模糊的核大小为5x5。最后,将结果显示出来,并等待按下任意键关闭窗口。
### 回答3:
下面是用Python编写的图像二值化、形态学处理、轮廓提取、抠图、更换背景和进行高斯模糊的代码示例:
```python
import cv2
import numpy as np
def image_binarization(image):
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, binary_image = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY)
return binary_image
def morphology_processing(binary_image):
kernel = np.ones((5, 5), np.uint8)
closing = cv2.morphologyEx(binary_image, cv2.MORPH_CLOSE, kernel)
return closing
def extract_contours(image):
contours, _ = cv2.findContours(image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
return contours
def extract_object(image, contours):
mask = np.zeros_like(image)
cv2.drawContours(mask, contours, -1, (255, 255, 255), thickness=cv2.FILLED)
extracted_image = np.zeros_like(image)
extracted_image[mask == 255] = image[mask == 255]
return extracted_image
def replace_background(image, background_image):
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, mask = cv2.threshold(image_gray, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
background = cv2.bitwise_and(background_image, background_image, mask=mask_inv)
foreground = cv2.bitwise_and(image, image, mask=mask)
result = cv2.add(background, foreground)
return result
def gaussian_blur(image):
blurred_image = cv2.GaussianBlur(image, (5, 5), 0)
return blurred_image
# 示例用法
image = cv2.imread('input.jpg')
binary_image = image_binarization(image)
morphology_processed_image = morphology_processing(binary_image)
contours = extract_contours(morphology_processed_image)
object_image = extract_object(image, contours)
background_image = cv2.imread('background.jpg')
replaced_image = replace_background(object_image, background_image)
blurred_image = gaussian_blur(replaced_image)
cv2.imshow('Image', image)
cv2.imshow('Binary', binary_image)
cv2.imshow('Morphology Processed', morphology_processed_image)
cv2.imshow('Object', object_image)
cv2.imshow('Replaced', replaced_image)
cv2.imshow('Blurred', blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这段代码使用OpenCV库进行图像处理。首先,将输入图像进行二值化处理,然后进行形态学闭操作,接着提取轮廓,抠出图像的主体部分,再将主体与新的背景图像进行替换,最后对结果图像进行高斯模糊处理。具体的每个函数实现了对应的功能,示例用法展示了如何将每个步骤的结果显示出来。
阅读全文