1 close all; 2 clear all; 3 clc; 4 5 img=imread('rice.png'); 6 imshow(img); 7 [m n]=size(img); 8 9 tmp=zeros(m+2,n+2); 10 tmp(2:m+1,2:n+1)=img; 11 Ix=zeros(m+2,n+2); 12 Iy=zeros(m+2,n+2); 13 14 E=zeros(m+2,n+2); 15 16 Ix(:,2:n)=tmp(:,3:n+1)-tmp(:,1:n-1); 17 Iy(2:m,:)=tmp(3:m+1,:)-tmp(1:m-1,:); 18 19 Ix2=Ix(2:m+1,2:n+1).^2; 20 Iy2=Iy(2:m+1,2:n+1).^2; 21 Ixy=Ix(2:m+1,2:n+1).*Iy(2:m+1,2:n+1); 22 23 h=fspecial('gaussian',[7 7],2); 24 Ix2=filter2(h,Ix2); 25 Iy2=filter2(h,Iy2); 26 Ixy=filter2(h,Ixy); 27 28 Rmax=0; 29 R=zeros(m,n); 30 for i=1:m 31 for j=1:n 32 M=[Ix2(i,j) Ixy(i,j);Ixy(i,j) Iy2(i,j)]; 33 R(i,j)=det(M)-0.06*(trace(M))^2; 34 35 if R(i,j)>Rmax 36 Rmax=R(i,j); 37 end 38 end 39 end 40 re=zeros(m+2,n+2); 41 42 tmp(2:m+1,2:n+1)=R; 43 img_re=zeros(m+2,n+2); 44 img_re(2:m+1,2:n+1)=img; 45 for i=2:m+1 46 for j=2:n+1 47 48 if tmp(i,j)>0.01*Rmax &&... 49 tmp(i,j)>tmp(i-1,j-1) && tmp(i,j)>tmp(i-1,j) && tmp(i,j)>tmp(i-1,j+1) &&... 50 tmp(i,j)>tmp(i,j-1) && tmp(i,j)>tmp(i,j+1) &&... 51 tmp(i,j)>tmp(i+1,j-1) && tmp(i,j)>tmp(i+1,j) && tmp(i,j)>tmp(i+1,j+1) 52 img_re(i,j)=255; 53 end 54 55 end 56 end 57 58 figure,imshow(mat2gray(img_re(2:m+1,2:n+1)));
图片福利: