【问题标题】:How to ignore Black border in MATLAB如何在 MATLAB 中忽略黑色边框
【发布时间】:2014-08-30 23:36:32
【问题描述】:

我在 2 个非常相似的图像之间进行了一些比较(通过代码 RANSAC),然后我将其中一个图像旋转到第一个图像的角度。 问题是在某些图像中,您有黑色边框,这会扭曲主持人和旋转 我如何让主持人只出现在没有黑色边框的图像上(忽略它)

function [ output_args ] = ransac( )
%frames( filename)
global numFrames 
a=sprintf('Ransac begin');
disp(a);
for i=1:numFrames 
file_name = sprintf('frames/%0.3i.jpg', i);
file_name2 = sprintf('frames/%0.3i.jpg', i+1);
%file_name = sprintf('frames/008.jpg', i);
%file_name2 = sprintf('frames/049.jpg', i+1);


I1=im2double(imread(file_name2));
I2=im2double(imread(file_name));

% Get the Key Points
Options.upright=true;
Options.tresh=0.0001;
Ipts1=OpenSurf(I1,Options);
Ipts2=OpenSurf(I2,Options);

% Put the landmark descriptors in a matrix
D1 = reshape([Ipts1.descriptor],64,[]);
D2 = reshape([Ipts2.descriptor],64,[]);

% Find the best matches
err=zeros(1,length(Ipts1));
cor1=1:length(Ipts1);
cor2=zeros(1,length(Ipts1));
for i=1:length(Ipts1),
    distance=sum((D2-repmat(D1(:,i),[1 length(Ipts2)])).^2,1);
    [err(i),cor2(i)]=min(distance);
end

% Sort matches on vector distance
[err, ind]=sort(err);
cor1=cor1(ind);
cor2=cor2(ind);

% Make vectors with the coordinates of the best matches
Pos1=[[Ipts1(cor1).y]',[Ipts1(cor1).x]'];
Pos2=[[Ipts2(cor2).y]',[Ipts2(cor2).x]'];
Pos1=Pos1(1:30,:);
Pos2=Pos2(1:30,:);

% Show both images
I = zeros([size(I1,1) size(I1,2)*2 size(I1,3)]);
I(:,1:size(I1,2),:)=I1; I(:,size(I1,2)+1:size(I1,2)+size(I2,2),:)=I2;

% Calculate affine matrix
Pos1(:,3)=1; Pos2(:,3)=1;
M=Pos1'/Pos2';

% Add subfunctions to Matlab Search path
functionname='OpenSurf.m';
functiondir=which(functionname);
functiondir=functiondir(1:end-length(functionname));
addpath([functiondir '/WarpFunctions'])

% Warp the image
I1_warped=affine_warp(I1,M,'bicubic');

% Show the result
%figure,
subplot(1,3,1), imshow(I1);title('Figure 1');
subplot(1,3,2), imshow(I2);title('Figure 2');
subplot(1,3,3), imshow(I1_warped);title('Warped Figure 1');
imwrite(I1_warped,file_name2);
if (mod(i,20)==0 ) 
    disp(sprintf('he make a %d',i)); 
end
end
sprintf('finish');
aaa();
end 

【问题讨论】:

    标签: image matlab image-processing


    【解决方案1】:
    1. 计算黑色列 - this question。只需删除列,如果 没有任何非零像素。
    2. 对行做同样的事情。
    3. 其余部分随心所欲。

    如果黑色边框不是真正的黑色 - 例如,非常深的灰色,这将失败。在这种情况下,应用阈值检测黑色。

    如果这张图片的主要部分有任何黑色的柱子,这也是一个不好的解决方案。在这种情况下,您应该检查黑色列的位置并仅删除相对靠近边界的列。

    这是删除史诗中没有黑行的对称黑盒的简单版本

    I1=im2double(imread('dart.jpg'));
    
    sizeI = size(I1);
    zeros = floor((sizeI(2) -  min(sum(any(I1))))/2);
    I2 = I1(:, zeros : sizeI(2)-zeros, :);
    nonZero = sum(any(I1,2));
    
    
    sizeI2 = size(I2);
    zerosRows = floor((sizeI(1) -  min(sum(any(I2, 2))))/2);
    I3 = I2(zerosRows : sizeI2(1)-zerosRows, :, :);
    
    subplot(1,3,1), imshow(I1);title('Figure 1');
    subplot(1,3,2), imshow(I2);title('Figure 2');
    subplot(1,3,3), imshow(I3);title('Figure 3');
    

    应用于“好”输入:

    应用于带有内部黑线的图像 - 结果不太好。

    如果您需要精确检测,这里有一个计划:

    1. 检查从最左边到第一列的颜色和 删除所有黑色。
    2. 用颜色从最右边向后检查列并删除黑色
    3. 对行做同样的事情。

    我不会提供这个的代码,因为它只是一些OP可以自己做的矩阵运算。

    【讨论】:

      【解决方案2】:

      但是pavel你的回答不太好 因为你还有一点黑色边框......有人可以修复代码吗? 到最后不会是黑色边框

      【讨论】:

        猜你喜欢
        • 2019-01-26
        • 1970-01-01
        • 2012-09-13
        • 2014-07-09
        • 1970-01-01
        • 2014-12-02
        • 2019-11-17
        • 1970-01-01
        • 2014-10-24
        相关资源
        最近更新 更多