[ create a new paste ] login | about

Link: http://codepad.org/76qGL5Cb    [ raw code | output | fork ]

C++, pasted on Nov 26:
// kk3.cpp : コンソールアプリケーションのエントリポイントを定義します。

#include "stdafx.h"
#include <malloc.h>
#include <math.h>



int _tmain(int argc, _TCHAR* argv[])
{
        int *image_in,*image_out;
        int x,y,K,newdata,M;
        int a,b;                                                                        // 元画像の最大の画素値をb、最小の画素値をa
        int b1 = 255,a1 = 0;                                                // 変換後の濃度値の最大と最小をそれぞれb1,a1
        int height,wedth;                                                        // 元画像の大きさ
        char magic[5],s[256];                                                // マジックナンバーなど
        FILE *fp1,*fp2,*fp3,*fp4,*N1,*N2,*N3,*N4;
        int kazu[256];                                                                // 画素数
        double n,m=0.75;                                                        // mにガンマ補正のγの値、nはdoubleに変換のため
        double h=0,i,j=0,k,l;                                                // hは元画像の濃度の平均値、jは標準偏差、kとlはd
oubleに変換のため
        double p=40,q=100;                                                        //pに任意の標準偏差、qに任意の平均値

        fp1=fopen("baboon-256x256-a.pgm","r");                //画像ファイルを直接開く

        fgets(magic,5,fp1);                                                        //マジックナンバーを読み込む
        fgets(s,256,fp1);                                                        //コメントを読み込む
        fscanf(fp1,"%d",&wedth);                                        //画像の横の長さを読み込む
        fscanf(fp1,"%d",&height);                                        //画像の縦の長さを読み込む
        fscanf(fp1,"%d",&M);                                                //画素値の最大を読み込む


    image_in = (int *)malloc(sizeof(int)*(wedth*height));
        image_out = (int *)malloc(sizeof(int)*(wedth*height));

    if(image_in == NULL)
        {
                printf("Memory allocation error.\n");
                return(0);
        }

        if(image_out == NULL)
        {
                printf("Memory allocation error.\n");
                return(0);
        }

        N1 = fopen("moto","w");
        N2 = fopen("senkei","w");
        N3 = fopen("ganma","w");
        N4 = fopen("hensa","w");

        // 初期化
        for(K=0;K<256;K++)
        {
                kazu[K] = 0;
        }

        a = 255;
        b = 0;
        for(y=0;y<height;y++)
        {
                for(x=0;x<wedth;x++)
                {
                        fscanf(fp1,"%d",&newdata);


                        if(newdata > b)
                        {
                                b = newdata;
                        }

                        if(newdata < a)
                        {
                                a = newdata;
                        }

                        image_in[y*wedth+x] = newdata;

                        // 同じ濃度値をもつ画素数を数える
                        for(K=0;K<256;K++)
                        {
                                if(K == image_in[y*wedth+x])
                                {
                                        kazu[K]++;
                                }
                        }

                }
        }

        // 同じ濃度値をもつ画素数をファイルに書き込む
        for(K=0;K<256;K++)
        {
                fprintf(N1,"%d\n",kazu[K]);
        }


// 線形変換

        for(K=0;K<256;K++)
        {
                kazu[K] = 0;
        }

        // 線形変換
        for(y=0;y<height;y++)
        {
                for(x=0;x<wedth;x++)
                {
                        image_out[y*wedth+x] = ((b1-a1)/(b-a))*(image_in[y*wedth+x]-a)+a1;

                        for(K=0;K<256;K++)
                        {
                                if(K == image_out[y*wedth+x])
                                {
                                        kazu[K]++;
                                }
                        }

                }

        }

        // 同じ濃度値をもつ画素数をファイルに書き込む
        for(K=0;K<256;K++)
        {
                fprintf(N2,"%d\n",kazu[K]);
        }



        fp2 = fopen("senkei.pgm","w");

        fputs(magic,fp2);                                                        //マジックナンバーを書き込む
        fputs(s,fp2);                                                                //コメントを書き込む
        fprintf(fp2,"%d %d\n",wedth,height);                //画像の横と縦の長さを書き込む
        fprintf(fp2,"%d\n",M);                                                //画素値の最大を書き込む

        //画像データを書き込む
        for(y=0;y<height;y++)
        {
                for(x=0;x<wedth;x++)
                {
                        fprintf(fp2,"%d\n",image_out[y*wedth+x]);
                }
        }



// 問題1ガンマ補正


        for(K=0;K<256;K++)
        {
                kazu[K] = 0;
        }

        // ガンマ補正
        for(y=0;y<height;y++)
        {
                for(x=0;x<wedth;x++)
                {
                        n = image_in[y*wedth+x];
                        image_out[y*wedth+x] = b*pow((n/b),m);

                        for(K=0;K<256;K++)
                        {
                                if(K == image_out[y*wedth+x])
                                {
                                        kazu[K]++;
                                }
                        }

                }

        }

        // 同じ濃度値をもつ画素数をファイルに書き込む
        for(K=0;K<256;K++)
        {
                fprintf(N3,"%d\n",kazu[K]);
        }



        fp3 = fopen("ganma.pgm","w");

        fputs(magic,fp3);                                                                                //マジックナンバーを書き込む
        fputs(s,fp3);                                                                                        //コメントを書き込む
        fprintf(fp3,"%d %d\n",wedth,height);                                        //画像の横と縦の長さを書き込む
        fprintf(fp3,"%d\n",M);                                                                        //画素値の最大を書き込む

        //画像データを書き込む
        for(y=0;y<height;y++)
        {
                for(x=0;x<wedth;x++)
                {
                        fprintf(fp3,"%d\n",image_out[y*wedth+x]);
                }
        }








        free(image_in);
        free(image_out);

        fclose(fp1);
        fclose(fp2);
        fclose(fp3);
        fclose(N1);
        fclose(N2);
        fclose(N3);



        return 0;
}


Output:
1
2
3
4
5
Line 19: error: stdafx.h: No such file or directory
In file included from t.cpp:5:
Line 20: error: stddef.h: No such file or directory
Line 21: error: stray '\343' in program
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: