简体   繁体   中英

How to overlay two image figures plotly (python)

I'm trying to overlay an mask on an image in plotly. I'm trying to do this as you would with two scatter plots for instance, but the error says that the data property is wrong when adding the figure to the trace.

rgb_pad_removed, thermal_img = get_thermal_and_rgb(img_path)


img, mask = get_masked_image(rgb_pad_removed)

thermal_img_resized = cv2.resize(thermal_img, dsize=(mask.shape[1], mask.shape[0]), interpolation=cv2.INTER_CUBIC)


from plotly.subplots import make_subplots
f1 = px.imshow(rgb_pad_removed)
f2 = px.imshow(mask)

fig = make_subplots()
fig.add_trace(f1)
fig.add_trace(f2)

fig.show()

With the following error:

ValueError: 
    Invalid element(s) received for the 'data' property of 
        Invalid elements include: [Figure({
        ect...

Am I using the correct method or is there a better one?

Here the problem is, you are adding the figure as a trace. add_trace supports only a trace object. Trace is basically Scatter or Bar. You have added a figure as a trace. That is the error here.

To mask an image, this is what you can do. Get your image and add the mask from opencv and show the image. I have added an example. Please check it.

import numpy as np
import plotly.express as px

img_rgb = np.array([[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
                    [[0, 255, 0], [0, 0, 255], [255, 0, 0]]
                    ], dtype=np.uint8)

mask = np.array([[0, 1, 0],
                 [0, 1, 0]], dtype=np.uint8)

result = cv2.bitwise_and(img_rgb, img_rgb2, mask=mask)
f1 = px.imshow(result)

f1.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM