读取、写入和查询图像文件

读取、写入和查询图像文件

划分图形图像的分集(裁剪)有时候您只想使用图像文件的一部分或要将其划分成几个子区域。在命令行中,指定您想要使用的矩形子区域的内部坐标,并将它保存到文件中。如果您不知道子区域的各边角点的坐标,则以交互的方式进行选择,正如以下示例所示:

% Read RGB image from graphics file.

im = imread('street2.jpg');

% Display image with true aspect ratio

image(im); axis image

% Use ginput to select corner points of a rectangular

% region by pointing and clicking the mouse twice

p = ginput(2);

% Get the x and y corner coordinates as integers

sp(1) = min(floor(p(1)), floor(p(2))); %xmin

sp(2) = min(floor(p(3)), floor(p(4))); %ymin

sp(3) = max(ceil(p(1)), ceil(p(2))); %xmax

sp(4) = max(ceil(p(3)), ceil(p(4))); %ymax

% Index into the original image to create the new image

MM = im(sp(2):sp(4), sp(1): sp(3),:);

% Display the subsetted image with appropriate axis ratio

figure; image(MM); axis image

% Write image to graphics file.

imwrite(MM,'street2_cropped.tif') 如果您知道图像边角点的坐标,那么在上例中,您可以手动定义 sp 而不必使用 ginput。

您还可以在对图像执行交互操作时,显示一个“橡皮筋框”以划分图像的子集。有关详细信息,请参阅 rbbox 代码示例。有关详细信息,请参阅 ginput 和 image 函数的文档。

相关探索