ROI прозрачный фон (MATLAB) - PullRequest
       69

ROI прозрачный фон (MATLAB)

0 голосов
/ 27 августа 2018

Я пытаюсь использовать imfreehand и сохранить координаты моего ROI, чтобы при наложении изображения на другое ROI был прозрачным (или установлен на NAN). В принципе, есть ли способ установить внутри свободной области значение «NaN», когда я накладываю изображение на другое изображение? Ссылка на прикрепленное изображение.

enter image description here

Вот мой код:

%% 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]

1 Ответ

0 голосов
/ 29 августа 2018

Если вы уверены, что хотите использовать 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. Если вам интересно, вы можете прочитать об используемых уравнениях, следуя этой ссылке .

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