c菜鸟,求高手指点
前几天发了个帖子求助,是因为一个指针的类型不对的问题。现在我把程序修改了,并加入了简单的处理部分,程序编译无错,执行也ok。
可是保存的是空图片,限于水平,查了很久没查出bug,所以求高手指点迷津。
程序:
#include <stdio.h>
#include<stdlib.h>
#include<math.h> //因为有开方,居然没加math。。。
#define X_SIZE 484 /*图像的横宽 */
#define Y_SIZE 592 /*图像的纵长 */
#define X_EXP 7 /* X_EXP=log2(X_SIZE) */
#define Y_EXP 7 /*Y_EXP=log2(X_SIZE) */
#define HIGH 255 /*2值图像的高端值 */
#define LOW 0 /*2值图像的低端值 */
int image_read(unsigned char image[Y_SIZE][X_SIZE], FILE* fp);
int image_write(unsigned char image[Y_SIZE][X_SIZE], FILE* fp);
void gradient(unsigned char image_in[Y_SIZE][X_SIZE],unsigned char
image_out[Y_SIZE][X_SIZE], float amp);
int main ( )
{
FILE * fp=NULL; //先命名指针和定义数组
unsigned char image_in[Y_SIZE][X_SIZE];
unsigned char image_out[Y_SIZE][X_SIZE];
float amp = 10.0000;
if((fp=fopen("c:\\test.bmp","rb"))==NULL){
printf("read the bmp file has failed\n");
return 1;
}
image_read(image_in, fp); //图像读取部分
if((fp=fopen("c:\\test0.bmp","wb"))==NULL){
printf("write the bmp file has failed\n");
return 1;
}
image_write(image_out, fp); //图像存储部分
return 0;
}
int image_read(unsigned char image[Y_SIZE][X_SIZE], FILE* fp)
{
//FILE *fp;
fp = fopen("test.bmp", "rb");
if(NULL ==fp)
return -1;
else {
fread(image, (size_t)X_SIZE, (size_t)Y_SIZE, fp);
fclose(fp);
return 0;
}
}
int image_write(unsigned char image[Y_SIZE][X_SIZE], FILE* fp)
{
fp = fopen ("test0.bmp", "wb");
if (NULL == fp)
return -1;
else {
fwrite(image, (size_t)X_SIZE, (size_t)Y_SIZE, fp);
fclose(fp);
return 0;
}
}
void gradient(unsigned char image_in[Y_SIZE][X_SIZE],unsigned char
image_out[Y_SIZE][X_SIZE], float amp) //用一阶微分提取轮廓线
{
static int cx[9] ={0,0,0,
0,1,0,
0,0,-1};
static int cy[9] ={0,0,0,
0,0,1,
0,-1,0};
int d[9];
int i, j, dat;
float xx, yy, zz;
for (i =1; i<Y_SIZE-1; i++) {
for (j =1; j<X_SIZE-1; j++) {
d[0] = image_in[i-1][j-1];
d[1] = image_in[i-1][j];
d[2] = image_in[i-1][j+1];
d[3] = image_in[i][j-1];
d[4] = image_in[i][j];
d[5] = image_in[i][j+1];
d[6] = image_in[i+1][j-1];
d[7] = image_in[i+1][j];
d[8] = image_in[i+1][j+1];
xx = (float) (cx[0]*d[0] + cx[1]*d[1] + cx[2]*d[2]
+ cx[3]*d[3] + cx[4]*d[4] + cx[5]*d[5]
+ cx[6]*d[6] + cx[7]*d[7] + cx[8]*d[8] );
yy = (float) (cy[0]*d[0] + cy[1]*d[1] + cy[2]*d[2]
+ cy[3]*d[3] + cy[4]*d[4] + cy[5]*d[5]
+ cy[6]*d[6] + cy[7]*d[7] + cy[8]*d[8] );
zz = (float) (amp*sqrt (xx*yy+yy*yy));
dat = (int)zz;
if(dat > 255) dat =255;
image_out[i][j] =(char)dat;
}
}
}