內容提要
如果一幅圖中只有一小部分圖像你感興趣(你想研究的部分),那么截圖工具就可以了,但是如果你想知道這個區域在原圖像中的坐標位置呢? 這可是截圖工具所辦不到的,前段時間我就需要這個功能,於是將其用Matlab實現。
其實只要用到Matlab中的兩個函數:
- 函數:
imrect
- 函數:
getPosition
如果要截取其中的部分圖像,就離不開下面的函數:
- 函數:
imcrop
代碼實現
clc;
clear;
close all;
%---------------------------------------- % Matlab截圖程序 %---------------------------------------- [filename, pathname] = uigetfile({'*.jpg'; '*.bmp'; '*.gif'; '*.png' }, '選擇圖片'); %沒有圖像 if filename == 0 return; end src = imread([pathname, filename]); [m, n, z] = size(src); figure(1) imshow(src)%顯示原圖像 %---------------------------------------- %畫圖后: h=imrect;%鼠標變成十字,用來選取感興趣區域 %---------------------------------------- %圖中就會出現可以拖動以及改變大小的矩形框,選好位置后: %---------------------------------------- pos=getPosition(h); %---------------------------------------- %pos有四個值,分別是矩形框的左下角點的坐標 x y 和 框的 寬度和高度 %---------------------------------------- %---------------------------------------- %拷貝選取圖片 %---------------------------------------- imCp = imcrop( src, pos ); figure(2) imshow(imCp);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
效果展示
圖中的區域信息為:
pos =
255.8263 65.1737 87.5789 87.5789
% x y dx dy %左上角點(x,y), 區域長寬(dx,dy)
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
不要忘了圖像坐標系的x和y的方向
from: http://blog.csdn.net/humanking7/article/details/46822349