英文:
Select segmentation by mouse and change the color in Cornerstone.js
问题
我使用cornerstoneTools与Cornerstone.js [DICOM]视图。我使用Cornerstone.js分割工具绘制多个分割,并从RLE加载分割。
如何通过鼠标在DICOM查看器上点击来选择现有分割?
英文:
I’m using cornerstoneTools with the Cornerstone.js DICOM view. I draw multiple segments by the Cornerstone.js segment tool and load the segments from RLE.
How can I select an existing segment by clicking it by mouse on the DICOM viewer?
答案1
得分: 1
在Cornerstone.js中无法直接设置分割颜色。但是,您可以通过在colorLutTables
中切换所需的颜色来设置Cornerstone.js中的颜色。
要执行此操作,您需要执行三个步骤:
1)使用您的颜色列表设置colorLutTables
,例如:
const COLORLIST = [[255,0,0,255], [255,255,0,255],[0,0,255,255],[255,255,255,255]] // 四种颜色:红色、黄色、蓝色和白色
// 在初始化cornerstone工具时设置colorLut列表
const { configuration, state } = cornerstoneTools.getModule('segmentation');
state.colorLutTables[0] = COLORLIST
2)选择分割。在Cornerstone.js中选择分割没有直接的方法。为此,您必须使用Cornerstone.js工具事件监听器。您可以通过将此事件附加到启用Cornerstone.js的元素上使用cornerstonetoolsmouseclick
事件。
element.addEventListener("cornerstonetoolsmouseclick", (e) => {
const {getters: {segmentOfActiveLabelmapAtEvent}, setters: {deleteSegment}} = cornerstoneTools.getModule('segmentation');
});
const isSegment = segmentOfActiveLabelmapAtEvent(e); // 如果您的鼠标点击了任何分割,这将返回一个对象,否则返回undefined
if(isSegment !== undefined) {
// 在这里,您可以使用所选段的索引切换colorLut索引,例如,您可以替换colorLUT表中的白色索引
}
3)在colorLUT中切换索引,从任何颜色到白色
/*
* from: 所需颜色的索引
* to: 所选分割的索引
* colorLUT: colorLUTTable数组
* element: 活动元素
*/
const switchColorLUT = (from, to, colorLUT, element) => {
const updatedLUT = [...colorLUT];
[updatedLUT[from], updatedLUT[to]] = [updatedLUT[to], updatedLUT[from]];
const {state, getters, setters} = cornerstoneTools.getModule('segmentation');
state.colorLutTables[0] = updatedLUT
setters.activeSegmentIndex(element, to)
setters.activeLabelmapIndex(element, 0);
cornerstone.updateImage(element)
}
您可以使用此函数更新colorLUT表:
// 在`cornerstonetoolsmouseclick`事件中
switchColorLUT(COLORLIST.length -1, isSegment.segmentIndex, COLORLIST, element);
这将更新所选分割为白色。
英文:
It’s not possible to set the segmentation colour directly in Cornerstone.js. However, you can set the color in Cornerstone.js by switching your desired color from colorLutTables
.
To do this, you have to do three steps:
-
Set
colorLutTables
with your list of colors, for example.const COLORLIST = [[255,0,0,255], [255,255,0,255],[0,0,255,255],[255,255,255,255]] // four colors red, yellow, blue and white // Set colorLut list when you initialise cornerstone tools const { configuration, state } = cornerstoneTools.getModule('segmentation'); state.colorLutTables[0] = COLORLIST
-
Selecting the segment. This one. Also, there isn't any straight way to select a segment in Cornerstone.js. To do this, you have to use the Cornerstone.js tools event listener. You can use the
cornerstonetoolsmouseclick
event by attach this event to your Cornerstone.js-enabled element.element.addEventListener("cornerstonetoolsmouseclick", (e) => { const {getters: {segmentOfActiveLabelmapAtEvent}, setters: {deleteSegment}} = cornerstoneTools.getModule('segmentation'); }); const isSegment = segmentOfActiveLabelmapAtEvent(e); // If your mouse clicked any segments this will return an object otherwise undefined if(isSegment !== undefined) { // Here you can switch the colorLut index with your selected segment index, for example you can replace white color's index in colorLUT table }
-
Switching indexes in colorLUT, from any color to white
/* * from: Desired colors index * to: selectedSegment index * colorLUT: colorLUTTable array * element: Active element */ const switchColorLUT = (from, to, colorLUT, element) => { const updatedLUT = [...colorLUT]; [updatedLUT[from], updatedLUT[to]] = [updatedLUT[to], updatedLUT[from]]; const {state, getters, setters} = cornerstoneTools.getModule('segmentation'); state.colorLutTables[0] = updatedLUT setters.activeSegmentIndex(element, to) setters.activeLabelmapIndex(element, 0); cornerstone.updateImage(element) }
You can use this function to update the colorLUT table:
// Inside `cornerstonetoolsmouseclick` event switchColorLUT(COLORLIST.length -1, isSegment.segmentIndex, COLORLIST, element);
This will update the selected segment to a white color.
答案2
得分: -3
尝试启用CornerstoneJS中的交互以处理鼠标事件。
cornerstoneTools.init({
mouseEnabled: true
});
此外,您可以附加DICOM中的事件监听器,例如cornerstoneTools.mouseClick
,以检测鼠标单击。以下是可能会帮助您的代码片段。
const element = document.getElementById('dicomViewer');
element.addEventListener('cornerstoneToolsMouseClick', handleMouseClick);
function handleMouseClick(event) {
const eventData = event.detail;
if (eventData && eventData.toolType === 'segmentation') {
const segmentData = eventData.segmentData;
segmentData.color = 'red';
cornerstone.updateImage(element);
}
}
在此函数中,您可以访问分割数据并修改其属性,如颜色。在进行更改后,您需要使用cornerstone.updateImage()
刷新查看器以更新分割的外观。请将dicomViewer
替换为您的DICOM查看器元素的实际ID,放入getElementById
函数中。
英文:
try enabling the interaction in CornerstoneJS to handle mouse events.
cornerstoneTools.init({
mouseEnabled: true
});
furthermore you can attach event listner in DICOM such as cornerstoneTools.mouseClick
to detect the mouse click. here is a code snippet which might help you
const element = document.getElementById('dicomViewer');
element.addEventListener('cornerstoneToolsMouseClick', handleMouseClick);
function handleMouseClick(event) {
const eventData = event.detail;
if (eventData && eventData.toolType === 'segmentation') {
const segmentData = eventData.segmentData;
segmentData.color = 'red';
cornerstone.updateImage(element);
}
}
Inside this function you can access the segment data and modify its properties, such as the color. After making the changes you need to refresh the viewer to update the segment's appearance using cornerstone.updateImage()
.
replace dicomViewer
with the actual ID of your DICOM viewer element in the getElementById
function.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论