我想水平屏蔽多个图像
I want to mask multiple Image horizontally
我有几个 Journal 页面图像,其中有两列我想在不更改的情况下将一列屏蔽为白色 dimension.which 意味着即使只有一列,输出图像也应与输入图像具有相同的尺寸。
我可以遮罩图像,但遮罩部分变黑了,我想要白色。
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)
我最后的 objective 是读取一个文件夹,其中有几页 Journal 页面,其中需要通过先屏蔽一列然后屏蔽另一列来保存,而不影响输入图像的尺寸和屏蔽部分应该是白色的.
下面是附加的输入图像...
输出应该是这样的....
绘制矩形不需要遮罩。你可以直接在图像上绘制它。
您还可以使用 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()
我有几个 Journal 页面图像,其中有两列我想在不更改的情况下将一列屏蔽为白色 dimension.which 意味着即使只有一列,输出图像也应与输入图像具有相同的尺寸。
我可以遮罩图像,但遮罩部分变黑了,我想要白色。
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)
我最后的 objective 是读取一个文件夹,其中有几页 Journal 页面,其中需要通过先屏蔽一列然后屏蔽另一列来保存,而不影响输入图像的尺寸和屏蔽部分应该是白色的. 下面是附加的输入图像...
输出应该是这样的....
绘制矩形不需要遮罩。你可以直接在图像上绘制它。
您还可以使用 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()