電子書籍の厳選無料作品が豊富!

ソースは以下です
途中までは実行されるんですけど,,,
教えてください


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define M_PI 3.14159265358979 /* 円周率 */

int main(int argc, char **argv)
{



int xsize,ysize,depth,x,y,c;
char code[10];
FILE *fp;
unsigned char ***image,***outimage;
double sum_kido,sum_kido_x,grad;
int xmax,xmin,ymax,ymin,count;
char grad_im;
int sumx, sumy, N;
char dummy[1024];
double Deltax;
double Deltay;


if(argc != 3){
fprintf(stderr,"Usage : a.out input_filename output_filename\n");
exit(1);
}
if((fp=fopen(argv[1],"r"))==NULL){
fprintf(stderr,"File can not open : %s\n",argv[1]);
exit(1);
}

fscanf(fp,"%s",code);
/*
fscanf(fp,"%s",dummy);
*/
fscanf(fp,"%d %d",&xsize,&ysize);
fscanf(fp,"%d ",&depth);

fprintf(stderr,"%s, %d, %d, %d\n",code,xsize,ysize,depth);
getchar();

image = (unsigned char ***)malloc(sizeof(unsigned char**)*ysize);
for(y=0;y<ysize;y++){
image[y] = (unsigned char **)malloc(sizeof(unsigned char*)*xsize);
for(x=0;x<xsize;x++){
image[y][x] = (unsigned char *)malloc(sizeof(unsigned char)*3);
}
}

outimage = (unsigned char ***)malloc(sizeof(unsigned char**)*ysize);
for(y=0;y<ysize;y++){
outimage[y] = (unsigned char **)malloc(sizeof(unsigned char*)*xsize);
for(x=0;x<xsize;x++){
outimage[y][x] = (unsigned char *)malloc(sizeof(unsigned char)*3);
}
}
// load image
for(y=0;y<ysize;y++){
for(x=0;x<xsize;x++){
for(c=0;c<3;c++){
image[y][x][c] = (unsigned char)(fgetc(fp));
printf("%d\n", image[y][x][c]);
}
}
}
fclose(fp);





sumx = 0;
sumy = 0;
N = 0;

for(y=0;y<ysize;y++){
for(x=0;x<xsize;x++){
if((image[y][x][0]*2<image[y][x][2])&&(image[y][x][1]*2<image[y][x][2])){
outimage[y][x][0]=255;
outimage[y][x][1]=0;
outimage[y][x][2]=0;
}else{
outimage[y][x][0]=image[y][x][0];
outimage[y][x][1]=image[y][x][1];
outimage[y][x][2]=image[y][x][2];
sumx += x; sumy += y, N += 1;
}
}
}


// output image
if((fp=fopen(argv[2],"w"))==NULL){
fprintf(stderr,"File can not open : %s\n",argv[2]);
exit(1);
}
fprintf(fp,"%s\n",code);
fprintf(fp,"%d %d\n",xsize,ysize);
fprintf(fp,"%d\n",depth);
count = 0;
for(y=0;y<ysize;y++){
for(x=0;x<xsize;x++){
for(c=0;c<3;c++){
fputc(outimage[y][x][c],fp);


}
}
}

fclose(fp);
fprintf(stdout, "%d, %d, %d, Gravity (x,y) = (%lf, %lf)\n",sumx,sumy,N,(double)(sumx)/(double)(N), (double)(sumy)/(double)(N));


for(y=0;y<ysize;y++){
for(x=0;x<xsize;x++){
Deltax = sqrt(( pow (image[x+1][y][0] , 2.0 ) + pow (image[x+1][y][1] , 2.0 ) + pow (image[x+1][y][2] , 2.0 )) - ( sqrt( pow (image[x-1][y][0] , 2.0) + (image[x-1][y][1] , 2.0) + (image[x-1][y][2] , 2.0))));
Deltay = sqrt(( pow (image[x][y+1][0] , 2.0 ) + pow (image[x][y+1][1] , 2.0 ) + pow (image[x][y+1][2] , 2.0 )) - ( sqrt( pow (image[x][y-1][0] , 2.0) + (image[x][y-1][1] , 2.0) + (image[x][y-1][2] , 2.0))));
}
}


for(y=0;y<ysize;y++){
for(x=0;x<xsize;x++){

double T = atan( Deltax / Deltay ); //勾配ベクトルの方向

double U = sqrt( pow ( Deltax , 2.0 ) + pow (Deltay , 2.0 ) ); //勾配ベクトルの勾配量 

double V = atan( ( x - (double)(sumx) / (double)(N) ) / ( y - (double)(sumy) / (double)(N) ) );

printf(" %f\n " , V - ( T + ( M_PI / 2 )));
}
}
}

A 回答 (3件)

segmentation faultの原因は、画像の外側にアクセスしているためですね。



Deltax, Deltayを求める部分で、x == 0 の場合も image[x-1][y][0] を読み取ろうとしています。

 for(y=0;y<ysize;y++){
  for(x=0;x<xsize;x++){
   if ((x > 0) && (x < xsize - 1) && (y > 0) && (y < ysize - 1)) {
    Deltax = ...
    Deltay = ...
   } else {
    ...
   }
  }
 }

のようにすれば大丈夫ですが、ただこれではDeltaxもDeltayも最後の画素のエッジ強度だけになってしまうような...?

質問する際はプログラムの目的も書かれた方が、他の方も答えやすいと思います。
    • good
    • 0
この回答へのお礼

ありがとうございます

if ((x > 0) && (x < xsize - 1) && (y > 0) && (y < ysize - 1))
この部分がよくわからないのですがどのような意味でしょうか?
xsize-1などはなぜそうなるのでしょうか?

またこのプログラム目的は
背景の青に近いものをカットし、
画像の中心からの任意の点への方向ベクトルと
任意の点の匂配ベクトルが直行しているかを確認するプログラムです

お礼日時:2013/01/23 04:05

> でしたら


> for(y=1;y<ysize;y++)
> このようにして1に変更したらよいと考えたのですがこのようなやり方はいいのでしょうか??

○確実に間違っている点
このループでは、変数yは1<=y<ysize になります。yは整数なので、1<=y<=ysize-1 です。
では、y=ysize-1のとき、image[x][y+1][0] はどうなるでしょう?

○間違っているとは言いきれない点
このような画像処理で問題になるのは周辺部です。
y=0に注目すると image[x][y-1]が存在しません。このような場合、次のような処理をします。
1) image[x][-1]を何かの方法で用意する
最後の値を繰り返す(image[x][-1]=image[x][0]),周辺の値から補間する 等
2) 全てのデータのあるところだけ計算し、足りない座標は計算しない。

この例の場合、一番端を見る必要は無いように思うので、1<=y<ysize-1の範囲で計算すればいいように思います。
端まで計算したいのなら、1)の方法で「外側」の値を用意してください。

xについても同様です。
    • good
    • 0

配列を使っていてsegmentation faultが出たら、まずは、範囲が正しいか確認するとよいでしょう。



for(y=0;y<ysize;y++){
for(x=0;x<xsize;x++){
Deltax = sqrt(( pow (image[x+1][y][0] , 2.0 ) + pow (image[x+1][y][1] , 2.0 ) + pow (image[x+1][y][2] , 2.0 )) - ( sqrt( pow (image[x-1][y][0] , 2.0) + (image[x-1][y][1] , 2.0) + (image[x-1][y][2] , 2.0))));
Deltay = sqrt(( pow (image[x][y+1][0] , 2.0 ) + pow (image[x][y+1][1] , 2.0 ) + pow (image[x][y+1][2] , 2.0 )) - ( sqrt( pow (image[x][y-1][0] , 2.0) + (image[x][y-1][1] , 2.0) + (image[x][y-1][2] , 2.0))));
}
}


x=0,y=0のとき、正しいでしょうか?
    • good
    • 0
この回答へのお礼

ありがとうございます

確かにx=0,y=0のとき正しくありません
座標がマイナスはありえませんでした

でしたら
for(y=1;y<ysize;y++)

このようにして1に変更したらよいと考えたのですがこのようなやり方はいいのでしょうか??

お礼日時:2013/01/23 04:00

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!