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)