Reputation: 43
I'm trying to use imfreehand and save the coordinates of my ROI so when I overlay the image onto another, that ROI is transparent (or set to NAN). Basically is there a way to set inside the imfreehand region as 'NaN' when I overlay onto another image? Reference attached image.
Here is my code:
%% 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]
Upvotes: 1
Views: 293
Reputation: 36
If you are sure that you want to use imfreehand to draw the ROIs then use following code.
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)
If you want a more generalised method I suggest you try the code below. It calculates which image points are within the polygon defined by x and y. If you are interested you can read about the equations used following this 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)
Upvotes: 2