将图像读入数组? [英] Reading an image into an array?

查看:173
本文介绍了将图像读入数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm attempting to write a program that utilizes the sobel filter to detect edges in images. So first off, I've written down some of the requirements in some basic code, such as the x and y direction filters as arrays and also an attempt to read in a pgm image:

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文件之外.读取图像的代码是我以前打印出的图像的副本,但我将写入改为读取.

This is my first time working with image manipulation in FORTRAN, apart from once when I printed an image out as a pbm file. The code for reading the image in is a replicate of what I used to print one out before, except I changed write to read.

所以我的问题是,如何将pgm格式的图像读入"inp"数组,以便可以应用sobel过滤器?进行尝试时,出现以下错误:

So my question is, how can I read in an image that's in pgm format into the "inp" array, so that I can apply the sobel filter? When I run my attempt, I get the following errors:

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)

谢谢

推荐答案

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

The first mistake is, as the compiler states quite clearly, in this line:

read (out_unit,11) 'P2'

编译器希望,因为这是Fortran的定义方式,因此会被告知将out_unit中的值读入变量,但是'P2'是字符文字(或标准调用的内容),这是一个字符串,不是一个变量.

The compiler expects, because that's the way that Fortran is defined, to be told to read a value from out_unit into a variable, but 'P2' is a character literal (or whatever the heck the standard calls them), it's a string, it's not a variable.

下一行也发生相同的错误.编译器期望像

The same mistake occurs in the next lines too. The compiler expects something like

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

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

etc. After execution of this version of the read statement the variable p2 holds the magic number read from the pgm file.

我正在写的时候,调用要从out_unit中读取的单元只是错误的,最终会使您感到困惑.

While I'm writing, calling the unit to be read from out_unit is just perverse and will, eventually, confuse you.

这篇关于将图像读入数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆