【问题标题】:MATLAB - copying specific values from one 3d array to anotherMATLAB - 将特定值从一个 3d 数组复制到另一个
【发布时间】:2016-04-10 22:41:52
【问题描述】:

我需要使用图像 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

非常感谢您的建议!

【问题讨论】:

    标签: arrays matlab graphics vectorization homography


    【解决方案1】:

    方法#1

    这将是一种使用 linear indexingbsxfun 的矢量化方法 -

    [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,效果很好。
    • 实际上,我做了一个小修改,将 im2EstCart_tocopyfrom(2,:) 的索引交换为 im2EstCart_tocopyfrom(1,:) 和 im1Cart_topupdate() 的索引。这是因为这些矩阵在第一行存储 X 坐标,在第二行存储 Y 坐标,并且因为图像数组 im1 和 im2 在第一维中具有 Y 值,在第二维中存储 X 值。 (尽管这从我上面发布的代码中并不容易看出)。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    • 2013-08-26
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多