使用 imwrite 创建的文件不出现

发布于 2024-11-28 21:11:50 字数 895 浏览 2 评论 0原文

Matlab 新手,如果这是一个愚蠢的问题,我很抱歉。我正在为我的研究过滤一系列图像。我在实际图像处理方面没有遇到问题,但当我去保存修改后的图像时遇到了麻烦。由于某种原因,我只能使用 imwrite 将修改后的图像保存为 .gif 文件。如果我尝试将它们另存为 .jpg、.bmp 等,该文件不会出现在工作文件夹中。出现相应的通用文件,但实际的 .jpg 没有出现。此外,当我使用 imread 重新打开中间文件(实际上保存为 .gif)时,图像只是黑色。但是,如果我在 Matlab 之外打开 .gif 文件,它会按预期显示。代码如下。

close all
N=90;
IMAGES=cell(1,N); %creates a cell to store image data
FNAMEFMT='20110805115033(1)_%d.jpg';

for i=1:N
    IMAGES{i}=imread(sprintf(FNAMEFMT,i)); %reads original images into IMAGES
end

RESULT=cell(1,N); %to store modified/filtered images

for i=1:N
    gray=rgb2gray(IMAGES{i}); %converts to grayscale
    binary=im2bw(gray,.5); %converts to bw
    filter=bwareaopen(binary,35); %removes small features
    RESULT{i}=filter; %saves modified image in RESULTS
end

for i=1:N
    WRITEFMT='filter_%d';
    imwrite(RESULT{i},sprintf(WRITEFMT,i),'gif'); %writes RESULTS as .gif
end

New to Matlab, so sorry if this is a silly question. I'm filtering a series images for my research. I'm not having a problem with the actual image processing, it's when I go to save the modified images that I run into trouble. For some reason, I can only save the modified images using imwrite as .gif files. If I try to save them as .jpg, .bmp, etc., the file does not appear in the working folder. The corresponding generic file appears, but the actual .jpg does not. Additionally, when I use imread to reopen the midified files (that actually saved as .gifs), the image is just black. But, if I open the .gif file outside Matlab, it appears as expected. Code below.

close all
N=90;
IMAGES=cell(1,N); %creates a cell to store image data
FNAMEFMT='20110805115033(1)_%d.jpg';

for i=1:N
    IMAGES{i}=imread(sprintf(FNAMEFMT,i)); %reads original images into IMAGES
end

RESULT=cell(1,N); %to store modified/filtered images

for i=1:N
    gray=rgb2gray(IMAGES{i}); %converts to grayscale
    binary=im2bw(gray,.5); %converts to bw
    filter=bwareaopen(binary,35); %removes small features
    RESULT{i}=filter; %saves modified image in RESULTS
end

for i=1:N
    WRITEFMT='filter_%d';
    imwrite(RESULT{i},sprintf(WRITEFMT,i),'gif'); %writes RESULTS as .gif
end

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

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

发布评论

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

评论(1

心如狂蝶 2024-12-05 21:11:50

如果我尝试将它们另存为 .jpg、.bmp 等,该文件不会出现在工作文件夹中。

您需要更改

WRITEFMT='filter_%d';

WRITEFMT='filter_%d.jpg';

您输出的文件是 jpeg 文件(根据 imwrite 参数 'jpg' 而不是 'gif'),但它们没有文件扩展名。如果您手动添加扩展名,它们会以 jpg 格式打开。

对于黑色 gif,看看这是否有帮助

导出为 jpg 后,即可查看它们

imshow(imread('filter_1.jpg'))

If I try to save them as .jpg, .bmp, etc., the file does not appear in the working folder.

You need to change

WRITEFMT='filter_%d';

to

WRITEFMT='filter_%d.jpg';

The files you are outputting are jpeg files (as per the imwrite argument 'jpg' instead of 'gif'), but they don't have a file extension. If you manually add the extension, they open as jpgs.

For the black gif, see if this helps.

Once you export as jpg, viewing them works

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