MATLAB - 将特定值从一个 3d 数组复制到另一个
MATLAB - copying specific values from one 3d array to another
我需要使用图像 2 的特定坐标的 rgb 值更新图像 1。
我有两个二维矩阵(im1Cart_toupdate 2x114056 和 im2EstCart_tocopyfrom 也是 2x114056)。这些包含我要将 rgb 值从图像 2 复制到图像 1 的有序 x-y 对。
即有 114,056 个像素,我想在其中复制颜色。
im1 (440x1370x3) 和 im2 (240x320x3) 是图像数组。注意 im2 将被拉伸,因此 im2 的一些像素将在 im2EstCart_tocopyfrom.
中出现不止一次
我需要一种有效的方法来执行此操作,因为即使使用上述图像尺寸,我当前的实现速度也非常慢。我曾认为可能有一些使用 sub2ind 的方法 - 但我不确定如何使用 3d 数组来做到这一点。
这是我当前的代码。是 for 循环要了我的命!
%Create a matrix of all pixel coordinates in im1 (homogenised form)
[im1gridx im1gridy]=meshgrid(1:im1width,1:im1height);
im1Cart = [im1gridx(:) im1gridy(:)]';
im1Hom = [im1Cart; ones(1,numel(im1gridy))];
%transform pixel positions with homography (HEst is a matrix built
%elsewhere) to find where they are in the coordinates of image 2
im2EstHom = HEst*im1Hom;
im2EstCart = im2EstHom(1:2,:)./repmat(im2EstHom(3,:),2,1);
im2EstCart = round(im2EstCart);
%check if the the transformed position is within the boundary of image 2
validCoords = im2EstCart(1,:)>0 & im2EstCart(2,:)>0 & im2EstCart(1,:)<=im2width & im2EstCart(2,:)<=im2height;
im1Cart_toupdate=im1Cart(:,validCoords);
im2EstCart_tocopyfrom=im2EstCart(:,validCoords);
%copy colour from image 2 to image 1 - currently pixel by pixel
%but CAN THIS BE VECTORISED?
for i=1:size(im1Cart_toupdate,2)
im1y=im1Cart_toupdate(1,i);
im1x=im1Cart_toupdate(2,i);
im2y=im2EstCart_tocopyfrom(1,i);
im2x=im2EstCart_tocopyfrom(2,i);
im1(im1y,im1x,:) = im2(im2y,im2x,:);
drawnow
end
非常感谢任何建议!
方法 #1
这将是一种使用 linear indexing
with bsxfun
-
的矢量化方法
[m2,n2,r2] = size(im2);
RHS_idx1 = (im2EstCart_tocopyfrom(2,:)-1)*m2 + im2EstCart_tocopyfrom(1,:)
RHS_allidx = bsxfun(@plus,RHS_idx1(:),(0:r2-1)*m2*n2)
[m1,n1,r1] = size(im1);
LHS_idx1 = (im1Cart_toupdate(2,:)-1)*m1 + im1Cart_toupdate(1,:)
LHS_allidx = bsxfun(@plus,LHS_idx1(:),(0:r1-1)*m1*n1)
im1(LHS_allidx) = im2(RHS_allidx)
方法 #2
这是另一种方法,在合并前两个维度后将输入 3D
数组重塑为 2D
数组,然后使用 linear indexing
提取和设置值,最后重塑回它原来的 3D
大小,像这样 -
[m2,n2,r2] = size(im2)
RHS_idx1 = (im2EstCart_tocopyfrom(2,:)-1)*m2 + im2EstCart_tocopyfrom(1,:)
im2r = reshape(im2,[],r2)
[m1,n1,r1] = size(im1)
LHS_idx1 = (im1Cart_toupdate(2,:)-1)*m1 + im1Cart_toupdate(1,:)
im1r = reshape(im1,[],r1)
im1r(LHS_idx1,:) = im2r(RHS_idx1,:)
im1 = reshape(im1r,size(im1));
我需要使用图像 2 的特定坐标的 rgb 值更新图像 1。
我有两个二维矩阵(im1Cart_toupdate 2x114056 和 im2EstCart_tocopyfrom 也是 2x114056)。这些包含我要将 rgb 值从图像 2 复制到图像 1 的有序 x-y 对。
即有 114,056 个像素,我想在其中复制颜色。
im1 (440x1370x3) 和 im2 (240x320x3) 是图像数组。注意 im2 将被拉伸,因此 im2 的一些像素将在 im2EstCart_tocopyfrom.
中出现不止一次我需要一种有效的方法来执行此操作,因为即使使用上述图像尺寸,我当前的实现速度也非常慢。我曾认为可能有一些使用 sub2ind 的方法 - 但我不确定如何使用 3d 数组来做到这一点。
这是我当前的代码。是 for 循环要了我的命!
%Create a matrix of all pixel coordinates in im1 (homogenised form)
[im1gridx im1gridy]=meshgrid(1:im1width,1:im1height);
im1Cart = [im1gridx(:) im1gridy(:)]';
im1Hom = [im1Cart; ones(1,numel(im1gridy))];
%transform pixel positions with homography (HEst is a matrix built
%elsewhere) to find where they are in the coordinates of image 2
im2EstHom = HEst*im1Hom;
im2EstCart = im2EstHom(1:2,:)./repmat(im2EstHom(3,:),2,1);
im2EstCart = round(im2EstCart);
%check if the the transformed position is within the boundary of image 2
validCoords = im2EstCart(1,:)>0 & im2EstCart(2,:)>0 & im2EstCart(1,:)<=im2width & im2EstCart(2,:)<=im2height;
im1Cart_toupdate=im1Cart(:,validCoords);
im2EstCart_tocopyfrom=im2EstCart(:,validCoords);
%copy colour from image 2 to image 1 - currently pixel by pixel
%but CAN THIS BE VECTORISED?
for i=1:size(im1Cart_toupdate,2)
im1y=im1Cart_toupdate(1,i);
im1x=im1Cart_toupdate(2,i);
im2y=im2EstCart_tocopyfrom(1,i);
im2x=im2EstCart_tocopyfrom(2,i);
im1(im1y,im1x,:) = im2(im2y,im2x,:);
drawnow
end
非常感谢任何建议!
方法 #1
这将是一种使用 linear indexing
with bsxfun
-
[m2,n2,r2] = size(im2);
RHS_idx1 = (im2EstCart_tocopyfrom(2,:)-1)*m2 + im2EstCart_tocopyfrom(1,:)
RHS_allidx = bsxfun(@plus,RHS_idx1(:),(0:r2-1)*m2*n2)
[m1,n1,r1] = size(im1);
LHS_idx1 = (im1Cart_toupdate(2,:)-1)*m1 + im1Cart_toupdate(1,:)
LHS_allidx = bsxfun(@plus,LHS_idx1(:),(0:r1-1)*m1*n1)
im1(LHS_allidx) = im2(RHS_allidx)
方法 #2
这是另一种方法,在合并前两个维度后将输入 3D
数组重塑为 2D
数组,然后使用 linear indexing
提取和设置值,最后重塑回它原来的 3D
大小,像这样 -
[m2,n2,r2] = size(im2)
RHS_idx1 = (im2EstCart_tocopyfrom(2,:)-1)*m2 + im2EstCart_tocopyfrom(1,:)
im2r = reshape(im2,[],r2)
[m1,n1,r1] = size(im1)
LHS_idx1 = (im1Cart_toupdate(2,:)-1)*m1 + im1Cart_toupdate(1,:)
im1r = reshape(im1,[],r1)
im1r(LHS_idx1,:) = im2r(RHS_idx1,:)
im1 = reshape(im1r,size(im1));