python - 我想水平屏蔽多个图像

标签 python numpy opencv image-processing mask

我有很少的日记页面图像,其中有两列我想在不改变维度的情况下将一列屏蔽为白色。这意味着即使有一列,输出图像也应该与输入图像具有相同的维度。

我能够遮盖图像,但 mask 部分变黑了,我想要白色。

import cv2

import numpy as np

# Load the original image

image = cv2.imread(filename = "D:\output_final_word5\image1.jpg")

# Create the basic black image 

mask = np.zeros(shape = image.shape, dtype = "uint8")

# Draw a white, filled rectangle on the mask image

cv2.rectangle(img = mask, pt1 = (0, 0), pt2 = (795, 3000), color = (255, 255, 

255), thickness = -1)

# Apply the mask and display the result

maskedImg = cv2.bitwise_and(src1 = image, src2 = mask)

#cv2.namedWindow(winname = "masked image", flags = cv2.WINDOW_NORMAL)

cv2.imshow("masked image",maskedImg)

cv2.waitKey(delay = 0)

cv2.imwrite("D:\Test_Mask.jpg",maskedImg)


我的最终目标是读取一个文件夹,其中有几个期刊页面,其中需要通过屏蔽第一列然后另一列来保存而不影响输入图像的尺寸,并且 mask 部分应该是白色的。
以下是附加的输入图像...

Input_Image1

Input_Image2

和输出应该是这样的......

Output_Image1

Output_Image2

最佳答案

你不需要面具来绘制矩形。您可以直接在图像上绘制它。

您也可以使用image.copy()与其他列创建第二个图像

顺便说一句:如果 795在宽度的中间,那么你可以使用 image.shape得到它的(height,width)并使用 width//2而不是 795因此它将适用于具有不同宽度的图像。但是如果 795不是理想的中间然后使用half_width = 795

import cv2

image_1 = cv2.imread('image.jpg')
image_2 = image_1.copy()

height, width, depth = image_1.shape # it gives `height,width`, not `width,height`
half_width = width//2
#half_width = 795

cv2.rectangle(img=image_1, pt1=(0, 0), pt2=(half_width, height), color=(255, 255, 255), thickness=-1)
cv2.rectangle(img=image_2, pt1=(half_width, 0), pt2=(width, height), color=(255, 255, 255), thickness=-1)

cv2.imwrite("image_1.jpg", image_1)
cv2.imwrite("image_2.jpg", image_2)

cv2.imshow("image 1", image_1)
cv2.imshow("image 2", image_2)

cv2.waitKey(0)
cv2.destroyAllWindows()

关于python - 我想水平屏蔽多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58449414/

相关文章:

python - sift算法颜色不变吗?

python - 并行处理时 Python 和 Fabric 1.x 出现错误

python - 使用GPRS扩展板和AT命令将arduino传感器数据发送到服务器

python - 我可以遍历python中的逻辑运算符吗?

python - 扩大 Pandas 数据框,类似于 Pivot 或 Stack/Unstack

c++ - Opencv - 如何获取图像中存在的垂直线数(线数)

python - 带 gevent 的 redis-py

python - numpy 数组转换规则不是 'safe'

python - 在特定时间间隔内找到列表最大值的最快方法

c++ - 使用网络摄像头时 ffmpeg 启动太慢(与使用 OpenCV 相同)