简体   繁体   English

有没有办法以交互方式 select 感兴趣区域 (ROI),然后在 python 上返回其 position

[英]Is there a way to select a region of interest (ROI) interactively then return its position on python

I have a CT image array where I want to be able to select the region of interest on the plot itself and then return the position of the bounding-box rectangle/square, basically the start and the end of the rectangle/ square.我有一个 CT 图像阵列,我希望能够 select 感兴趣的区域 plot 本身,然后返回边界框矩形/正方形的 position,基本上是矩形/正方形的开始和结束。 Then I will crop the data py position: clipw = newdata[:,y1:y2,x1:x2].然后我将裁剪数据 py position:clipw = newdata[:,y1:y2,x1:x2]。

I tried to use dash app and as shown here: https://dash.plotly.com/annotations it returns the charactaristics of the shapes by the callback functions which is useful but I have to copy and paste the positions myself.我尝试使用破折号应用程序,如下所示: https://dash.plotly.com/annotations它通过回调函数返回形状的特征,这很有用,但我必须自己复制和粘贴位置。 I cannot find a way to make it automatically pass the poition list to the next step (clipw).我找不到让它自动将位置列表传递到下一步 (clipw) 的方法。 I am using Jupyter notebook.我正在使用 Jupyter 笔记本。

If you intend to run your Dash app inside a Jupyter notebook, you'll probably want to use JupyterDash .如果您打算在 Jupyter notebook 中运行 Dash 应用程序,您可能需要使用JupyterDash To select the coordinates from the region of interest, there's an example further down on documentation page you linked that shows all of the information about the drawn shape including coordinates.对于 select 来自感兴趣区域的坐标,在您链接的文档页面的下方有一个示例,它显示了有关绘制形状的所有信息,包括坐标。

Below is an example of a JupyterDash app that can be run entirely inside your Jupyter notebook, and it selects the closest indices of your image by rounding the floating point x,y coordinates to integers.下面是一个JupyterDash应用程序的示例,它可以完全在您的 Jupyter notebook 中运行,它通过将浮点 x、y 坐标四舍五入为整数来选择最接近图像的索引。 This relies on the fact that the shape of the image is (900,1600,3) and that plotly will be default set the x- and y-axes at the same scale.这依赖于图像的形状是(900,1600,3)并且 plotly 将默认将 x 轴和 y 轴设置为相同比例的事实。

Currently the app doesn't have much functionality except returning the closest x,y indices for the corners of your selected rectangle, but hopefully you can modify the rest of the function to fit your cropping use case.目前,该应用程序除了为所选矩形的角返回最接近的 x、y 索引外没有太多功能,但希望您可以修改 function 的 rest 以适合您的裁剪用例。

import plotly.express as px
from jupyter_dash import JupyterDash
from dash import Dash, dcc, html, Input, Output, no_update
from skimage import io 
import json

img = io.imread('https://raw.githubusercontent.com/michaelbabyn/plot_data/master/bridge.jpg')
fig = px.imshow(img)
fig.update_layout(dragmode="drawrect")

app = JupyterDash(__name__)
app.layout = html.Div(
    [
        html.H3("Drag and draw rectangle annotations"),
        dcc.Graph(id="graph-picture", figure=fig),
        dcc.Markdown("Characteristics of shapes"),
        html.Pre(id="annotations-data"),
    ]
)

@app.callback(
    Output("annotations-data", "children"),
    Input("graph-picture", "relayoutData"),
    prevent_initial_call=True,
)
def on_new_annotation(relayout_data):
    if "shapes" in relayout_data:
        shape_dict = relayout_data["shapes"][0]
        x0, x1, y0, y1 = shape_dict["x0"], shape_dict["x1"], shape_dict["y0"], shape_dict["y1"]
        [x0, x1, y0, y1] = [round(num) for num in [x0, x1, y0, y1]]
        ## crop data
        return f"x0={x0}, x1={x1}, y0={y0}, y0={y1}"
    else:
        return no_update

app.run_server(mode='inline')

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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