查看: 883|回复: 13|关注: 0

[已解决] matlab里对一个区域进行定位

[复制链接]

新手

29 麦片

财富积分


050


4

主题

7

帖子

0

最佳答案
  • 关注者: 1
如图,希望得到图中坦克的位置坐标,并且把该区域框起来。求程序,谢谢各位老师。
HB14955.JPG
HB14982.JPG
回复主题 已获打赏: 0 积分

举报

论坛优秀回答者

中级

518 麦片

财富积分


5001500


2

主题

624

帖子

110

最佳答案
  • 关注者: 45
发表于 2018-5-14 11:53:46 | 显示全部楼层 |此回复为最佳答案
代码如下,可根据自己的需要修改算法:
  1. close all
  2. clear
  3. clc

  4. I = imread('HB14982.JPG');

  5. level = graythresh(I);
  6. Ibw = im2bw(I,1.5*level);

  7. regionsMax = maxArea(Ibw,1,1);

  8. objIdx = find(regionsMax>0);
  9. [y, x] = ind2sub(size(I), objIdx);

  10. [rectx,recty,area,perimeter] = minboundrect(x,y);

  11. % show result
  12. figure;
  13. imshow(I);
  14. hold on;
  15. line(rectx,recty,'color','g');

  16. function regionsMax = maxArea(Ibw,regionNumber,strelSize)

  17. %-----------------------------------------------------------
  18. % 功能:找到二值图像中区域最大的几个形状
  19. %
  20. % 函数名: maxArea()
  21. % 参数:
  22. %   Ibw: 二值图像;
  23. %   regionNumber: 最大形状个数
  24. %   strelSize:使细小连通区域分离的开运算元素半径大小
  25. % 输出值:
  26. %   regionsMax:最大区域图形
  27. %------------------------------------------------------------

  28. se = strel('disk',strelSize);  % 半径为strelSize的圆盘型元素
  29. % BW = imopen(Ibw,se);   % 图像开运算
  30. BW = imclose(Ibw,se);   % 图像闭运算
  31. L = bwlabel(BW);       % 图像中图形区域编号
  32. S = regionprops(L,'Area'); % 求各图形的面积
  33. areas =[S.Area];
  34. [~,ind] = sort(areas,'descend');
  35. regionsMax = ismember(L,ind(1:regionNumber));  % 选择图形中面积最大的前regionNumber个图形

  36. end


  37. function [rectx,recty,area,perimeter] = minboundrect(x,y,metric)
  38. % minboundrect: Compute the minimal bounding rectangle of points in the plane
  39. % usage: [rectx,recty,area,perimeter] = minboundrect(x,y,metric)
  40. %
  41. % arguments: (input)
  42. %  x,y - vectors of points, describing points in the plane as
  43. %        (x,y) pairs. x and y must be the same lengths.
  44. %
  45. %  metric - (OPTIONAL) - single letter character flag which
  46. %        denotes the use of minimal area or perimeter as the
  47. %        metric to be minimized. metric may be either 'a' or 'p',
  48. %        capitalization is ignored. Any other contraction of 'area'
  49. %        or 'perimeter' is also accepted.
  50. %
  51. %        DEFAULT: 'a'    ('area')
  52. %
  53. % arguments: (output)
  54. %  rectx,recty - 5x1 vectors of points that define the minimal
  55. %        bounding rectangle.
  56. %
  57. %  area - (scalar) area of the minimal rect itself.
  58. %
  59. %  perimeter - (scalar) perimeter of the minimal rect as found
  60. %
  61. %
  62. % Note: For those individuals who would prefer the rect with minimum
  63. % perimeter or area, careful testing convinces me that the minimum area
  64. % rect was generally also the minimum perimeter rect on most problems
  65. % (with one class of exceptions). This same testing appeared to verify my
  66. % assumption that the minimum area rect must always contain at least
  67. % one edge of the convex hull. The exception I refer to above is for
  68. % problems when the convex hull is composed of only a few points,
  69. % most likely exactly 3. Here one may see differences between the
  70. % two metrics. My thanks to Roger Stafford for pointing out this
  71. % class of counter-examples.
  72. %
  73. % Thanks are also due to Roger for pointing out a proof that the
  74. % bounding rect must always contain an edge of the convex hull, in
  75. % both the minimal perimeter and area cases.
  76. %
  77. %
  78. % Example usage:
  79. %  x = rand(50000,1);
  80. %  y = rand(50000,1);
  81. %  tic,[rx,ry,area] = minboundrect(x,y);toc
  82. %
  83. %  Elapsed time is 0.105754 seconds.
  84. %
  85. %  [rx,ry]
  86. %  ans =
  87. %      0.99994  -4.2515e-06
  88. %      0.99998      0.99999
  89. %   2.6441e-05            1
  90. %  -5.1673e-06   2.7356e-05
  91. %      0.99994  -4.2515e-06
  92. %
  93. %  area
  94. %  area =
  95. %      0.99994
  96. %
  97. %
  98. % See also: minboundcircle, minboundtri, minboundsphere
  99. %
  100. %
  101. % Author: John D'Errico
  102. % E-mail: woodchips@rochester.rr.com
  103. % Release: 3.0
  104. % Release date: 3/7/07

  105. % default for metric
  106. if (nargin<3) || isempty(metric)
  107.     metric = 'a';
  108. elseif ~ischar(metric)
  109.     error 'metric must be a character flag if it is supplied.'
  110. else
  111.     % check for 'a' or 'p'
  112.     metric = lower(metric(:)');
  113.     ind = strmatch(metric,{'area','perimeter'});
  114.     if isempty(ind)
  115.         error 'metric does not match either ''area'' or ''perimeter'''
  116.     end
  117.    
  118.     % just keep the first letter.
  119.     metric = metric(1);
  120. end

  121. % preprocess data
  122. x=x(:);
  123. y=y(:);

  124. % not many error checks to worry about
  125. n = length(x);
  126. if n~=length(y)
  127.     error 'x and y must be the same sizes'
  128. end

  129. % start out with the convex hull of the points to
  130. % reduce the problem dramatically. Note that any
  131. % points in the interior of the convex hull are
  132. % never needed, so we drop them.
  133. if n>3
  134.     edges = convhull(x,y);
  135.    
  136.     % exclude those points inside the hull as not relevant
  137.     % also sorts the points into their convex hull as a
  138.     % closed polygon
  139.    
  140.     x = x(edges);
  141.     y = y(edges);
  142.    
  143.     % probably fewer points now, unless the points are fully convex
  144.     nedges = length(x) - 1;
  145. elseif n>1
  146.     % n must be 2 or 3
  147.     nedges = n;
  148.     x(end+1) = x(1);
  149.     y(end+1) = y(1);
  150. else
  151.     % n must be 0 or 1
  152.     nedges = n;
  153. end

  154. % now we must find the bounding rectangle of those
  155. % that remain.

  156. % special case small numbers of points. If we trip any
  157. % of these cases, then we are done, so return.
  158. switch nedges
  159.     case 0
  160.         % empty begets empty
  161.         rectx = [];
  162.         recty = [];
  163.         area = [];
  164.         perimeter = [];
  165.         return
  166.     case 1
  167.         % with one point, the rect is simple.
  168.         rectx = repmat(x,1,5);
  169.         recty = repmat(y,1,5);
  170.         area = 0;
  171.         perimeter = 0;
  172.         return
  173.     case 2
  174.         % only two points. also simple.
  175.         rectx = x([1 2 2 1 1]);
  176.         recty = y([1 2 2 1 1]);
  177.         area = 0;
  178.         perimeter = 2*sqrt(diff(x).^2 + diff(y).^2);
  179.         return
  180. end
  181. % 3 or more points.

  182. % will need a 2x2 rotation matrix through an angle theta
  183. Rmat = @(theta) [cos(theta) sin(theta);-sin(theta) cos(theta)];

  184. % get the angle of each edge of the hull polygon.
  185. ind = 1:(length(x)-1);
  186. edgeangles = atan2(y(ind+1) - y(ind),x(ind+1) - x(ind));
  187. % move the angle into the first quadrant.
  188. edgeangles = unique(mod(edgeangles,pi/2));

  189. % now just check each edge of the hull
  190. nang = length(edgeangles);
  191. area = inf;
  192. perimeter = inf;
  193. met = inf;
  194. xy = [x,y];
  195. for i = 1:nang
  196.     % rotate the data through -theta
  197.     rot = Rmat(-edgeangles(i));
  198.     xyr = xy*rot;
  199.     xymin = min(xyr,[],1);
  200.     xymax = max(xyr,[],1);
  201.    
  202.     % The area is simple, as is the perimeter
  203.     A_i = prod(xymax - xymin);
  204.     P_i = 2*sum(xymax-xymin);
  205.    
  206.     if metric=='a'
  207.         M_i = A_i;
  208.     else
  209.         M_i = P_i;
  210.     end
  211.    
  212.     % new metric value for the current interval. Is it better?
  213.     if M_i<met
  214.         % keep this one
  215.         met = M_i;
  216.         area = A_i;
  217.         perimeter = P_i;
  218.         
  219.         rect = [xymin;[xymax(1),xymin(2)];xymax;[xymin(1),xymax(2)];xymin];
  220.         rect = rect*rot';
  221.         rectx = rect(:,1);
  222.         recty = rect(:,2);
  223.     end
  224. end
  225. % get the final rect

  226. % all done

  227. end % mainline end
复制代码


运行结果:

result1

result1

result2

result2
回复此楼 已获打赏: 0 积分

举报

新手

29 麦片

财富积分


050


4

主题

7

帖子

0

最佳答案
  • 关注者: 1
 楼主| 发表于 2018-5-14 12:32:55 | 显示全部楼层
atpalain 发表于 2018-5-14 11:53
代码如下,可根据自己的需要修改算法:

谢谢您!我试了一下,提示未对参数 rectx 或者还有其他参数赋值,请问怎么赋值啊
回复此楼 已获打赏: 0 积分

举报

论坛优秀回答者

中级

518 麦片

财富积分


5001500


2

主题

624

帖子

110

最佳答案
  • 关注者: 45
发表于 2018-5-14 12:51:46 | 显示全部楼层
NO_34 发表于 2018-5-14 12:32
谢谢您!我试了一下,提示未对参数 rectx 或者还有其他参数赋值,请问怎么赋值啊 ...

并不需要赋值 那个是输出结果 所有代码跑一遍就行
回复此楼 已获打赏: 0 积分

举报

新手

49 麦片

财富积分


050


3

主题

104

帖子

0

最佳答案
发表于 2018-5-14 14:59:37 | 显示全部楼层
atpalain 发表于 2018-5-14 12:51
并不需要赋值 那个是输出结果 所有代码跑一遍就行

close all
clear
clc

I = imread('d:\work\tank.jpg');

level = graythresh(I);
Ibw = im2bw(I,1.5*level);
Ibw = double(Ibw)
regionsMax = maxArea(Ibw,1,1);

objIdx = find(regionsMax>0);
[y, x] = ind2sub(size(I), objIdx);

[rectx,recty,area,perimeter] = minboundrect(x,y);

% show result
figure;
imshow(I);
hold on;
line(rectx,recty,'color','g');
到这就出错:Undefined function or method 'maxArea' for input arguments of type 'double'
回复此楼 已获打赏: 0 积分

举报

新手

49 麦片

财富积分


050


3

主题

104

帖子

0

最佳答案
发表于 2018-5-14 15:02:40 | 显示全部楼层
Ibw = double(Ibw)这句是后来加的,一样的错误
回复此楼 已获打赏: 0 积分

举报

论坛优秀回答者

中级

518 麦片

财富积分


5001500


2

主题

624

帖子

110

最佳答案
  • 关注者: 45
发表于 2018-5-14 15:28:42 | 显示全部楼层
dwj777 发表于 2018-5-14 15:02
Ibw = double(Ibw)这句是后来加的,一样的错误

maxArea的输入参数是二值图 转成double型是无法使用这个函数的
回复此楼 已获打赏: 0 积分

举报

新手

49 麦片

财富积分


050


3

主题

104

帖子

0

最佳答案
发表于 2018-5-14 16:20:56 | 显示全部楼层
谢谢,但是您的原程序报Undefined function or method 'maxArea' for input arguments of type 'double'怎么解决啊!
回复此楼 已获打赏: 0 积分

举报

新手

49 麦片

财富积分


050


3

主题

104

帖子

0

最佳答案
发表于 2018-5-14 16:25:32 | 显示全部楼层
未定义的函数或方法“maxArea”用于输入“Double”类型的参数
回复此楼 已获打赏: 0 积分

举报

新手

49 麦片

财富积分


050


3

主题

104

帖子

0

最佳答案
发表于 2018-5-14 16:29:41 | 显示全部楼层
查了一下帮助,遗憾,我的版本2010B没有maxArea这个函数,再次谢谢您
回复此楼 已获打赏: 0 积分

举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐上一条 /4 下一条

快速回复 返回顶部 返回列表