Align two images in OpenCV

前端 未结 1 931
时光说笑
时光说笑 2021-02-14 02:06

I have two images (see below). These images represent the contours of a pair of cables and were captured using laser based 3D triangulation. The first image is captured with the

1条回答
  •  遇见更好的自我
    2021-02-14 02:13

    Template matching would do the trick here. I played with it a little, hope you find it usuful (Code below):

    MAX_DISPARITY = 100;
    imgL=double(imread('https://i.stack.imgur.com/y5tOJ.png'));
    imgR=double(imread('https://i.stack.imgur.com/L1EQy.png'));
    imgRfused = imgR;
    minmax = @(v) [min(v) max(v)];
    [imgLbw,n]=bwlabel(imgL);
    nBlobs=2;
    a=arrayfun(@(i) sum(imgLbw(:)==i),1:n);
    [~,indx]=sort(a,'descend');
    imgLbwC=bsxfun(@eq,imgLbw,permute(indx(1:nBlobs),[3 1 2]));
    imgLbwC =bsxfun(@times,imgLbwC,2.^permute(0:nBlobs-1,[3 1 2]));
    imgLbwC  = sum(imgLbwC ,3);
    
    src = zeros(nBlobs,4);
    dst = zeros(nBlobs,4);
    
    for i=1:nBlobs
        [y,x]=find(imgLbwC==i);
        mmx = minmax(x);
        mmy = minmax(y);
        ker = imgL(mmy(1):mmy(2),mmx(1):mmx(2));
    
        [yg,xg]=ndgrid(mmy(1):mmy(2),mmx(1):mmx(2));
        src(i,:)=[mmx(1) mmy(1) fliplr(size(ker))];
    
    
        imgR_ = imgR(:,mmx(1)-MAX_DISPARITY:mmx(2)+MAX_DISPARITY);
        c=conv2(imgR_ ,rot90(double(ker),2),'valid')./sqrt(conv2(imgR_.^2,ones(size(ker)),'valid'));
        [yy,xx]=find(c==max(c(:)),1);
        dst(i,:)=[src(i,1:2)+[xx yy-mmy(1)]+[-MAX_DISPARITY,0] fliplr(size(ker))];
    
        imgRfused(dst(i,2):dst(i,2)+dst(i,4),dst(i,1):dst(i,1)+dst(i,3)) = max(imgRfused(dst(i,2):dst(i,2)+dst(i,4),dst(i,1):dst(i,1)+dst(i,3)),imgL(src(i,2):src(i,2)+src(i,4),src(i,1):src(i,1)+src(i,3)));
    end
    imagesc(imgRfused);
    axis image
    colormap gray
    

    0 讨论(0)
提交回复
热议问题