简体   繁体   English

在MATLAB中读取.img文件并

[英]Read .img file in MATLAB and

I am quite new to MATLAB. 我对MATLAB很陌生。 I want to read a .img file in MATLAB and want to export it is any generic image format. 我想在MATLAB中读取.img文件,并希望将其导出为任何通用图像格式。 I don't have any information about the file (ie image resolution) I tried reading the file using fread command. 我没有有关该文件的任何信息(即图像分辨率),我尝试使用fread命令读取该文件。

fid=fopen('image.img');
A=fread(fid,256*378,'unit8=>unit8');
fclose(fid);
b=reshape(A,256,378);
imshow(A);

I'm able to open it only if I know the resolution of my .img file. 仅当我知道我的.img文件的分辨率时,才能打开它。 Please help me do the same without knowing the resolution to start with or if anyone can suggest a better way to do it. 请帮助我做同样的事情,而不知道开始的分辨率,或者是否有人可以提出更好的解决方法。

Download the .img file here . 此处下载.img文件。

It is not clear what kind of .img file you are dealing with. 目前尚不清楚您正在处理哪种.img文件。 .img files are normally disk image files. .img文件通常是磁盘映像文件。 There are different file formats for Mac, Windows and ISO. Mac,Windows和ISO有不同的文件格式。 However, it sounds like you are asking about a graphic image file format, which might be proprietary. 但是,听起来您正在询问图形图像文件格式,该格式可能是专有的。 If you know what program was used to create the .img file, you may be able to learn more about its internal format. 如果您知道用于创建.img文件的程序,则可以了解有关其内部格式的更多信息。

The dir function can be used to determine the total number of bytes in the file, which will allow you to read the file using fread. dir函数可用于确定文件中的字节总数,这使您可以使用fread读取文件。 However, you would still need to know the dimensions to reshape the image. 但是,您仍然需要知道尺寸来重塑图像。

If you want to process images, Matlab has a nice Image Processing Toolbox and I strongly suggest you to read the help/manual. 如果您想处理图像,Matlab有一个不错的图像处理工具箱,强烈建议您阅读帮助/手册。 You'll find several built-in functions ready-to-use that can sort you out. 您会发现几个可以立即使用的内置函数,可以对您进行分类。

imageinfo() is one of that: will show a structure with several infos regarding the image, imread() will import your image in a matrix form, mat2grey() will convert your RGB image in a greyscale image...and the list goes on. imageinfo()就是其中之一:将显示包含有关该图像的多个信息的结构, imread()将以矩阵形式导入您的图像, mat2grey()将您的RGB图像转换为灰度图像...上。

Do not use fopen() / fread() / fclose() with images: such functions are mainly used to deal with text files. 不要对图像使用fopen() / fread() / fclose() :此类函数主要用于处理文本文件。

PS: the link you provided contains a disk image (of course, given that extension, as @Herb correctly pointed out). PS:您提供的链接包含磁盘映像(当然,给定扩展名,如@Herb正确指出的那样)。

The sintax of fread() function in your code is wrong. 代码中的fread()函数的fread()错误。

First, the datatype is uint8 not unit8 . 首先,数据类型是uint8而不是unit8

  • U is unsigned, which means: All values are positive (no negative "sign" allowed) U是无符号的,这意味着:所有值都是正的(不允许使用负的“符号”)

  • INT is integers, which means: All values are whole numbers, 0,1,2,3... INT是整数,表示:所有值都是整数,0、1、2、3 ...

  • 8 is Only 8 bits of information, which means: the max value is 255 and the min value is 0. 8仅是8位信息,表示:最大值为255,最小值为0。

Second, the 256*378 argument in the function fread() will create an another dimension of this image. 其次,函数fread()256*378参数将创建此图像的另一个尺寸。 Thus the correct way of this is using [] . 因此,正确的方法是使用[]

f = fopen('image.img')
image = fread(f, [256 378], '*uint8');
imshow(image,[])

达芬奇

imwrite(image, 'davinci.jpg', 'jpg')
whos image
Variables in the current scope:

   Attr Name          Size                     Bytes  Class
   ==== ====          ====                     =====  =====
        image         256x378                  96768  uint8

Total is 96768 elements using 96768 bytes

This question may be a little bit equally with this question 这个问题可能和这个问题有点相同

  • references 1 参考文献1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM