我正在尝试在参考图像上堆叠多个mask(6)。
def masking(file_list):
#read images
for file in glob.glob(path):
img = cv2.imread(file)
#edge detection
canny = auto_canny(img)
#Dilation(Morphological function to increase edge width)
img_dilate = cv2.dilate(canny, (3,3), iterations = 1)
#Gaussian Blur to blur the edges to remove noise
very_blurred = ndimage.gaussian_filter(img_dilate, sigma=5)
#Apply Threshold to generate mask
ret, thresh1 = cv2.threshold(very_blurred, 100, 255, cv2.THRESH_BINARY) #cv2.THRESH_TOZERO
#Retrieve regions from original images
res = cv2.bitwise_and(img, img, mask = thresh1)
return res
这将执行遮罩并从图像中返回原始区域。
def variance_of_laplacian(file_list):
blurriest = 0
blurr = 0
# compute the Laplacian of the image and then return the focus
# measure, which is simply the variance of the Laplacian
for file in file_list:
image = cv2.imread(file)
# gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
canny = auto_canny(image)
blur_extent = cv2.Laplacian(canny, cv2.CV_64F).var()
if blurr == 0:
blurr = blur_extent
if blur_extent < blurr:
blurr = blur_extent
blurriest = image
# print(bb, blur_extent)
return blurriest
这将返回参考图像,用于将所有蒙版与从图像中剪切出的原始区域堆叠在一起。
现在,我尝试将蒙版添加到基本图像中,但是没有成功。谁能建议如何进行。
added_image = cv2.addWeighted(b,1.0,res,0.7,0)
cv2.imshow('combined', added_image)
cv2.waitKey(0)
编辑:
for i in range(len(image_1)):
b = cv2.addWeighted(b,0.5,image_1[i],0.5,0)
# # new = cv2.add(blurring,b)
added_image = cv2.addWeighted(blurring,0.5,b,0.5,0)
cv2.imshow("final",added_image)
cv2.waitKey(0)
此处image1是包含所有提取区域的列表。模糊是参考图像的副本,上面添加了所有提取的区域。
这是添加所有蒙版并将提取的区域放到参考图像上之后的最终结果。 Final Image after adding masks and putting on the reference image
答案 0 :(得分:0)
如果我理解正确,您想剪切图像的一部分并将其粘贴到另一个图像的顶部吗?如果是这样的话,addWeighted
是不正确的功能,因为它会融合整个图像,而不仅是您想要的区域。
对6张图片中的每张重复此过程。
获取要复制的部分(您已经拥有了):
bitwise_and_result = cv2.bitwise_and(img, img, mask = thresh1)
反转遮罩并将其应用于主图像:
thresh1_inv = cv2.bitwise_not(thresh1)
main_altered = cv2.bitwise_and(main_img, main_img, mask = thresh1_inv)
合并图像:
result = cv2.add(main_altered , bitwise_and_result )
编写代码,以便对下一张图像使用更改后的main_image。例如:
main_img = result
您可以找到示例here
如果要粘贴多个部分,可以重复该过程。将输出作为新的主图像: