0

I am using cv2 toolbox and want to mix / weight two pictures, where one picture is a mask of an apparel piece appearing in the first picture. However the weighted picture appears way too bright, independent of the parameter values. I switched alpha, beta, gamma several times without any significant difference. Does anyone know why all colours seem to be supressed and how I can actually mix the two pictures, so that the masked object is visibly highlighted in the original picture? You can see all three pictures here. I use the following code in colab:

import pdb
for i in range(len(md)):
    imag = cv2.imread("chloe/"+md.ImageId[i]+".jpg")
    #pdb. set_trace()
    imag = _scale_image(imag, 1024)
    mask = cv2.resize(np.float32(md.Maskdata[i]), (imag.shape[1],imag.shape[0]), interpolation=cv2.INTER_NEAREST)
    where = np.where(mask!=0)
    y1,y2,x1,x2 = 0,0,0,0
    if len(where[0]) > 0 and len(where[1]) > 0:
        y1,y2,x1,x2 = min(where[0]),max(where[0]),min(where[1]),max(where[1])
        if y2>y1+80 and x2>x1+80 and np.sum(mask)/255 > 1000:
            print("image id=",md.ImageId[i])
            plt.subplot(1,3,1)
            plt.imshow(imag)
            plt.subplot(1,3,2)
            plt.imshow(mask)
            plt.subplot(1,3,3)
            mask2 = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
            dst = cv2.addWeighted(np.float32(imag), 0.5, mask2, 0.5, 0)
            plt.imshow(dst)
            plt.show()

with the function:

import math
def _scale_image(img, long_size):
    if img.shape[0] < img.shape[1]:
        scale = img.shape[1] / long_size
        size = (long_size, math.floor(img.shape[0] / scale))
    else:
        scale = img.shape[0] / long_size
        size = (math.floor(img.shape[1] / scale), long_size)
    return cv2.resize(img, size, interpolation=cv2.INTER_NEAREST)
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • This is not a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). We should be able to cut and paste your code into and editor and run it as is. Further, if your question involves image processing, you need to supply the **original** image (instead of a janky screenshot). For what it's worth, it's not obvious to me why your `addWeighted` call is not working. It seems like 0.5 weights should work. – bfris Jan 05 '22 at 19:41
  • I just need a guess why the two photos in the appendix, where the second one is the result of using cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) on the first one, lead to the blank image when using cv2.cvtcolor – Entropie_13 Jan 05 '22 at 21:13

0 Answers0