I need to fill the white part of the circle of the first image with the second image I have attached, how can I do this in opencv? enter image description here
Asked
Active
Viewed 93 times
-3

chigan_6
- 3
- 2
-
use `cv2.circle(image, center_coordinates, radius, color, thickness)` https://www.geeksforgeeks.org/python-opencv-cv2-circle-method/ – Sembei Norimaki Feb 24 '23 at 09:14
-
Does this answer your question? [draw a circle over image opencv](https://stackoverflow.com/questions/16484796/draw-a-circle-over-image-opencv) – Florent Monin Feb 24 '23 at 09:14
-
I already have a drawn circle, I need to insert another picture into it – chigan_6 Feb 24 '23 at 09:16
-
1Oh I got it now. what you want to generate is not the picture you posted? then why did you post a picture of something you don't want? What you want is to fill the white zone of that picture with another picture? So please change completely your question. What you want is fill the white pixels of an image with a second image. Basically you want to put another face to this dog. It would be much clearer if you explain it this way – Sembei Norimaki Feb 24 '23 at 09:28
1 Answers
2
Here is one way in Python/OpenCV.
- Read each image
- Convert the circle image to grayscale
- Threshold the circle image
- Crop the second image to the size of the first image
- Combine the cropped second image with the first image using the threshold image as the selector of the region to use
- Save the results
Input 1:
Input 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()
Threshold Image:
Result Image:

fmw42
- 46,825
- 10
- 62
- 80