[Various image analysis with plotly] Dynamic visualization with plotly [python, image]

Let's analyze the image easily with plotly

dd.gif

environment

python==3.8 plotly==4.10.0 scikit-image==0.17.2 requests==2.24.0 Pillow==7.2.0 matplotlib==3.3.2

Handling of common images

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)

image.png

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

Image processing using skimage and plotly

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()

dd.gif

You can also handle images read with pillow

fig = px.imshow(gray_img, color_continuous_scale='gray')
fig.show()

image.png

Create contour lines in the image

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()

image.png

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()

image.png

Create a color channel histogram from plotly

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)

image.png

image.png

image.png

image.png

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()

image.png

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()

image.png

It looks like this when it is divided neatly

image.png

Confirmation for black and white is as follows

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()

image.png

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()

image.png

By the way, how to display the image in the figure

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()

image.png

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()

image.png

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.

image.png

that's all

Anything fun theory if you can move it for the time being

Recommended Posts

[Various image analysis with plotly] Dynamic visualization with plotly [python, image]
[Write to map with plotly] Dynamic visualization with plotly [python]
[Time series with plotly] Dynamic visualization with plotly [python, stock price]
Data analysis starting with python (data visualization 1)
Data analysis starting with python (data visualization 2)
[Talking about the drawing structure of plotly] Dynamic visualization with plotly [python]
Data analysis with python 2
Medical image analysis with Python 1 (Read MRI image with SimpleITK)
Various Python visualization tools
Voice analysis with python
Image processing with Python
Voice analysis with python
Dynamic analysis with Valgrind
Data analysis with Python
Logistics visualization with Python
[OpenCV / Python] I tried image analysis of cells with OpenCV
[Python] Morphological analysis with MeCab
Image editing with python OpenCV
Sentiment analysis with Python (word2vec)
Sorting image files with Python (2)
Sorting image files with Python (3)
Image processing with Python (Part 1)
Tweet with image in Python
Sorting image files with Python
Planar skeleton analysis with Python
Manipulate various databases with Python
Japanese morphological analysis with Python
Image processing with Python (Part 3)
Muscle jerk analysis with Python
[Python] Image processing with scikit-image
Principal component analysis hands-on with PyCaret [normalization + visualization (plotly)] memo
Cut out an image with python
[Python] Using OpenCV with Python (Image Filtering)
3D skeleton structure analysis with Python
[Python] Using OpenCV with Python (Image transformation)
Impedance analysis (EIS) with python [impedance.py]
Dynamic proxy with python, ruby, PHP
Text mining with Python ① Morphological analysis
Image processing with Python 100 knocks # 3 Binarization
Let's do image scraping with Python
Easy data visualization with Python seaborn.
Find image similarity with Python + OpenCV
Image processing with Python 100 knocks # 2 Grayscale
Introduction to image analysis opencv python
Logistic regression analysis Self-made with python
Send image with python, save with php
Gradation image generation with Python [1] | np.linspace
Get PowerShell commands from malware dynamic analysis site with BeautifulSoup + Python
Image processing with Python 100 knocks # 4 Binarization of Otsu (discriminant analysis method)
[Capacity indicator, Gantt chart, UI] Plotly dynamic visualization [python, gauge display, Gantt chart]
Basics of binarized image processing with Python
Image processing with Python 100 knock # 10 median filter
Python Application: Data Visualization Part 3: Various Graphs
HTML email with image to send with python
[In-Database Python Analysis Tutorial with SQL Server 2017]
Marketing analysis with Python ① Customer analysis (decyl analysis, RFM analysis)
Two-dimensional saturated-unsaturated osmotic flow analysis with Python
Create a dummy image with Python + PIL.
Image processing with Python 100 knocks # 8 Max pooling
Machine learning with python (2) Simple regression analysis
Introduction to Python Image Inflating Image inflating with ImageDataGenerator