我需要用附加的第二张图片填充第一张图片圆圈的白色部分,如何在opencv中完成此操作?enter image description here
h9vpoimq1#
下面是Python/OpenCV中的一种方法。
输入1:
输入2:
import cv2 import numpy as np # read image 1 img1 = cv2.imread('white_circle.png') h1, w1 = img1.shape[:2] # read image 2 img2 = cv2.imread('bear_heart.jpg') h2, w2 = img2.shape[:2] # convert img1 to grayscale gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) # threshold to binary thresh = cv2.threshold(gray, 254, 255, cv2.THRESH_BINARY)[1] thresh = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR) # crop second image to size of first image img2_crop = img2[0:h1, 0:w1] # combine img1, img2_crop with threshold as mask result = np.where(thresh==255, img2_crop, img1) # save results cv2.imwrite('white_circle_thresh.jpg', thresh) cv2.imwrite('white_circle_bear_heart.jpg', result) cv2.imshow("thresh", thresh) cv2.imshow("result", result) cv2.waitKey(0) cv2.destroyAllWindows()
阈值图像:
结果图像:
1条答案
按热度按时间h9vpoimq1#
下面是Python/OpenCV中的一种方法。
输入1:
输入2:
阈值图像:
结果图像: