ROI透明背景(MATLAB)
ROI transparent background (MATLAB)
我正在尝试使用 imfreehand 并保存我的 ROI 的坐标,因此当我将图像叠加到另一个图像上时,该 ROI 是透明的(或设置为 NAN)。当我覆盖到另一个图像上时,基本上有没有办法在 imfreehand 区域内设置为 'NaN'?参考附图。
这是我的代码:
%% output arrival_time parametric heatmap %%
figure;imagesc(at);colorbar;title('arrival time');
colormap('jet');caxis([2.5 5]);
%savefig(['Acute_1e13_draft_DRAFT_ignore' '.fig']);
%% code to blend heatmap %%
minv = 2.5;%min(min(R1_perf(:,:,29)));
maxv = 5;%max(max(R1_perf(:,:,2 t9)));
map=colormap('jet');
ncol = size(map,1);
s = round(1+(ncol-1)*(at-minv)/(maxv-minv)); % Taking arrival time values and rounding differences
rgb_at = ind2rgb(s,map);
rgb_at = imresize(rgb_at,5);
rgb_perf = ind2rgb(s,map);
rgb_perf = imresize(rgb_perf,5);
rgb_at_scale = imresize(rgb_at,[100 350],'nearest');
%rgb_at_scale_2 = imresize(rgb_at,[170 220],'nearest');
toto = zeros(size(rgb_at_scale));
%toto_2 = zeros(size(rgb_at_scale_2));
%toto(190:293,100:455,:) = rgb_at;
alpha = 0.65;
rgb_blend = fliplr(alpha * rgb_at_scale + (1 - alpha) * toto);
hFH = imfreehand();
% Create a binary image ("mask") from the ROI object.
binaryImage = hFH.createMask();
xy = hFH.getPosition;
structBoundaries = bwboundaries(binaryImage);
xy=structBoundaries{1}; % Get n by 2 array of x,y coordinates.
x = xy(:, 2); % Columns.
y = xy(:, 1); % Rows.
hold on; % Don't blow away the image.
plot(x, y, 'LineWidth', 2);
drawnow; % Force it to draw immediately.
% Burn line into image by setting it to 255 wherever the mask is true.
burnedImage = figure;
burnedImage(binaryImage) = nan;
imshow(burnedImage);
axis on;[![enter image description here][1]][1]
如果您确定要使用 imfreehand 绘制 ROI,请使用以下代码。
I = imread('pout.tif');
imshow(I)
% Draw the ROI and double click when finished
h = imfreehand();
position = wait(h);
map = createMask(h);
% set image points within the ROI to NaN
I(map) = nan;
imshow(I)
如果你想要一个更通用的方法,我建议你试试下面的代码。它计算哪些图像点位于由 x 和 y 定义的多边形内。如果您有兴趣,可以阅读此 link 之后使用的方程式。
I = imread('eight.tif');
% Get subscripts
[py, px] = meshgrid(1:size(I,1), 1:size(I,2));
% Coordinates defining the region
x = [222 272 300 270 221 194];
y = [21 21 75 121 121 75];
% Display the region
figure
imagesc(I)
hold on
fill(x,y,'r')
hold off
% Close the loop of the polygon
x = [x,x(1)];
y = [y,y(1)];
n = numel(x);
k = zeros([size(px),n-1]);
% See link for explanation
for i=1:n-1
k(:,:,i) = (px - x(i))*(y(i+1) - y(i)) - (py - y(i))*(x(i+1) - x(i));
end
map = all(k > 0,3) | all(k < 0,3);
% Set image points within the ROI to NaN
I(map') = nan;
% Display the final result
figure
imagesc(I)
我正在尝试使用 imfreehand 并保存我的 ROI 的坐标,因此当我将图像叠加到另一个图像上时,该 ROI 是透明的(或设置为 NAN)。当我覆盖到另一个图像上时,基本上有没有办法在 imfreehand 区域内设置为 'NaN'?参考附图。
这是我的代码:
%% output arrival_time parametric heatmap %%
figure;imagesc(at);colorbar;title('arrival time');
colormap('jet');caxis([2.5 5]);
%savefig(['Acute_1e13_draft_DRAFT_ignore' '.fig']);
%% code to blend heatmap %%
minv = 2.5;%min(min(R1_perf(:,:,29)));
maxv = 5;%max(max(R1_perf(:,:,2 t9)));
map=colormap('jet');
ncol = size(map,1);
s = round(1+(ncol-1)*(at-minv)/(maxv-minv)); % Taking arrival time values and rounding differences
rgb_at = ind2rgb(s,map);
rgb_at = imresize(rgb_at,5);
rgb_perf = ind2rgb(s,map);
rgb_perf = imresize(rgb_perf,5);
rgb_at_scale = imresize(rgb_at,[100 350],'nearest');
%rgb_at_scale_2 = imresize(rgb_at,[170 220],'nearest');
toto = zeros(size(rgb_at_scale));
%toto_2 = zeros(size(rgb_at_scale_2));
%toto(190:293,100:455,:) = rgb_at;
alpha = 0.65;
rgb_blend = fliplr(alpha * rgb_at_scale + (1 - alpha) * toto);
hFH = imfreehand();
% Create a binary image ("mask") from the ROI object.
binaryImage = hFH.createMask();
xy = hFH.getPosition;
structBoundaries = bwboundaries(binaryImage);
xy=structBoundaries{1}; % Get n by 2 array of x,y coordinates.
x = xy(:, 2); % Columns.
y = xy(:, 1); % Rows.
hold on; % Don't blow away the image.
plot(x, y, 'LineWidth', 2);
drawnow; % Force it to draw immediately.
% Burn line into image by setting it to 255 wherever the mask is true.
burnedImage = figure;
burnedImage(binaryImage) = nan;
imshow(burnedImage);
axis on;[![enter image description here][1]][1]
如果您确定要使用 imfreehand 绘制 ROI,请使用以下代码。
I = imread('pout.tif');
imshow(I)
% Draw the ROI and double click when finished
h = imfreehand();
position = wait(h);
map = createMask(h);
% set image points within the ROI to NaN
I(map) = nan;
imshow(I)
如果你想要一个更通用的方法,我建议你试试下面的代码。它计算哪些图像点位于由 x 和 y 定义的多边形内。如果您有兴趣,可以阅读此 link 之后使用的方程式。
I = imread('eight.tif');
% Get subscripts
[py, px] = meshgrid(1:size(I,1), 1:size(I,2));
% Coordinates defining the region
x = [222 272 300 270 221 194];
y = [21 21 75 121 121 75];
% Display the region
figure
imagesc(I)
hold on
fill(x,y,'r')
hold off
% Close the loop of the polygon
x = [x,x(1)];
y = [y,y(1)];
n = numel(x);
k = zeros([size(px),n-1]);
% See link for explanation
for i=1:n-1
k(:,:,i) = (px - x(i))*(y(i+1) - y(i)) - (py - y(i))*(x(i+1) - x(i));
end
map = all(k > 0,3) | all(k < 0,3);
% Set image points within the ROI to NaN
I(map') = nan;
% Display the final result
figure
imagesc(I)