我正在尝试将半透明纯色图像叠加在另一张图像的ROI上。
此代码将一个图像覆盖在另一个图像上:
I = rgb2gray(imread('peppers.png')); % Sample
imshow(I)
% Making an image which is all one colour:
c = {0.00 0.75 0.75}; % color code (e.g. sky blue)
overlay = cat(3, c{1}*ones(size(I)), c{2}*ones(size(I)), c{3}*ones(size(I)));
hold on
h = imshow(overlay);
hold off
set(h, 'AlphaData', I)
这是两张图片:
这就是结果:
但是,我们只想突出显示图像的特定区域(而不是整个图像)。我们怎么做到这一点?
例如,如果我们使用imrect(...)选择矩形ROI:
hh = imrect();
roi = round(wait(hh));
x1 = roi(1);
x2 = x1 + roi(3);
y1 = roi(2);
y2 = y1 + roi(4);
有没有办法只在这些选定的像素上叠加第二张图像(y1:y2,x1:x2)?
答案 0 :(得分:0)
我制作以下代码,希望您觉得它很有用。
I = rgb2gray(imread('peppers.png')); % Sample
imshow(I)
% Get the index of cut image
[Y, X] = ginput(2);
% Sort values to easily work with them
Y = sort(Y);
X = sort(X);
% The size of new image
M = round((X(2)-X(1)) +1);
N = round((Y(2)-Y(1)) +1);
% Cut the original image
I2 = I(round(X(1):X(2)), round(Y(1):Y(2)));
% The new image
R = zeros(M, N ,3);
% Set the values of color you want
R(:,:,3) = 0.75;
R(:,:,2) = 0.75;
% Normalize the image to [0 255] uint8
R = im2uint8(R);
% Add the original image
R(:,:,1) = I2;
imshow(R)
答案 1 :(得分:0)
如何绘制所选像素?
II=repmat(I,1,1,3);
II(y1:y2,x1:x2,1)=II(y1:y2,x1:x2,1)+c{1}*256;
II(y1:y2,x1:x2,2)=II(y1:y2,x1:x2,1)+c{2}*256;
II(y1:y2,x1:x2,3)=II(y1:y2,x1:x2,1)+c{3}*256;
figure;imshow(II)