【问题标题】:Reading an image into an array?将图像读入数组?
【发布时间】:2018-08-11 17:28:56
【问题描述】:

我正在尝试编写一个程序,该程序利用 sobel 过滤器来检测图像中的边缘。所以首先,我已经在一些基本代码中写下了一些要求,例如将 x 和 y 方向过滤器作为数组以及尝试读取 pgm 图像:

program edges
implicit none
integer, dimension(:,:), allocatable :: inp, outim, GX, GY
integer, parameter :: dp = selected_real_kind(15,300)
integer :: ky, kx, x, y, out_unit = 10, M, N, sx, sy, i, j
real(kind=dp) :: G
M = 5
N = 5

allocate(inp(M,N))
allocate(outim(M-2,N-2))
allocate(GX(3,3))
allocate(GY(3,3))

open(file = 'clown.pgm',unit=out_unit,status= 'unknown') !opening file to         write to inp
read (out_unit,11) 'P2'      !pgm magic number
read (out_unit,12) 50,50     !width, height
read (out_unit,13) 1      !max gray value
do M=-25,25
    do N=-25,25
        read (out_unit,*) inp(M,N)
    end do
end do
11 format(a2)
12 format(i3,1x,i3)
13 format(i5)

这是我第一次在 FORTRAN 中使用图像处理,除了一次我将图像打印为 pbm 文件。读取图像的代码是我之前打印出来的代码的复制品,除了我将写入更改为读取。

所以我的问题是,如何将 pgm 格式的图像读入“inp”数组,以便应用 sobel 过滤器?当我运行我的尝试时,我收到以下错误:

read (out_unit,11) 'P2'      !pgm magic number
              1
Error: Expected variable in READ statement at (1)
sobel.f90:41:18:

 read (out_unit,12) 50,50     !width, height
              1
Error: Expected variable in READ statement at (1)
sobel.f90:42:18:

 read (out_unit,13) 1      !max gray value
              1
Error: Expected variable in READ statement at (1)

谢谢

【问题讨论】:

  • @HighPerformanceMark 谢谢,我现在已经减少了它并添加了我遇到的错误。
  • 好吧,我们无法读入文字常量。你只是想验证 - 检查第一行是“P2”等吗?
  • 您分配了一个 5x5 的数组,显然认为图像是 50x50,然后尝试将 51x51 的值读取到索引从 -25 开始的数组中。 ?

标签: fortran gfortran sobel


【解决方案1】:

第一个错误是,正如编译器非常清楚地指出的那样,在这一行:

read (out_unit,11) 'P2'

编译器期望,因为这是定义 Fortran 的方式,被告知将值从 out_unit 读取到变量中,但 'P2' 是字符文字(或者标准调用它们的任何内容),它是一个字符串,它不是一个变量。

同样的错误也出现在下一行。编译器期望类似

integer :: p2  ! pgm magic number
...
read (out_unit,11) p2

。执行此版本的 read 语句后,变量 p2 保存从 pgm 文件中读取的幻数。

在我写作的时候,从out_unit 调用要读取的单元是不正当的,最终会让你感到困惑。

【讨论】:

  • 谢谢。因此,要解决“P2”问题,我是否只需在代码开头将其声明为变量?
  • 我的错误,我只为输入 pgm 而不是输出,因此我收到了同样的错误。这将如何应用于其他 2,即宽度/高度和最大灰度值?我是否将它们声明为上面的变量,评估它们然后使用它们?谢谢。
  • 好的,我已经解决了。我现在得到的错误是:“在 sobel.f90 (unit=10, file = "clown.pgm") Fortran 运行时错误的第 43 行:文件结束”
猜你喜欢
  • 2011-08-17
  • 1970-01-01
  • 2013-02-20
  • 1970-01-01
  • 2021-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多