@duyafeng/pseudocolor-viewer 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/PseudoColorViewer.vue +532 -63
- package/package.json +4 -4
- package/positionPresets.js +39 -17
- package/lib/canvasRectangle.js +0 -404
- package/lib/colorMaps.js +0 -113
- package/lib/imageDisplayManager.js +0 -481
- package/lib/imageModule.js +0 -88
- package/lib/index.js +0 -17
- package/lib/medicalColorProcessor.js +0 -161
- package/lib/windowLevelController.js +0 -215
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
// medicalColorProcessor.js
|
|
2
|
-
import { COLOR_MAPS } from './colorMaps';
|
|
3
|
-
/**
|
|
4
|
-
* 医学图像伪彩色处理器类
|
|
5
|
-
* 用于将灰度图像转换为伪彩色图像
|
|
6
|
-
*/
|
|
7
|
-
class MedicalColorProcessor {
|
|
8
|
-
/**
|
|
9
|
-
* 构造函数
|
|
10
|
-
* @param {number} colorMapIndex - 颜色映射表索引,默认为0
|
|
11
|
-
*/
|
|
12
|
-
constructor(colorMapIndex = 0) {
|
|
13
|
-
this.allColorMaps = COLOR_MAPS.map(map => map.colors);
|
|
14
|
-
this.currentColorMapIndex = colorMapIndex;
|
|
15
|
-
this.colorMap = this.allColorMaps[colorMapIndex];
|
|
16
|
-
this.lut = new Array(256);
|
|
17
|
-
this.updateLUT(128, 90); // 初始化默认窗宽窗位
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 设置颜色映射表
|
|
22
|
-
* @param {number} index - 颜色映射表索引
|
|
23
|
-
* @returns {boolean} 设置是否成功
|
|
24
|
-
*/
|
|
25
|
-
setColorMap(index) {
|
|
26
|
-
if (index >= 0 && index < this.allColorMaps.length) {
|
|
27
|
-
this.currentColorMapIndex = index;
|
|
28
|
-
this.colorMap = this.allColorMaps[index];
|
|
29
|
-
return true;
|
|
30
|
-
}
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* 更新查找表(LUT)
|
|
36
|
-
* @param {number} winWidth - 窗宽
|
|
37
|
-
* @param {number} winLevel - 窗位
|
|
38
|
-
*/
|
|
39
|
-
updateLUT(winWidth, winLevel) {
|
|
40
|
-
const windowMin = Math.max(0, winLevel - winWidth / 2);
|
|
41
|
-
const windowMax = Math.min(255, winLevel + winWidth / 2);
|
|
42
|
-
const range = windowMax - windowMin || 1;
|
|
43
|
-
|
|
44
|
-
for (let i = 0; i < 256; i++) {
|
|
45
|
-
let normalized = (i - windowMin) / range;
|
|
46
|
-
normalized = Math.max(0, Math.min(1, normalized));
|
|
47
|
-
|
|
48
|
-
const position = normalized * (this.colorMap.length - 1);
|
|
49
|
-
const index1 = Math.floor(position);
|
|
50
|
-
const index2 = Math.ceil(position);
|
|
51
|
-
const ratio = position - index1;
|
|
52
|
-
|
|
53
|
-
const color1 = this.colorMap[index1] || [0, 0, 0];
|
|
54
|
-
const color2 = this.colorMap[index2] || color1;
|
|
55
|
-
this.lut[i] = [
|
|
56
|
-
Math.round(color1[0] + (color2[0] - color1[0]) * ratio),
|
|
57
|
-
Math.round(color1[1] + (color2[1] - color1[1]) * ratio),
|
|
58
|
-
Math.round(color1[2] + (color2[2] - color1[2]) * ratio)
|
|
59
|
-
];
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* 应用伪彩色映射到图像数据
|
|
65
|
-
* @param {ImageData} imageData - 图像数据
|
|
66
|
-
* @returns {ImageData} 处理后的图像数据
|
|
67
|
-
*/
|
|
68
|
-
applyTemperatureColorMap(imageData, grayWL, grayWW, grayMin, grayMax, tempMin, tempMax) {
|
|
69
|
-
const pixelData = imageData.data;
|
|
70
|
-
|
|
71
|
-
// 是否使用灰度阈值模式
|
|
72
|
-
const useGrayThreshold = (grayWL !== undefined && grayWW !== undefined && grayMax !== undefined);
|
|
73
|
-
|
|
74
|
-
// 计算图像窗口起始位置
|
|
75
|
-
let _imageWndStart = null;
|
|
76
|
-
if (useGrayThreshold) {
|
|
77
|
-
const calculated = grayMax - grayWW;
|
|
78
|
-
_imageWndStart = grayWL - grayWW / 4;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
for (let i = 0; i < pixelData.length; i += 4) {
|
|
82
|
-
let gray = pixelData[i]; // 原始灰度值
|
|
83
|
-
|
|
84
|
-
// 灰度阈值判断
|
|
85
|
-
if (useGrayThreshold && _imageWndStart !== null) {
|
|
86
|
-
if (gray < _imageWndStart) {
|
|
87
|
-
// 灰度低于起始阈值
|
|
88
|
-
gray = 0;
|
|
89
|
-
} else if (gray > (_imageWndStart + grayWL)) {
|
|
90
|
-
// 灰度高于(起始阈值 + 灰度窗位)
|
|
91
|
-
gray = 255;
|
|
92
|
-
}
|
|
93
|
-
// 否则保持原灰度值
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// 使用处理后的灰度值查找颜色
|
|
97
|
-
const color = this.lut[gray];
|
|
98
|
-
|
|
99
|
-
pixelData[i] = color[0];
|
|
100
|
-
pixelData[i + 1] = color[1];
|
|
101
|
-
pixelData[i + 2] = color[2];
|
|
102
|
-
pixelData[i + 3] = 255;
|
|
103
|
-
}
|
|
104
|
-
return imageData;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* 红色热区高亮模式(复刻后端 GenerateBitmap_red1)
|
|
109
|
-
* @param {ImageData} imageData - 图像数据
|
|
110
|
-
* @param {number} grayWL - 灰度窗位
|
|
111
|
-
* @param {number} grayWW - 灰度窗宽
|
|
112
|
-
* @param {object} grayStats - 灰度统计(含 avg 平均值)
|
|
113
|
-
* @returns {ImageData}
|
|
114
|
-
*/
|
|
115
|
-
applyRed1ColorMap(imageData, grayWL, grayWW, grayStats) {
|
|
116
|
-
const pixelData = imageData.data;
|
|
117
|
-
const averageGray = parseFloat(grayStats && grayStats.avg) || 0;
|
|
118
|
-
const wndStart = grayWL !== undefined && grayWW !== undefined ? grayWL - grayWW / 4 : null;
|
|
119
|
-
const wndRange = grayWW !== undefined ? grayWW : 128;
|
|
120
|
-
const fSlope = wndRange > 0 ? 255 / wndRange : 0;
|
|
121
|
-
|
|
122
|
-
for (let i = 0; i < pixelData.length; i += 4) {
|
|
123
|
-
const gray = pixelData[i];
|
|
124
|
-
let value;
|
|
125
|
-
|
|
126
|
-
if (wndStart !== null && gray < wndStart) {
|
|
127
|
-
value = 0;
|
|
128
|
-
} else if (wndStart !== null && gray < wndStart + wndRange) {
|
|
129
|
-
value = Math.trunc((gray - wndStart) * fSlope);
|
|
130
|
-
if (value > 255) value = 255;
|
|
131
|
-
} else if (wndStart !== null) {
|
|
132
|
-
value = 255;
|
|
133
|
-
} else {
|
|
134
|
-
value = gray;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if (value === 255) {
|
|
138
|
-
// 最高温区域 → 纯红色
|
|
139
|
-
pixelData[i] = 255;
|
|
140
|
-
pixelData[i + 1] = 0;
|
|
141
|
-
pixelData[i + 2] = 0;
|
|
142
|
-
} else if (gray > averageGray) {
|
|
143
|
-
// 高于平均温度区域 → 后端 ColorsTable[1] 纯灰度表,value%40 循环
|
|
144
|
-
const g = Math.floor((value % 40) / 4) * 4;
|
|
145
|
-
pixelData[i] = g;
|
|
146
|
-
pixelData[i + 1] = g;
|
|
147
|
-
pixelData[i + 2] = g;
|
|
148
|
-
} else {
|
|
149
|
-
// 低于平均温度区域 → 白色
|
|
150
|
-
pixelData[i] = 255;
|
|
151
|
-
pixelData[i + 1] = 255;
|
|
152
|
-
pixelData[i + 2] = 255;
|
|
153
|
-
}
|
|
154
|
-
pixelData[i + 3] = 255;
|
|
155
|
-
}
|
|
156
|
-
return imageData;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// 导出类供外部使用
|
|
161
|
-
export default MedicalColorProcessor;
|
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
// windowLevelController.js
|
|
2
|
-
/**
|
|
3
|
-
* 窗宽窗位控制器
|
|
4
|
-
* 管理图像显示和窗宽窗位调节功能
|
|
5
|
-
*/
|
|
6
|
-
export class WindowLevelController {
|
|
7
|
-
constructor(processor) {
|
|
8
|
-
this.processor = processor;
|
|
9
|
-
|
|
10
|
-
this.originalImageData = null;
|
|
11
|
-
this.windowWidth = 128;
|
|
12
|
-
this.windowLevel = 90;
|
|
13
|
-
this.wlRange = { min: 0, max: 255 };
|
|
14
|
-
|
|
15
|
-
this.grayStats = { min: null, max: null, avg: null };
|
|
16
|
-
this.temperatureStats = { min: null, max: null, avg: null };
|
|
17
|
-
this.currentGrayStats = { min: 0, max: 255 };
|
|
18
|
-
this.grayToTempMapping = { tempAtGray0: null, tempAtGray255: null };
|
|
19
|
-
|
|
20
|
-
this.selectedColorMap = 0;
|
|
21
|
-
this.imageSize = { width: 256, height: 192 };
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 根据温度范围设置窗位(混合模式:窗宽用灰度,窗位用温度)
|
|
26
|
-
* @param {number} minGray - 最小灰度值
|
|
27
|
-
* @param {number} maxGray - 最大灰度值
|
|
28
|
-
*/
|
|
29
|
-
setupWindowLevel({ grayStats, tempStats, wlRange,canvasData }) {
|
|
30
|
-
let { min: minGray, max: maxGray } = grayStats;
|
|
31
|
-
let { min: tempMin, max: tempMax } = tempStats;
|
|
32
|
-
const tempRange = tempMax - tempMin;
|
|
33
|
-
// 计算温度窗位初始值(温度范围的1/2位置,更偏左一些)
|
|
34
|
-
const initialTempWL = tempMin + tempRange * 1 / 3;
|
|
35
|
-
const initialGrayWW = 128;
|
|
36
|
-
canvasData.wwValue = (initialGrayWW / 10).toFixed(1);;
|
|
37
|
-
// 计算窗位最小值:给更多空间,让窗位可以向左移动更多
|
|
38
|
-
// 使用更小的偏移量,让窗位最小值更接近实际最小灰度对应的温度
|
|
39
|
-
const minGrayForWL = minGray - 2; // 进一步减少到2,给更多向左移动的空间
|
|
40
|
-
const tempWLMin = this.calculateTempForGray(minGrayForWL, minGray, maxGray, tempMin, tempMax);
|
|
41
|
-
// 计算窗位最大值:直接使用灰度最大值转成温度(不加窗宽/2)
|
|
42
|
-
const tempWLMax = this.calculateTempForGray(maxGray, minGray, maxGray, tempMin, tempMax);
|
|
43
|
-
// 窗位:使用温度值
|
|
44
|
-
// 最小值:(灰度最小值 - 窗宽/2) 对应的温度
|
|
45
|
-
// 最大值:灰度最大值对应的温度
|
|
46
|
-
wlRange.min = tempWLMin !== null ? tempWLMin.toFixed(2) : tempMin.toFixed(2);
|
|
47
|
-
wlRange.max = tempWLMax !== null ? tempWLMax.toFixed(2) : tempMax.toFixed(2);
|
|
48
|
-
canvasData.wlValue = initialTempWL.toFixed(2);
|
|
49
|
-
// 将初始温度窗位转换为灰度窗位
|
|
50
|
-
const initialGrayWL = this.calculateGrayForTemp(initialTempWL, minGray, maxGray, tempMin, tempMax);
|
|
51
|
-
// 将温度窗位转换为灰度窗位,与灰度窗宽一起更新图像
|
|
52
|
-
if (initialGrayWL !== null) {
|
|
53
|
-
this.processor.updateLUT(initialGrayWW, initialGrayWL);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
updateWindow({ grayWW, tempWL, canvasData }) {
|
|
57
|
-
let { grayStats, tempStats, wlRange, wlValue, imageData } = canvasData
|
|
58
|
-
this.originalImageData = imageData
|
|
59
|
-
this.currentGrayStats = grayStats
|
|
60
|
-
this.temperatureStats = tempStats
|
|
61
|
-
// 根据当前窗宽动态更新窗位最小值:给更多空间,让窗位可以向左移动更多
|
|
62
|
-
const minGrayForWL = grayStats.min; // 使用固定偏移量,与初始化保持一致
|
|
63
|
-
const tempWLMin = this.calculateTempForGray(minGrayForWL, grayStats.min, grayStats.max, tempStats.min, tempStats.max);
|
|
64
|
-
|
|
65
|
-
// 更新窗位滑块的最小值
|
|
66
|
-
const oldMin = parseFloat(wlRange.min);
|
|
67
|
-
const newMin = tempWLMin.toFixed(2);
|
|
68
|
-
|
|
69
|
-
if (tempWLMin !== null && oldMin !== parseFloat(newMin)) {
|
|
70
|
-
wlRange.min = newMin;
|
|
71
|
-
|
|
72
|
-
// 如果当前窗位小于新的最小值,需要调整
|
|
73
|
-
if (tempWL < parseFloat(newMin)) {
|
|
74
|
-
wlValue = newMin;
|
|
75
|
-
// document.getElementById('wlValue').textContent = newMin;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// 将温度窗位转换为灰度窗位
|
|
80
|
-
const currentTempWL = parseFloat(wlValue);
|
|
81
|
-
const grayWL = this.calculateGrayForTemp(
|
|
82
|
-
currentTempWL,
|
|
83
|
-
grayStats.min,
|
|
84
|
-
grayStats.max,
|
|
85
|
-
tempStats.min,
|
|
86
|
-
tempStats.max
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
if (grayWL === null) {
|
|
90
|
-
console.error('无法将温度窗位转换为灰度窗位');
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
// 使用灰度窗宽和转换后的灰度窗位更新 LUT
|
|
96
|
-
this.processor.updateLUT(grayWW, grayWL);
|
|
97
|
-
// this.updateDisplay(canvasData);
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* 鼠标滚轮计算窗位变动量:按照窗宽变动量占窗宽范围的比例来计算窗位变动量
|
|
101
|
-
* 窗位变动量 = (窗位范围) * (step / 窗宽范围)
|
|
102
|
-
* 然后保留两位小数,确保变动量精确到小数点后两位
|
|
103
|
-
* @param {Object} event - 鼠标滚轮事件对象
|
|
104
|
-
*/
|
|
105
|
-
mouseScrollDelta(event, windowWidth,windowLevel,wlRange, wlValue) {
|
|
106
|
-
let delta = Math.sign(event.deltaY);
|
|
107
|
-
let step = 0.11 // event.shiftKey ? 20 : 5;
|
|
108
|
-
const wwMin = parseFloat(windowWidth?.min || 0);
|
|
109
|
-
const wwMax = parseFloat(windowWidth?.max || 255);
|
|
110
|
-
const wlMin = parseFloat(windowLevel?.min || wlRange.min || 0);
|
|
111
|
-
const wlMax = parseFloat(windowLevel?.max || wlRange.max || 100);
|
|
112
|
-
let wwDiff = wwMax - wwMin;
|
|
113
|
-
let wlDiff = wlMax - wlMin;
|
|
114
|
-
let wlStep = wwDiff > 0 ? (wlDiff * step) / wwDiff : step;
|
|
115
|
-
// 将变动量四舍五入到小数点后两位,并确保至少为0.01
|
|
116
|
-
wlStep = Math.max(0.11, Math.round(wlStep * 100) / 100);
|
|
117
|
-
// 计算新的窗位值(注意这里是修改窗位而不是窗宽)
|
|
118
|
-
let newWl = wlValue - delta * wlStep;
|
|
119
|
-
// 将窗位值四舍五入到小数点后两位
|
|
120
|
-
newWl = Math.round(newWl * 100) / 100;
|
|
121
|
-
// 确保在范围内
|
|
122
|
-
newWl = Math.max(wlMin, Math.min(newWl, wlMax));
|
|
123
|
-
return {
|
|
124
|
-
newWl,
|
|
125
|
-
ww: parseInt(windowWidth.value)
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* 计算任意灰度值对应的温度
|
|
130
|
-
* @param {number} gray - 灰度值
|
|
131
|
-
* @param {number} grayMin - 最小灰度值
|
|
132
|
-
* @param {number} grayMax - 最大灰度值
|
|
133
|
-
* @param {number} tempMin - 最小温度值
|
|
134
|
-
* @param {number} tempMax - 最大温度值
|
|
135
|
-
* @returns {number|null} 对应的温度值
|
|
136
|
-
*/
|
|
137
|
-
calculateTempForGray(gray, grayMin, grayMax, tempMin, tempMax) {
|
|
138
|
-
// 检查输入有效性
|
|
139
|
-
if (grayMin === grayMax) {
|
|
140
|
-
return null;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// 使用非线性映射的反向计算,与calculateGrayForTemp保持一致
|
|
144
|
-
const tempRange = tempMax - tempMin;
|
|
145
|
-
const grayRange = grayMax - grayMin;
|
|
146
|
-
|
|
147
|
-
// 归一化灰度到 [0, 1] 范围
|
|
148
|
-
const normalizedGray = (gray - grayMin) / grayRange;
|
|
149
|
-
|
|
150
|
-
// 应用幂函数的反向(平方函数)
|
|
151
|
-
const adjustedGray = normalizedGray * normalizedGray;
|
|
152
|
-
|
|
153
|
-
// 映射回温度范围
|
|
154
|
-
const temp = tempMin + adjustedGray * tempRange;
|
|
155
|
-
return temp;
|
|
156
|
-
}
|
|
157
|
-
// 计算任意温度值对应的灰度(温度→灰度)
|
|
158
|
-
calculateGrayForTemp(temp, grayMin, grayMax, tempMin, tempMax) {
|
|
159
|
-
// 检查输入有效性
|
|
160
|
-
if (tempMin === tempMax) {
|
|
161
|
-
return null;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// 使用非线性映射,让温度变化对应的灰度变化更平缓
|
|
165
|
-
// 使用幂函数,让低温度区域变化更慢,高温度区域变化更快
|
|
166
|
-
const tempRange = tempMax - tempMin;
|
|
167
|
-
const grayRange = grayMax - grayMin;
|
|
168
|
-
|
|
169
|
-
// 归一化温度到 [0, 1] 范围
|
|
170
|
-
const normalizedTemp = (temp - tempMin) / tempRange;
|
|
171
|
-
|
|
172
|
-
// 应用幂函数,让变化更平缓(使用0.5次方,相当于平方根)
|
|
173
|
-
const adjustedTemp = Math.pow(normalizedTemp, 0.5);
|
|
174
|
-
|
|
175
|
-
// 映射回灰度范围
|
|
176
|
-
const gray = grayMin + adjustedTemp * grayRange;
|
|
177
|
-
return gray;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* 灰度-温度线性插值计算
|
|
182
|
-
*/
|
|
183
|
-
calculateGrayToTempMapping() {
|
|
184
|
-
if (this.grayStats.min === null || this.grayStats.max === null ||
|
|
185
|
-
this.temperatureStats.min === null || this.temperatureStats.max === null) {
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
if (this.grayStats.min === this.grayStats.max) {
|
|
190
|
-
console.warn('灰度范围为0,无法进行插值计算');
|
|
191
|
-
this.grayToTempMapping = { tempAtGray0: null, tempAtGray255: null };
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
const tempAtGray0 = this.calculateTempForGray(
|
|
196
|
-
0,
|
|
197
|
-
this.grayStats.min,
|
|
198
|
-
this.grayStats.max,
|
|
199
|
-
this.temperatureStats.min,
|
|
200
|
-
this.temperatureStats.max
|
|
201
|
-
);
|
|
202
|
-
|
|
203
|
-
const tempAtGray255 = this.calculateTempForGray(
|
|
204
|
-
255,
|
|
205
|
-
this.grayStats.min,
|
|
206
|
-
this.grayStats.max,
|
|
207
|
-
this.temperatureStats.min,
|
|
208
|
-
this.temperatureStats.max
|
|
209
|
-
);
|
|
210
|
-
|
|
211
|
-
this.grayToTempMapping = { tempAtGray0, tempAtGray255 };
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
export default WindowLevelController;
|