python==3.8 plotly==4.10.0 scikit-image==0.17.2 requests==2.24.0 Pillow==7.2.0 matplotlib==3.3.2
I will use pillow first
import matplotlib.pyplot as plt
from PIL import Image
import requests
import io
url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Crab_Nebula.jpg/240px-Crab_Nebula.jpg'
img = Image.open(io.BytesIO(requests.get(url).content))
plt.figure(figsize=(5,5))
plt.subplot(111)
plt.imshow(img)
I was able to display the image with the usual matplot Let's make it black and white
gray_img = img.convert('L')
plt.figure(figsize=(5,5))
plt.subplot(111)
plt.imshow(gray_img)
image object can be black and white using convert
The nice thing about displaying images with plotly is that you can zoom in, etc.
import plotly.express as px
from skimage import io
img_sk = io.imread('https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Crab_Nebula.jpg/240px-Crab_Nebula.jpg')
fig = px.imshow(img_sk)
fig.show()
You can also handle images read with pillow
fig = px.imshow(gray_img, color_continuous_scale='gray')
fig.show()
You can add contour lines to the brightness of the image
Contour plot to color the value of Z Monitor the values from start to end Specify the interval of contour lines with size By making end and size the same, you can extract the part that has the specified value. In other words, it can be used for contour extraction, etc.
import plotly.graph_objects as go
fig = px.imshow(gray_img, color_continuous_scale='gray')
fig.add_trace(go.Contour(z=gray_img, showscale=True,
contours=dict(start=0, end=30, size=30,coloring='lines'),line_width=1))
fig.show()
import plotly.express as px
import plotly.graph_objects as go
from skimage import data
img_camera = data.camera()
fig = px.imshow(img_camera, color_continuous_scale='gray')
fig.add_trace(go.Contour(z=img_camera, showscale=False,
contours=dict(start=0, end=70, size=70, coloring='lines'),
line_width=2))
fig.show()
from plotly.subplots import make_subplots
from skimage import data
img_sk.shape
#(240, 240, 3)
plt.imshow(img_sk)
img_r = img_sk.copy()
img_r[:, :, 1] = 0
img_r[:, :, 2] = 0
plt.imshow(img_r)
img_g = img_sk.copy()
img_g[:, :, 0] = 0
img_g[:, :, 2] = 0
plt.imshow(img_g)
img_b = img_sk.copy()
img_b[:, :, 0] = 0
img_b[:, :, 1] = 0
plt.imshow(img_b)
Non-zero to hist
ff=go.Figure()
ff.add_trace(go.Histogram(x=img_r.flatten()[img_r.flatten()!=0],marker_color='red',name='red'))
ff.add_trace(go.Histogram(x=img_g.flatten()[img_g.flatten()!=0],marker_color='green',name='green'))
ff.add_trace(go.Histogram(x=img_b.flatten()[img_b.flatten()!=0],marker_color='blue',name='blue'))
ff.update_layout(barmode='overlay')
ff.update_traces(opacity=0.3)
ff.update_layout(height=400)
ff.show()
When expanding, it seems that 10 or less is the main, so check the distribution of 10 or more
ff=go.Figure()
ff.add_trace(go.Histogram(x=img_r.flatten()[img_r.flatten()>10],marker_color='red',name='red'))
ff.add_trace(go.Histogram(x=img_g.flatten()[img_g.flatten()>10],marker_color='green',name='green'))
ff.add_trace(go.Histogram(x=img_b.flatten()[img_b.flatten()>10],marker_color='blue',name='blue'))
ff.update_layout(barmode='overlay')
ff.update_traces(opacity=0.3)
ff.update_layout(height=400)
ff.show()
It looks like this when it is divided neatly
from skimage.color import rgb2gray
gray_img = rgb2gray(img_sk)
ff=go.Figure()
ff.add_trace(go.Histogram(x=gray_img.flatten()[gray_img.flatten()!=0],marker_color='pink',name='gray'))
ff.update_layout(barmode='overlay')
ff.update_traces(opacity=0.8)
ff.update_layout(height=400)
ff.show()
ff=go.Figure()
ff.add_trace(go.Histogram(x=gray_img.flatten()[gray_img.flatten()>0.1],marker_color='pink',name='gray'))
ff.update_layout(barmode='overlay')
ff.update_traces(opacity=0.8)
ff.update_layout(height=400)
ff.show()
Appeal to the entire surface of the graph
import plotly.express as px
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
fig.add_layout_image(
dict(
source="https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Crab_Nebula.jpg/240px-Crab_Nebula.jpg ",
xref="x",
yref="y",
x=0,
y=16,
sizex=4,
sizey=15,
sizing="stretch")
)
fig.update_xaxes(title_text="picture No")
fig.update_yaxes(title_text="view par day", hoverformat=".3f")
fig.show()
Gently appeal on the back (below)
import plotly.express as px
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
fig.add_layout_image(
dict(
source="https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Crab_Nebula.jpg/240px-Crab_Nebula.jpg ",
xref="x",
yref="y",
x=0,
y=16,
sizex=4,
sizey=15,
sizing="stretch",
opacity=0.5,
layer="below")
)
fig.update_xaxes(title_text="picture No")
fig.update_yaxes(title_text="view par day", hoverformat=".3f")
fig.show()
Make it smaller and add a stick or put text
import plotly.express as px
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
fig.add_layout_image(
dict(
source="https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Crab_Nebula.jpg/240px-Crab_Nebula.jpg ",
xref="x",
yref="y",
x=1.68,
y=16,
sizex=3,
sizey=3)
)
fig.update_layout(
annotations=[
dict(
x=0.5,
y=0.8,
xref="paper",
yref="paper",
showarrow=True,
arrowhead=0,
opacity=0.5,
ax=190,
ay=100,
)#,
# dict(x=,y=,xref="paper",yref="paper",showarrow=True,arrowhead=0,opacity=0.5,ax=,ay=,)
]
)
fig.update_xaxes(title_text="picture No")
fig.update_yaxes(title_text="view par day", hoverformat=".3f")
fig.show()
The layout image of the image conforms to the position of the plot The x, y axis of the plot corresponds to the upper left position x, y of the image
The bar is 0,0 at the bottom left of the image Center is 0.5,0.5 ax and ay represent the length of the bar, and extend from the specified positions x and y to the vector-combined position of the bars extending in the x-axis direction and the y-axis direction.
Anything fun theory if you can move it for the time being
Recommended Posts