【发布时间】: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