"""Overlay `img_overlay` onto `img` at (x, y) and blend using optional `alpha_mask`. def overlay_image_alpha(img, img_overlay, x, y, alpha_mask=None): I reworked concept to allow for optional alpha masks and allow any x or y, including values outside of the bounds of the image. Result = (alpha_front + alpha_back) / (1 + alpha_front*alpha_back) * 255 Result = alpha_front * front_cropped + (1-alpha_front) * back_cropped import cv2īack = cv2.cvtColor(back, cv2.COLOR_BGR2BGRA)įront = cv2.cvtColor(front, cv2.COLOR_BGR2BGRA)įront_cropped = frontĪlpha_front = front_cropped / 255 Where there is no overlap with front, the alpha value from back is taken. The output alpha channel is given by (u+v)/(1+uv) where u,v are the alpha channels of the front and back image and -1 <= u,v <= 1. The output image has the same size as back, but always 4 channels. It works with both 3 and 4-channel images and deals with the alpha channel. img_overlay has the same number of channels as img.Ī simple function that blits an image front onto an image back and returns the result.img should not contain an alpha channel.If you encounter errors or unusual outputs, please ensure: omarray(img_result).save("img_result.jpg") Overlay_image_alpha(img_result, img_overlay, x, y, alpha_mask) Img_overlay_rgba = np.array(Image.open("img_small.png"))Īlpha_mask = img_overlay_rgba / 255.0 Img = np.array(Image.open("img_large.jpg")) Img_crop = alpha * img_overlay_crop + alpha_inv * img_crop Img_overlay_crop = img_overlayĪlpha = alpha_mask # Blend overlay within the determined ranges X1o, x2o = max(0, -x), min(img_overlay.shape, img.shape - x) Y1o, y2o = max(0, -y), min(img_overlay.shape, img.shape - y) X1, x2 = max(0, x), min(img.shape, x + img_overlay.shape) Y1, y2 = max(0, y), min(img.shape, y + img_overlay.shape) `alpha_mask` must have same HxW as `img_overlay` and values in range. """Overlay `img_overlay` onto `img` at (x, y) and blend using `alpha_mask`. def overlay_image_alpha(img, img_overlay, x, y, alpha_mask): This works well for any position argument (including negative positions). Using idea, I wrote up a function to handle overlays. Print "size of image: ", img.shape # print size of image Small = cv2.imread("average_face.png",cv2.CV_LOAD_IMAGE_COLOR)Ĭv2.namedWindow('Display image') # create window for displayĬv2.imshow('Display image', small) # Show image in the window Img = cv2.imread(sys.argv,cv2.CV_LOAD_IMAGE_COLOR) # Read image fileĬascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml") If len(sys.argv) != 2: # Check for error in usage syntax Rects = tectMultiScale(img, scaleFactor=1.1, minNeighbors=3, minSize=(10, 10), flags = cv.CV_HAAR_SCALE_IMAGE)Ĭv2.rectangle(img, (x1, y1), (x2, y2), color, 2) I have researched ROI and addWeight(needs the images to be the same size) but I haven't found a way to do this in python. However, I am stuck on trying to insert the new face into the original, larger image. Hi I am creating a program that replaces a face in a image with someone else's face.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |