DDDD
DDDD

Reputation: 21

How to ignore Black border in MATLAB

i make some compare between 2 very similar images , (by code RANSAC) and after it i rotate one of the images to the angle of the first image . the problem is that in some images you have a black border, that Distorted the compere and the rotate how i make that the compere will be only on the image without black border (ignore it)?

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 

Upvotes: 2

Views: 1022

Answers (1)

Pavel Oganesyan
Pavel Oganesyan

Reputation: 6924

  1. Count black columns - this question. Just remove column if there is not any non-zero pixels.
  2. Do the same trick with rows.
  3. Do what your need with the rest.

This will fail if black borders are not really black - for example, very dark gray. In this case apply threshold on detecting black.

Also it will be bad solution if there are any black columns in the main part of this picture. In this case you should check position of the black column and remove only columns relatively close to borders.

Here is simple version of deleting symmetric black box without black rows inside the epic

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');

Applied to "good" input:

enter image description here

Applied to image with inner black lines - not so good result. enter image description here

In case you need precise detecting here is a plan:

  1. Check columns from the most left to the first with color and remove all black.
  2. Check columns from the most right backwards to the last with color and remove black
  3. Do the same with rows.

I will not provide code for this one because it's just some matrix operations OP can do by himself.

Upvotes: 1

Related Questions