Image read in MATLAB and C

3 次查看(过去 30 天)
Pravitha A
Pravitha A 2020-2-20
评论: Pravitha A 2020-2-25
I have program to read an image in MATLAB
% open image file
fd = fopen("i00.dng");
% read order
order = fread(fd, 1, 'uint16');
I converted this to C as follows:
int order[1234];
FILE *fd=fopen("i00.dng","wb");
fread(order,2,1,fd);
printf("%d",order);
But the value of order in both cases is different. Did I convert it wrong?What is the exact replica of this code in C?

回答(1 个)

James Tursa
James Tursa 2020-2-20
编辑:James Tursa 2020-2-20
An int is likely 4 bytes on your machine, not 2 bytes. Also the expression "order" by itself evaluates as a pointer to the first element, not the first element itself. And you should be opening the file for reading, not writing. So,
unsigned short order[1234];
FILE *fd = fopen("i00.dng","rb"); /* you should check that this result is not NULL */
fread(order,2,1,fd);
printf("%u\n",order[0]);
This is almost a replica of the MATLAB code. Your MATLAB code converts the input to double. If you don't want that, then use an asterisk on the class name:
order = fread(fd, 1, '*uint16');
  5 个评论
Walter Roberson
Walter Roberson 2020-2-25
If you want big endian then you need to pass in the third parameter to fopen as 'b' or 'ieee-be'. Or you can instead pass that information as the fifth parameter to fread(). Or you can swapbytes() the value that you fread.
Pravitha A
Pravitha A 2020-2-25
like this??
fd=fopen("filename","rb",'ieee-be')

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by