チーム16E2
課題名
モザイク処理の限度
研究者名
3-15-23 Kitafuku Keigo
概要
画像に対してアベレージフィルタをかけ続け、どこまで画像としての役割を維持できるのかを観察する。
ソースコード
アベレージフィルタ1回
Mosaic_Continue.c
#include <gd.h>
#include <jpeglib.h>
#include <stdlib.h>
#include <string.h>
#define step 5
int main(const int argc,const char *argv[]){
FILE *out,*in;
gdImagePtr im,im_new,im_new2,im_new3; int width,height,i,j,color,r,g,b,pixel,l,k,y,c;
if((argv[1]==NULL) | | (argv[2]==NULL)){ printf("argument error\n"); exit(-1); }
if((in=fopen(argv[1],"r"))==NULL){ printf("file open error for %s\n",argv[1]); exit(-1); } //第二引数で指定されたファイルを書き出し用にオープン if((out=fopen(argv[2],"wb"))==NULL){ printf("file open error for %s\n",argv[2]); exit(-1); }
im = gdImageCreateFromJpeg(in);
width=gdImageSX(im); height=gdImageSY(im);
im_new = gdImageCreateTrueColor(width,height); im_new2= gdImageCreateTrueColor(width,height); im_new3= gdImageCreateTrueColor(width,height); for(i=1;i<(width-step);i=i+step){ for(j=1;j<(height-step);j=j+step){ r=0;g=0;b=0; for(k=0;k<step;k++){ for(l=0;l<step;l++){ pixel=gdImageGetPixel(im,i-1+k,j-1+l); r=r+gdImageRed(im,pixel); g=g+gdImageGreen(im,pixel); b=b+gdImageBlue(im,pixel); } } //アベレージフィルタの実行(1回目) r=r/step/step; g=g/step/step; b=b/step/step; if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0; if (255 < r) r = 255; if (255 < g) g = 255; if (255 < b) b = 255; // 各画素を求めた色で表示する。表示範囲を超えないようにサイズの確認をする。 for (k = 0; k < step; k++){ for (l = 0; l < step; l++){ if (j + k < height){ if (i + l < width){ //im の (i,j) における r,g,b の値を取得 color=gdImageColorExact(im_new,r,g,b); //im_new の (i,j) におけるピクセル値を color で設定 gdImageSetPixel(im_new,i+k,j+l,color); } } } }
} }
gdImageJpeg(im_new,out,-1);
fclose(in);
fclose(out);
return 0;
}
アベレージフィルタ2回
Mosaic_Continue2.c
#include <gd.h>
#include <jpeglib.h>
#include <stdlib.h>
#include <string.h>
#define step 5
int main(const int argc,const char *argv[]){
FILE *out,*in;
gdImagePtr im,im_new,im_new2,im_new3; int width,height,i,j,color,r,g,b,pixel,l,k,y,c;
if((argv[1]==NULL) | | (argv[2]==NULL)){ printf("argument error\n"); exit(-1); }
if((in=fopen(argv[1],"r"))==NULL){ printf("file open error for %s\n",argv[1]); exit(-1); } //第二引数で指定されたファイルを書き出し用にオープン if((out=fopen(argv[2],"wb"))==NULL){ printf("file open error for %s\n",argv[2]); exit(-1); }
im = gdImageCreateFromJpeg(in);
width=gdImageSX(im); height=gdImageSY(im);
im_new = gdImageCreateTrueColor(width,height); im_new2= gdImageCreateTrueColor(width,height); im_new3= gdImageCreateTrueColor(width,height); for(i=1;i<(width-step);i=i+step){ for(j=1;j<(height-step);j=j+step){ r=0;g=0;b=0; for(k=0;k<step;k++){ for(l=0;l<step;l++){ pixel=gdImageGetPixel(im,i-1+k,j-1+l); r=r+gdImageRed(im,pixel); g=g+gdImageGreen(im,pixel); b=b+gdImageBlue(im,pixel); } } //アベレージフィルタの実行(1回目) r=r/step/step; g=g/step/step; b=b/step/step; if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0; if (255 < r) r = 255; if (255 < g) g = 255; if (255 < b) b = 255; // 各画素を求めた色で表示する。表示範囲を超えないようにサイズの確認をする。 for (k = 0; k < step; k++){ for (l = 0; l < step; l++){ if (j + k < height){ if (i + l < width){ //im の (i,j) における r,g,b の値を取得 color=gdImageColorExact(im_new,r,g,b); //im_new の (i,j) におけるピクセル値を color で設定 gdImageSetPixel(im_new,i+k,j+l,color); } } } } //アベレージフィルタの実行(2回目) r=r/step; g=g/step; b=b/step; if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0; if (255 < r) r = 255; if (255 < g) g = 255; if (255 < b) b = 255; // 各画素を求めた色で表示する。表示範囲を超えないようにサイズの確認をする。 for (k = 0; k < step; k++){ for (l = 0; l < step; l++){ if (j + k < height){ if (i + l < width){ //im の (i,j) における r,g,b の値を取得 color=gdImageColorExact(im_new,r,g,b); //im_new の (i,j) におけるピクセル値を color で設定 gdImageSetPixel(im_new,i+k,j+l,color); } } } }
} }
gdImageJpeg(im_new,out,-1);
fclose(in);
fclose(out);
return 0;
}
アベレージフィルタ3回
Mosaic_Continue3.c
#include <gd.h>
#include <jpeglib.h>
#include <stdlib.h>
#include <string.h>
#define step 5
int main(const int argc,const char *argv[]){
FILE *out,*in;
gdImagePtr im,im_new,im_new2,im_new3; int width,height,i,j,color,r,g,b,pixel,l,k,y,c;
if((argv[1]==NULL) | | (argv[2]==NULL)){ printf("argument error\n"); exit(-1); }
if((in=fopen(argv[1],"r"))==NULL){ printf("file open error for %s\n",argv[1]); exit(-1); } //第二引数で指定されたファイルを書き出し用にオープン if((out=fopen(argv[2],"wb"))==NULL){ printf("file open error for %s\n",argv[2]); exit(-1); }
im = gdImageCreateFromJpeg(in);
width=gdImageSX(im); height=gdImageSY(im);
im_new = gdImageCreateTrueColor(width,height); im_new2= gdImageCreateTrueColor(width,height); im_new3= gdImageCreateTrueColor(width,height); for(i=1;i<(width-step);i=i+step){ for(j=1;j<(height-step);j=j+step){ r=0;g=0;b=0; for(k=0;k<step;k++){ for(l=0;l<step;l++){ pixel=gdImageGetPixel(im,i-1+k,j-1+l); r=r+gdImageRed(im,pixel); g=g+gdImageGreen(im,pixel); b=b+gdImageBlue(im,pixel); } } //アベレージフィルタの実行(1回目) r=r/step/step; g=g/step/step; b=b/step/step; if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0; if (255 < r) r = 255; if (255 < g) g = 255; if (255 < b) b = 255; // 各画素を求めた色で表示する。表示範囲を超えないようにサイズの確認をする。 for (k = 0; k < step; k++){ for (l = 0; l < step; l++){ if (j + k < height){ if (i + l < width){ //im の (i,j) における r,g,b の値を取得 color=gdImageColorExact(im_new,r,g,b); //im_new の (i,j) におけるピクセル値を color で設定 gdImageSetPixel(im_new,i+k,j+l,color); } } } } //アベレージフィルタの実行(2回目) r=r/step; g=g/step; b=b/step; if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0; if (255 < r) r = 255; if (255 < g) g = 255; if (255 < b) b = 255; // 各画素を求めた色で表示する。表示範囲を超えないようにサイズの確認をする。 for (k = 0; k < step; k++){ for (l = 0; l < step; l++){ if (j + k < height){ if (i + l < width){ //im の (i,j) における r,g,b の値を取得 color=gdImageColorExact(im_new,r,g,b); //im_new の (i,j) におけるピクセル値を color で設定 gdImageSetPixel(im_new,i+k,j+l,color); } } } } //アベレージフィルタの実行(3回目) r=r/step; g=g/step; b=b/step; if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0; if (255 < r) r = 255; if (255 < g) g = 255; if (255 < b) b = 255; // 各画素を求めた色で表示する。表示範囲を超えないようにサイズの確認をする。 for (k = 0; k < step; k++){ for (l = 0; l < step; l++){ if (j + k < height){ if (i + l < width){ //im の (i,j) における r,g,b の値を取得 color=gdImageColorExact(im_new,r,g,b); //im_new の (i,j) におけるピクセル値を color で設定 gdImageSetPixel(im_new,i+k,j+l,color); } } } }
} }
gdImageJpeg(im_new,out,-1);
fclose(in);
fclose(out);
return 0;
}
説明
1つの画像に対し、アベレージフィルタを1回,2回,3回と回数を変えてかけてみてその結果画像がどうなるかを出力する。
<1回目>
<2回目>
<3回目>
考察
やはりアベレージフィルタは繰り返し実行するとスペクトルが減っていくため最終的に画像として認識できなくなることがわかった。ソースコードを作成しているときはアベレージフィルタを繰り返すと最終的に与えられた画像の色の平均の色が単色として出力されると思ったが、それは予想とは違った。あくまで色として与えられた数値を足し合わせて平均をとっているので人が認識する色と違うということで理解した。
- 最終更新:2016-11-22 16:45:25