0

I have to warp the image1 and add to image2.

This is images link.

https://drive.google.com/drive/folders/1IitVGsbx72vSYPqeARd5Xlh7kMpGyPO4?usp=sharing

So I have to use this function.

added_image = cv2.addWeighted(img2,1,Affinedst,1,0)

But I can't see the image1 on the added_image. We have to keep the img2's overlay value to 1.

M=cv2.getPerspectiveTransform(src_interest_pts ,Affine_interest_pts)

Affinedst = cv2.warpPerspective(img1,M,(cols,rows))

added_image = cv2.addWeighted(img2,1,Affinedst,1,0)
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • Can you try with `alpha = beta = 0.5`? In general, `beta` should equal `1 - alpha`. See https://docs.opencv.org/3.4/d5/dc4/tutorial_adding_images.html – beaker Jun 30 '23 at 14:39
  • You have to expand your image1 to be the size of both image1 and image2 so that there is room to overlay the new image with the old one. See https://stackoverflow.com/questions/68565531/remove-black-dashed-lines-from-image-stitching and https://stackoverflow.com/questions/66552527/how-to-warp-an-document-image-in-python-correctly/66558064#66558064 – fmw42 Jun 30 '23 at 17:04
  • Thanks for your opinion. But i have tried all of these. – Marina Parshina Jul 02 '23 at 14:42

2 Answers2

0

From what you shared I think you're doing something similar to image stitching where you want to add two images together. In general, there are a few ways to go around that but in a basic stitching, you will have to combine the common parts of the images with the weighted average or completely remove that common region from one of the images. In either case, this could lead to artefacts or a sharp change on the edge where the images meet. So, the method addWeighted() is only really used for blending in which case the beta = 1 - alpha. I believe that if you were to use weights of 1 for both then the result will be similar to the add() method. The effects of it you can read here. Essentially it clips the values. Perhaps if you shared picture of what you get as a result and explain what you want to achieve we can explain better.

Ivalin
  • 45
  • 9
  • Didn`t you check the google drive link? – Marina Parshina Jul 02 '23 at 14:45
  • @MarinaParshina links didn't work at the time. If you want to get a good answer, you should explain what you want to achieve. From what I see now, it appears you want to put `img1` on top of `img2` in the white box space, is that correct? Anyways, please update your question to add all the information we need to help you out – Ivalin Jul 02 '23 at 16:13
  • I have solve this project. Please check my code. – Marina Parshina Jul 04 '23 at 14:30
0
    import cv2
    import numpy as np
    img2 = cv2.imread("img2.png")
    deformation_image = cv2.imread("img1.png")


    #img1.png shape(550,350)

    rows, cols, ch = img2.shape

    src_interest_pts = np.float32([[0,0],[550,0],[550,350],[0,350]])
    Affine_interest_pts = np.float32([[487, 149], [852, 160], [901, 437], [619, 476]])
    M=cv2.getPerspectiveTransform(src_interest_pts ,Affine_interest_pts)
    Affinedst = cv2.warpPerspective(deformation_image,M,(cols,rows))
    cv2.imwrite("text.png",Affinedst)
    file_name = "text.png"
    src = cv2.imread(file_name, 1)
    
    tmp = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
    
    _, alpha = cv2.threshold(tmp, 0, 255, cv2.THRESH_BINARY)
    
    b, g, r = cv2.split(src)
    
    rgba = [b, g, r, alpha]
    
    dst = cv2.merge(rgba, 4)
    
    cv2.imwrite("gfg_white.png", dst)
    bimg = img2

    hh, ww = bimg.shape[:2]

    fimg = cv2.imread('gfg_white.png', cv2.IMREAD_UNCHANGED)

    fimg_small = fimg
    ht, wd = fimg_small.shape[:2]

    fimg_new = np.full((hh,ww,4), (0,0,0,0), dtype=np.uint8)

    fimg_new[0:ht, 0:wd] = fimg_small

    alpha = fimg_new[:,:,3]
    alpha = cv2.merge([alpha,alpha,alpha])

    base = fimg_new[:,:,0:3]

    added_image = np.where(alpha==(0,0,0), bimg, base)
    cv2.imshow("result", added_image)
    cv2.waitkey(0)
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 05 '23 at 11:30