【发布时间】: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 开始的数组中。 ?