当前位置:   article > 正文

Matlab实现人眼精准识别和定位_人眼的识别与定位

人眼的识别与定位

detector=vision.CascadeObjectDetector('LeftEyeCART');

利用matlab中的vision包来检测人眼

 

是不是定位的很准,下面放一下代码:

  1. detector=vision.CascadeObjectDetector('LeftEyeCART');
  2. input_image=imread('img3.jpg');
  3. figure();imshow(input_image);title('原图');
  4. gauss_image=imgaussfilt(input_image,1);
  5. eyes=step(detector,gauss_image);
  6. size_eyes = size(eyes);%眼睛的狂
  7. hold on;
  8. for i =1:size_eyes(1);2
  9. eyes_final=eyes(i,1:2);%x,y
  10. image = imcrop(gauss_image,eyes(i,:));
  11. rectangle('Position',eyes(i,:),'edgeColor','b');
  12. image = rgb2gray(image);
  13. image = im2double(image);
  14. [grad_x,grad_y] = gradient(image);%计算梯度
  15. g = grad_x.*grad_x + grad_y.*grad_y;
  16. g = sqrt(g);%眼睛区域的梯度图
  17. std_g = std(g(:));%计算标准差
  18. mean_g = mean(g(:));%计算平均值
  19. %其实这一步取得是< mean_g - 0.5*std_g和> mean_g + 0.5*std_g部分
  20. g_1 = g;
  21. g_1(g_1 < mean_g + 0.5*std_g) = 0;%梯度图中小于均值+0.5*标准差的设置为0
  22. g_2 = g;
  23. g_2(g_2 > mean_g - 0.5*std_g) = 0;%梯度图中大于均值-0.5标准差的设置为0
  24. g = g_1 + g_2;
  25. temp_g = g;
  26. w = 1-image;
  27. temp_g(temp_g ~= 0) = 1;%将不等于0的设置为1 这该图只有01两个值
  28. grad_x = grad_x.*temp_g;%与梯度相成
  29. grad_y = grad_y.*temp_g;
  30. size_img = size(image);
  31. Dx = repmat(1:size_img(2),size_img(1),1);%每一行都是1-44
  32. Dy = repmat(transpose(1:size_img(1)),1,size_img(2));%转置
  33. C_max = 0;
  34. X = 0;
  35. Y = 0;
  36. %为了得出眼睛的中心位置
  37. for x = 1:size_img(2)
  38. for y = 1:size_img(1)
  39. D_x = Dx - x;
  40. D_y = Dy - y;
  41. D_t = D_x.*D_x + D_y.*D_y;
  42. E = sqrt(D_t);
  43. D_x = D_x./E;
  44. D_y = D_y./E;
  45. C_x = D_x.*grad_x;
  46. C_y = D_y.*grad_y;
  47. C = w(y,x)*(C_x + C_y);
  48. C(C < 0) = 0;
  49. ind = find(isnan(C));
  50. C(ind)=0;
  51. % C(isNaN(C)) = 0;
  52. total_C = sum(sum(C));
  53. if(total_C > C_max)
  54. C_max = total_C;
  55. X = x;
  56. Y = y;
  57. end
  58. end
  59. end
  60. plot(X+eyes_final(1),Y+eyes_final(2),'r+','MarkerSize',10);
  61. p(i,1)=X+eyes_final(1);
  62. p(i,2)=Y+eyes_final(2);
  63. end

再展示几张普通人的定位把:

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/498573
推荐阅读
相关标签
  

闽ICP备14008679号