自动裁剪图片

发布于 2024-10-27 15:22:47 字数 178 浏览 6 评论 0原文

我有很多由黑色背景物体组成的图片。我用 Matlab 创建了它们,它们的图片周围都有一个白色的“额外”。我希望能够自动裁剪它们,以便图片不会有多余的白色。

文件为 .tif 格式

这里以图片为例:

>一开始可能不太明显,但如果您选择图片,它会比黑色区域大得多。

I have many pictures that are made of an object with black background. I've created them with Matlab, and they all have a white "extra" surrounding the picture. I want to be able to automatically crop them so that the picture will not have the white extra.

The files are .tif format

Here is a picture as an example:enter image description here

It may be not noticeable at first, but if you select the picture, it's much bigger than just the black area.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九八野马 2024-11-03 15:22:47

您可以简单地丢弃只有行和列的行和列。

%# read the image
[img,map] = imread('https://i.sstatic.net/Yyz8Z.png');

%# keep only rows/cols that have values other than 1
img = img(any(img<1,2),any(img<1,1));

对于 RGB tiff,您可以使用(假设 tiff 图像中的最大值为 86,如 png)

img = img(~all(all(img==max(img(:)),3),2),~all(all(img==max(img(:)),3),1),:);

编辑

有几种方法可以确保您不会在图像中出现边框首位。例如,如果要保存 M×N×3 RGB 图像数组,可以使用 IMWRITE 进行书写

imwrite(imageArray,'myFile.tif')

或者,如果您想保存没有边框的图形,您可以调用 GETFRAME 在轴句柄上获取图像:

axesHandle = gca; %# to get the axes handle of the current figure
f = getframe(axesHandle);
imwrite(f.cdata,'myFile.tif'); 

如果你想用抗锯齿保存你的图形,你可以使用优秀的 export_fig 来自文件交换。

You can simply throw away the rows and columns in which you have only ones.

%# read the image
[img,map] = imread('https://i.sstatic.net/Yyz8Z.png');

%# keep only rows/cols that have values other than 1
img = img(any(img<1,2),any(img<1,1));

For RGB tiffs, you can use (assuming that the max value in the tiff image is 86, as in the png)

img = img(~all(all(img==max(img(:)),3),2),~all(all(img==max(img(:)),3),1),:);

EDIT

There are a few ways to ensure that you don't get the border in the first place. For example, if you want to save an M-by-N-by-3 RGB image array, you can use IMWRITE to write

imwrite(imageArray,'myFile.tif')

Alterantively, if you have a figure you'd like to save without the border, you can call GETFRAME on the axes handle to obtain the image:

axesHandle = gca; %# to get the axes handle of the current figure
f = getframe(axesHandle);
imwrite(f.cdata,'myFile.tif'); 

If you want to save your figure with anti-aliasing, you can use the excellent export_fig from the file exchange.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文