C语言实现raw格式图像的读入和存取

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dan15188387481/article/details/49766111

C语言实现raw格式图像的读入和存取


    raw格式是在生活中比较少见的图像格式,但是它作为一种相机的原始图像数据,在图像处理领域用处很多。raw格式的图像相当于就是一个二进制流,所有图像数据按顺序单字节存放,中间没有任何间隔,自然也不存在所谓的每一行一个回车,它的每个图像数据都是紧挨着的,读取的时候必须自己按照图像的分辨率进行存取,放在二维数组中的情况居多,当存取到二维数组中时才有了行和列的概念。下面给出C语言实现的读入和存取raw图像。


   
   
  1. /*========================================================================*/
  2. //
  3. // Description: 针对RAW图像的读入和存取
  4. //
  5. // Arguments:
  6. //
  7. // Returns:
  8. //
  9. // Notes: none
  10. //
  11. // Time: none
  12. //
  13. // Memory: none
  14. //
  15. // Example: none
  16. //
  17. // History: 1. wangyi 2014-4-19 22:46 Verison1.00 create
  18. /*========================================================================*/
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #define height 256
  23. #define width 256
  24. typedef unsigned char BYTE; // 定义BYTE类型,占1个字节
  25. int main()
  26. {
  27. FILE *fp = NULL;
  28. BYTE B[height][width];
  29. BYTE *ptr;
  30. char path[ 256];
  31. char outpath[ 256];
  32. int i,j;
  33. // 输入源路径并打开raw图像文件
  34. printf( "Input the raw image path: ");
  35. scanf( "%s",path);
  36. if((fp = fopen( path, "rb" )) == NULL)
  37. {
  38. printf( "can not open the raw image " );
  39. return;
  40. }
  41. else
  42. {
  43. printf( "read OK");
  44. }
  45. // 分配内存并将图像读到二维数组中
  46. ptr = (BYTE*) malloc( width * height * sizeof(BYTE) );
  47. for( i = 0; i < height; i++ )
  48. {
  49. for( j = 0; j < width ; j ++ )
  50. {
  51. fread( ptr, 1, 1, fp );
  52. B[i][j]= *ptr; // 把图像输入到2维数组中,变成矩阵型式
  53. printf( "%d ",B[i][j]);
  54. ptr++;
  55. }
  56. }
  57. fclose(fp);
  58. // 这里可以对二维数组中的图像数据进行处理
  59. // 将处理后的图像数据输出至文件
  60. printf( "Input the raw_image path for save: ");
  61. scanf( "%s",outpath);
  62. if( ( fp = fopen( outpath, "wb" ) ) == NULL )
  63. {
  64. printf( "can not create the raw_image : %s\n", outpath );
  65. return;
  66. }
  67. for( i = 0; i < height; i++ )
  68. {
  69. for( j = 0; j < width ; j ++ )
  70. {
  71. fwrite( &B[i][j], 1 , 1, fp );
  72. }
  73. }
  74. fclose(fp);
  75. }

      上述程序实现了读入和存取的功能,中间可以自己加入对图像数据的处理算法,如注释中所述即可。

    总之,raw格式的图像一般情况下不能直接打开,需要有专门的工具才能打开,大家可以使用程序对其数据进行读写,从而实现图像算法处理的过程。
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dan15188387481/article/details/49766111

猜你喜欢

转载自blog.csdn.net/monk1992/article/details/82883046
今日推荐