| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 439 人关注过本帖
标题:c菜鸟,求高手指点
只看楼主 加入收藏
madrabbit
Rank: 1
等 级:新手上路
帖 子:11
专家分:2
注 册:2010-1-11
结帖率:100%
收藏
 问题点数:0 回复次数:2 
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;
              }
         }
    }
        
        
搜索更多相关主题的帖子: 天发 图片 include 
2010-07-25 12:08
madrabbit
Rank: 1
等 级:新手上路
帖 子:11
专家分:2
注 册:2010-1-11
收藏
得分:0 
等待
2010-07-25 12:26
madrabbit
Rank: 1
等 级:新手上路
帖 子:11
专家分:2
注 册:2010-1-11
收藏
得分:0 
恩,好像有人说是应为主函数中的地址和子函数中的不一致,
我将image_read 和image_write中的地址改为C:\\test.bmp和c:\\test0.bmp之后就可以保存了
2010-07-25 21:22
快速回复:c菜鸟,求高手指点
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016640 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved