对uchar 类型图象数据添加误码

作者在 2010-08-08 11:01:22 发布以下内容


 //  10  -4,-5,-6

 
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
#include <dos.h>

// 对uchar 类型图象数据添加误码
void Add_Error_Code(char *in_fname,char *out_fname,short r,short c,
    double err_rate,short seed)
{
    FILE *stream;
    unsigned char *buffer ,out_byte;
    unsigned int length, k;
    char plane; // 误码平面
    short i,err_num ;
    // 误码率 err_rate :(1.0e-4, 1.0e-6)

    if( NULL == (buffer=(unsigned char*)malloc(sizeof(unsigned char)*r*c)) )
    {
        fprintf(stdout, "Merory Exhausted\n");
        return ;
    }

    // 读取数据        
    if( (stream=fopen(in_fname,"rb")) == NULL )
    {
        fprintf(stdout, "Cannot open the file %s.\n",in_fname);
        return ;
    }
    k = 0;
    while( fread(&out_byte,sizeof(unsigned char),1,stream) != EOF )
    {
        buffer[k] = out_byte;
        k++;
    }
    fclose(stream);
    length = k;

    srand(seed);
    err_num = (short)floor(8.0*length*err_rate + 0.5); // 误码比特数
    for(i=0;i<err_num;i++)
    {
        k = (unsigned int)floor((rand()/32768.0)*length);
        plane = rand()/(8*sizeof(unsigned char)); // 指定误码出现的位平面
        printf("%d\t",k);
        buffer[k] ^= (1 << plane);
    }
    
    // 存放带误码数据        
    if( (stream=fopen(out_fname,"wb")) == NULL )
    {
        fprintf(stdout, "Cannot open the file %s.\n",out_fname);
        return ;
    }  
    for (k=0;k<length;k++)
        fwrite(&(buffer[k]),sizeof(unsigned char),1,stream);
    fclose(stream);
    
    free(buffer);
}

int main(int argc, char **argv)
{
    
     char in_fname[60],out_fname[60];
    unsigned char *file_buff;
 
    short  seed;
    double  err_rate;
    FILE *fp_in ;
    
    if(argc!=5){
        printf("\n");
        printf("usage:\n");
        printf("name infname outfname seed err_rate\n");
        exit(0);
    }
    strcpy(in_fname,argv[1]);
    strcpy(out_fname,argv[2]);
    seed =atoi(argv[3]);
    err_rate =atof(argv[4]);
    
    
    /*for(i=0;i<100;i++)
     printf("%f ",rand()/32768.0);
     getch();
     */
    
    file_buff=(unsigned char*)malloc(sizeof(unsigned char)*4096*4096);
    
    fp_in=fopen(in_fname,"rb");
            
    Add_Error_Code(in_fname,out_fname,512,512,err_rate,seed);      
    

}
图像 | 阅读 2367 次
文章评论,共2条
vfdff(作者)
2010-08-08 11:02
1
// 两 raw 图像的信噪比比较 <br />
<br />
<br />
#include &lt;stdio.h&gt;<br />
#include &lt;assert.h&gt;<br />
#include &lt;math.h&gt;<br />
#include &lt;dos.h&gt;<br />
#include &lt;io.h&gt;<br />
#include &lt;fcntl.h&gt;<br />
#include &lt;fcntl.h&gt;<br />
#include &lt;minmax.h&gt;<br />
<br />
/*------------------Functions for data compare.-----------------------------*/<br />
double File_Psnr(const char *fpath1, const char *fpath2)<br />
{&nbsp; &nbsp; &nbsp; &nbsp; // compare two file to get the PSNR,UChar data are required<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; assert( (fpath1 != NULL) || (fpath2 != NULL) );<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; int handle1 = open(fpath1, O_RDONLY ); <br />
&nbsp; &nbsp; &nbsp; &nbsp; int handle2 = open(fpath2, O_RDONLY );<br />
&nbsp; &nbsp; &nbsp; &nbsp; if ( (handle1==-1)||(handle2==-1) )<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;Error open file %s or %s:&quot;,fpath1,fpath2);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1.0;<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; long minFileLength = min(filelength(handle2),filelength(handle1));<br />
&nbsp; &nbsp; &nbsp; &nbsp; long maxFileLength = max(filelength(handle2),filelength(handle1));<br />
&nbsp; &nbsp; &nbsp; &nbsp; // printf(&quot;\nminFileLength=%ld,maxFileLength=%ld\n&quot;,minFileLength,maxFileLength);<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; unsigned char *array1 = new unsigned char[minFileLength];<br />
&nbsp; &nbsp; &nbsp; &nbsp; unsigned char *array2 = new unsigned char[minFileLength];<br />
&nbsp; &nbsp; &nbsp; &nbsp; /* read minFileLength chars from the file */<br />
&nbsp; &nbsp; &nbsp; &nbsp; read(handle1,array1, minFileLength);<br />
&nbsp; &nbsp; &nbsp; &nbsp; read(handle2,array2, minFileLength);<br />
&nbsp; &nbsp; &nbsp; &nbsp; close(handle1);<br />
&nbsp; &nbsp; &nbsp; &nbsp; close(handle2);<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; long Length = 0;<br />
&nbsp; &nbsp; &nbsp; &nbsp; double mse = 0.0;<br />
&nbsp; &nbsp; &nbsp; &nbsp; signed short temp = 0;<br />
&nbsp; &nbsp; &nbsp; &nbsp; do {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = array1[Length]-array2[Length];<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mse += (double)temp*temp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Length++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //putch(ch1); putch(ch2);<br />
&nbsp; &nbsp; &nbsp; &nbsp; } while ( Length &lt; minFileLength ); // 至少一个文件到文件尾<br />
&nbsp; &nbsp; &nbsp; &nbsp; delete [] array1;<br />
&nbsp; &nbsp; &nbsp; &nbsp; delete [] array2;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; if( mse &lt; 0.1e-6)<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;\nThe two file are the same!\n&quot;);<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; mse = mse / (1.0*maxFileLength);<br />
&nbsp; &nbsp; &nbsp; &nbsp; // printf(&quot;\n[%g],%ld&quot;,mse,maxFileLength);<br />
&nbsp; &nbsp; double psnr = 10 * log10(255.0*255.0/mse);<br />
&nbsp; &nbsp; &nbsp; &nbsp; return psnr;<br />
}<br />
<br />
<br />
void main(short argc,char *argv[])<br />
{<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; double psnr;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; char *fname1 = &quot;F:\\program\\Microsoft Visual Studio\\MyProjects\\Jpeg2000\\map.raw&quot;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; char *fname2 = &quot;F:\\program\\Microsoft Visual Studio\\MyProjects\\Jpeg2000\\postmap.raw&quot;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; psnr = File_Psnr(fname1,fname2);<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;\npsnr=%.6Lf\n&quot;,psnr);<br />
<br />
}
vfdff(作者)
2010-08-08 12:02
2
// 为每2个字符之间增加空格 <br />
<br />
#include &lt;stdlib.h&gt;<br />
#include &lt;string.h&gt;<br />
#include &lt;stdio.h&gt;<br />
#include &lt;VCLIB.h&gt; //&quot;../VCLIB.h&quot;<br />
<br />
int main(short argc,char *argv[])<br />
{<br />
&nbsp; &nbsp; char in_fname[60],out_fname[60];<br />
&nbsp; &nbsp; unsigned char **in_img;<br />
&nbsp; &nbsp; short i=0,j=0,x_size,y_size;<br />
&nbsp; &nbsp; short **tile_img;<br />
&nbsp; &nbsp; FILE *pOutFile = NULL;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; if(argc != 5){<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;printf(&quot;usage:\n&quot;);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;printf(&quot;name infname outfname x_size y_size\n&quot;);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;exit(0);<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; strcpy(in_fname,argv[1]);<br />
&nbsp; &nbsp; strcpy(out_fname,argv[2]);<br />
&nbsp; &nbsp; x_size = atoi(argv[3]);<br />
&nbsp; &nbsp; y_size = atoi(argv[4]);<br />
&nbsp; &nbsp; pOutFile = fopen(out_fname,&quot;wb&quot;);<br />
&nbsp; &nbsp; in_img =Alloc_UChar_Img(x_size,y_size);<br />
&nbsp; &nbsp; tile_img=Alloc_Short_Img(x_size,y_size);<br />
&nbsp; &nbsp; File_To_UChar_Img(in_fname,in_img,x_size,y_size);<br />
&nbsp; &nbsp; for(i=0;i&lt;y_size;i=i++)<br />
&nbsp; &nbsp; for(j=0;j&lt;x_size;j=j++)<br />
&nbsp; &nbsp; { <br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;tile_img<i>[j] = in_img<i>[j];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;fprintf(pOutFile,&quot;%c%c&quot;,in_img<i>[j],' '); // ' ' = 20<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; //Short_Img_To_File(out_fname,tile_img,x_size,y_size);<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; Free_Img(in_img);<br />
&nbsp; &nbsp; Free_Img(tile_img);<br />
&nbsp; &nbsp; fclose(pOutFile);<br />
&nbsp; &nbsp; return 0;<br />
<br />
}
游客请输入验证码
浏览1942923次