代码之家  ›  专栏  ›  技术社区  ›  Sere_na

查找两个视频之间匹配的SIFT特征

  •  0
  • Sere_na  · 技术社区  · 12 年前

    我从两个视频中提取了SIFT特征以进行匹配。 我需要将每个第二视频特征与存储在第一视频的特征阵列中的特征进行比较。我在设置代码时遇到了问题,这样当我有了对应关系时,我就可以得到该功能所在的帧。我该怎么做?有人能给我看一个代码示例吗?

    这是我的代码:

    obj = VideoReader('video2.avi');
    lastFrame = read(obj, inf);
    numFrames = obj.NumberOfFrames;
    
    %estrazione frame e sift
    for k = 1 : 3 % numFrames / 5
        disp(['Processing frame #', num2str(k)]);
        this_frame = read(obj, k * 5); % leggi solo un fotogramma ogni 5 per velocizzarle la cosa
        this_frame = imresize(this_frame, 0.5); % rimpiccioliamolo per questioni di efficienza!
        I = single(rgb2gray(this_frame)) ;
        [f,d] = vl_sift(I);  % estrazione feature
    
        features{k} = f;     % salviamo le feautre e i relativi descrittori in delle celle
        descriptors{k} = d;
    end
    save('feature_input', 'features');
    save('descrittori_input', 'descriptors');
    
    %%% un esempio di come ripescare i dati...
    pippo = load('feature_input');
    newfeat = pippo.features;
    pippo = load('descrittori_input');
    newdesc = pippo.descriptors;
    
    for k = 1 : 3
        disp(['Le feature del fotogramma', num2str(k), ' sono: ']);
        f = cell2mat( newfeat(k) );
        f(:, 1:10) % ne mostriamo solo un pezzetto... le posizioni delle prime 10 features
    end
    
    
    obj2 = VideoReader('video2u.avi');
    lastFrame = read(obj2, inf);
    numFrames = obj2.NumberOfFrames;
    
    
    
    %estrazione frame e sift video2
    for k2 = 1 : 3 % numFrames / 5
        disp(['Processing frame #', num2str(k2)]);
        this_frame2 = read(obj2, k2 * 5); % leggi solo un fotogramma ogni 5 per velocizzarle la cosa
        this_frame2 = imresize(this_frame2, 0.5); % rimpiccioliamolo per questioni di efficienza!
        K = single(rgb2gray(this_frame2)) ;
        [f2,d2] = vl_sift(K);  % estrazione feature
    
        features2{k2} = f2;     % salviamo le feautre e i relativi descrittori in delle celle
        descriptors2{k2} = d2;
    end
    
    
    
    
    save('feature2_input', 'features2');
    save('descrittori2_input', 'descriptors2');
    
    %%% un esempio di come ripescare i dati...
    pippo2 = load('feature2_input');
    newfeat2 = pippo2.features2;
    pippo2 = load('descrittori2_input');
    newdesc2 = pippo2.descriptors2;
    
    for k2 = 1 : 3
        disp(['Le feature del fotogramma', num2str(k2), ' sono: ']);
        f2 = cell2mat( newfeat2(k2) );
        f2(:, 1:10) % ne mostriamo solo un pezzetto... le posizioni delle prime 10 features
    
    
    end
    
    
    [matches, scores] = vl_ubcmatch(d, d2, 1.5) ;
    
    % sift points plot
    
        subplot(1,2,1);
        imshow(uint8(I));
        hold on;
        plot(f(1,matches(1,:)),f(2,matches(1,:)),'b*');
    
    
        subplot(1,2,2);
        imshow(uint8(K));
        hold on;
        plot(f2(1,matches(2,:)),f2(2,matches(2,:)),'r*');
    
    
        figure;
    
         %-------------  
    
     % RANSAC
    
    X1 = f(1:2,matches(1,:)) ; X1(3,:) = 1 ;
    X2 = f2(1:2,matches(2,:)) ; X2(3,:) = 1 ;
    
    
    numMatches = size(matches,2) ;
    
    for t = 1:100
      % estimate homograpyh
      subset = vl_colsubset(1:numMatches, 4) ;
      A = [] ;
      for i = subset
        A = cat(1, A, kron(X1(:,i)', vl_hat(X2(:,i)))) ;
      end
    
    
      [U,S,V] = svd(A) ;
    
    
    H{t} = reshape(V(:,9),3,3) ;
    
      % score homography
      X2_ = H{t} * X1 ;
      du = X2_(1,:)./X2_(3,:) - X2(1,:)./X2(3,:) ;
      dv = X2_(2,:)./X2_(3,:) - X2(2,:)./X2(3,:) ;
      ok{t} = (du.*du + dv.*dv) < 6*6 ;
      score(t) = sum(ok{t}) ;
    end
    
    
    
    [score, best] = max(score) ;
    H = H{best};
    ok = ok{best};
    
    
    % sift feature matching 
    
       dh1 = max(size(K,1)-size(I,1),0) ;
       dh2 = max(size(I,1)-size(K,1),0) ;
    
    
    subplot(2,1,1) ;
    imagesc([padarray(I,dh1,'post') padarray(K,dh2,'post')]) ;
     colormap (gray);
    o = size(I,2) ;
    line([f(1,matches(1,:));f2(1,matches(2,:))+o], ...
         [f(2,matches(1,:));f2(2,matches(2,:))]) ;
    
    
    axis image off ;
    
    subplot(2,1,2) ;
    imagesc([padarray(I,dh1,'post') padarray(K,dh2,'post')]) ;
     colormap (gray);
    o = size(I,2) ;
    line([f(1,matches(1,ok));f2(1,matches(2,ok))+o], ...
         [f(2,matches(1,ok));f2(2,matches(2,ok))]) ;
    title(sprintf('%d (%.2f%%) inliner matches out of %d', ...
                  sum(ok), ...
                  100*sum(ok)/numMatches, ...
                  numMatches)) ;
    axis image off ;
    
    drawnow ;
    
    end
    
    1 回复  |  直到 12 年前
        1
  •  0
  •   Autonomous    12 年前

    您正在使用匹配 [matches, scores] = vl_ubcmatch(d, d2, 1.5) ; . d 仅包含最新帧的描述符。您应该执行以下操作:

    for nFrames=1:3
       [matches{nFrames}, scores{nFrames}] = vl_ubcmatch(descriptors{nFrames}, descriptors2{nFrames}, 1.5);
    end
    

    从这里,您应该能够获得帧之间的匹配。