- %%%%矩形区域选取%%%%%
- hf = figure(1);
- A = imread('board.tif');
- A = rgb2gray(A); %-此句注释后下面的处理就相当于对真彩色图像进行
- imshow(A)
- rect = getrect(hf);
- w = round(rect(3));
- h = round(rect(4));
- if w >= 1 & h >= 1
- r = [rect(1),rect(1)+w,rect(1)+w,rect(1);rect(2),rect(2),rect(2)+h,rect(2)+h];
- rectangle('Position',[rect(1),rect(2),w,h], 'edgecolor','red');
- figure(2);
- bw = roipoly(A,r(1,:),r(2,:));
- AA = reshape(A, [(size(A,1)*size(A,2)), size(A,3)]);
- BB = AA(bw, :);
- B = reshape(BB, [h, w, size(A,3)]);
- imshow(B);
- end
- %%%%任意形状区域选取%%%%%
- clear all
- A = imread('board.tif');
- A = rgb2gray(A); %-此句注释后下面的处理就相当于对真彩色图像进行
- figure(1)
- imshow(A);
- %[a,b] = ginput;
- button = 1;
- n = 0;
- a = [];
- b = [];
- hold on;
- while button == 1 % 输入数据点,以右键作结
- [xi,yi,button] = ginput(1);
- n = n + 1;
- if n > 1
- plot([xi,a(n-1,1)], [yi,b(n-1,1)], '-rs','LineWidth',1,...
- 'MarkerEdgeColor','k',...
- 'MarkerFaceColor','g',...
- 'MarkerSize',10);
- else
- plot(xi, yi, 's','LineWidth',2,...
- 'MarkerEdgeColor','k',...
- 'MarkerFaceColor','g',...
- 'MarkerSize',10);
- end
- a(n,1) = xi;
- b(n,1) = yi;
- end
- plot([xi,a(1,1)], [yi,b(1,1)], '-rs','LineWidth',2,...
- 'MarkerEdgeColor','k',...
- 'MarkerFaceColor','g',...
- 'MarkerSize',10);
- hold off;
- if length(a) >= 3
- va = round(a);
- vb = round(b);
- x = 1:size(A,1);
- y = 1:size(A,2);
- [Y,X] = meshgrid(y,x);
- in = inpolygon(Y(:),X(:),va,vb);
- AA = reshape(A, [size(A,1)*size(A,2),size(A,3)]);
- BB = repmat(uint8(255),size(AA));
- BB(in,:) = AA(in,:);
- B = reshape(BB, [size(A,1),size(A,2),size(A,3)]);
- figure(2)
- imshow(B);
- end
复制代码
选自eight总结贴。
选出感兴趣的区域后,把figure文件保存为需要的格式即可。 |