Has anyone implemented moravec corner detector in matlab??

This is what i have tried.But with very less accurate.
clear all
close all
clc
tic
I=imread('panorama_image1.jpg');
originalmap=I;
w=5;
m=floor(size(I,1)/w);
n=floor(size(I,2)/w);
c=ceil(w/2)+(0:n-1)*w;
r=ceil(w/2)+(0:m-1)*w;
step=floor(w/2);
for y_unit=1:m
for x_unit=1:n
v1=0;v2=0;v3=0;v4=0;
for i=-step:step-1
v1=v1+(I(r(y_unit),c(x_unit)+i)-I(r(y_unit),c(x_unit)+i+1))^2;
v2=v2+(I(r(y_unit)+i,c(x_unit)+i)-I(r(y_unit)+i+1,c(x_unit)+i+1))^2;
v3=v3+(I(r(y_unit)+i,c(x_unit))-I(r(y_unit)+i+1,c(x_unit)))^2;
v4=v4+(I(r(y_unit)-i,c(x_unit)+i)-I(r(y_unit)-i-1,c(x_unit)+i+1))^2;
end
IV_cr(y_unit,x_unit)=min([v1,v2,v3,v4]);
end
end
exper_thr=20;
IV_cr(IV_cr<exper_thr)=NaN;
wf=9;
mf=floor(m/wf);
nf=floor(n/wf);
xc=[];
yc=[]; for y_unit=1:mf
for x_unit=1:nf
[C,I]=max(IV_cr((y_unit-1)*wf+1:y_unit*wf,(x_unit-1)*wf+1:x_unit*wf))
[C1,I1]=max(C);
IV_crch(y_unit,x_unit)=C1;
row=I(I1);
col=I1;
crch_row(y_unit,x_unit)=(y_unit-1)*wf+row;
crch_col(y_unit,x_unit)=(x_unit-1)*wf+col;
yc=[yc,r((y_unit-1)*wf+row)];xc=[xc,c((x_unit-1)*wf+col)];
IV_cr((y_unit-1)*wf+1:y_unit*wf,(x_unit-1)*wf+1:x_unit*wf)=NaN;
IV_cr((y_unit-1)*wf+row,(x_unit-1)*wf+col)=C1;
end
end
figure(1)
imshow(originalmap)
figure(2)
plot(xc,yc,'*')
view(0,-90)
toc;
t=toc;
disp(['amount of time',num2str(t),'sec'])
figure(3)
imshow(originalmap)
hold on
plot(xc,yc,'R*')
axis on
end

 Accepted Answer

I just did if for you:
function C = moravec(I)
%MORAVEC Moravec corner detector
%
% C = MORAVEC(I)
%
% Thorsten.Hansen@psychol.uni-giessen.de 2015-05-20
if nargin == 0 % simple test
I = zeros(100);
I(40:60, 40:60) = 1;
C = moravec(I);
imshow(0.5*C+I, [])
if nargout == 0, clear C, end
return
end
D(:,:,1) = diff([I I(:,end)]')';
D(:,:,2) = diff([I(:,1) I]')';
D(:,:,3) = diff([I; I(end,:)]);
D(:,:,4) = diff([I(1,:); I]);
C = sum(D.^2, 3);
if nargout == 0, clear C, end

12 Comments

Dimensions of matrices being concatenated are not consistent.??
So far it works just for grayscale images, not color images, but you could extend it.
Tried for grayscale image also.
You might have an alpha channel with your grayscale image, check:
size(image) and check if it says (x x 2) or (x x 1), if it says (x x 2) you have to remove the alpha channel first.
I just tried the following, which works fine:
>> I = imread('cameraman.tif');
>> C = moravec(I);
>> size(I)
ans =
256 256
Can you reproduce that?
I also tried
R = rand(225, 300); C = moravec(R);
which also works fine for me.
Bad luck!!! Not happening here. Can u display the output here.
What do you mean by "Bad luck"? Does the result look strange, or do you get a Matlab error? The Moravec detector is highly sensitive to noise, so you get fine results for artificial images (run moravec without arguments to see an overlay of the image and the detected corners), but less so for natural images.

Sign in to comment.

More Answers (1)

Hi,
Thanks Thorsten for your code! However I think it's missing a few details. I slightly modified the implementation according to the original text by Moravec [1980], and I think now it's producing much better results.
Here's the updated code.
function C = moravec(I)
%MORAVEC Moravec corner detector
%
% C = MORAVEC(I)
%
% Thorsten.Hansen@psychol.uni-giessen.de 2015-05-20
% Ignacio.Rocco@inria.fr 2019-11-14
if nargin == 0 % simple test
I = zeros(100);
I(40:60, 40:60) = 1;
C = moravec(I);
imshow(0.5*C+I, [])
if nargout == 0, clear C, end
return
end
D(:,:,1) = conv2(conv2(single(I),[1 -1],'same').^2,ones(3),'same');
D(:,:,2) = conv2(conv2(single(I),[1;-1],'same').^2,ones(3),'same');
D(:,:,3) = conv2(conv2(single(I),[1 0;0 -1],'same').^2,ones(3),'same');
D(:,:,4) = conv2(conv2(single(I),[0 1;-1 0],'same').^2,ones(3),'same');
C = min(D.^2,[], 3);
if nargout == 0, clear C, end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!