ラベリング処理プログラム
画像のラベリング処理プログラムを作っているんですが
どうもうまく実行できません。よければ教えていただけないでしょうか。
#include<stdio.h>
#include<stdlib.h>
int column, row;
unsigned char val[4] = {0,0,0,0};
unsigned char tmp[255];
int pos_y[4] = {-1, 0, 1, 0};
int pos_x[4] = {0, 1, 0, -1};
int i, j, x, y, label, level, label1;
int label_count = 1;
unsigned char *in, *out;
void labeling_main();
void labeling_search();
void labeling_main()
{
for(i = 0; i < y; i++){
for(j = 0; j < x; j++){
printf("aaa\n");
if(out[i * x + j] == 255){
printf("bbb\n");
fflush(stdout);
out[i * x + j] = label_count;
labeling_search(label_count, i, j);
label_count++;
}
}
}
}
void labeling_search(int label_count, int x, int y)
{
for(i = 0; i < 4; i++){
if(out[(pos_y[i] + y) * x + (pos_x[i] + x)] == 255){
out[(pos_y[i] + y) * x + (pos_x[i] + x)] = label_count;
labeling_search(label_count,(pos_y[i]+y),(pos_x[i]+x));
}
}
printf("ccc\n");
}
int main(int argc, char *argv[])
{
int result;
int head, Magic;
unsigned char *image, *in, *out, *res, *ros;
FILE *fin, *fout;
if(argc!=3){
printf("Usage : %s input output\n",argv[0]);
exit(1);
}
fin = fopen(argv[1],"rb");
/* -------------------- ヘッダ取得ここから -------------------- */
fgets(tmp,255,fin);
if(tmp[0]!='P') return 0;
sscanf(tmp,"P%d",&Magic);
if(Magic < 1 || Magic > 6) return 0;
do fgets(tmp,255,fin); while(tmp[0]=='#');
sscanf(tmp,"%d %d",&x,&y);
if(x < 1 || y < 1) return 0;
fgets(tmp,255,fin);
sscanf(tmp,"%d",&level);
/* ヘッダの確認 */
printf("P%d\n",Magic);
printf("%d %d\n",x,y);
printf("%d\n",level);
/* 画素の読み込み */
in = (unsigned char *)malloc(sizeof(unsigned char) *x*y);
fread(in,sizeof(unsigned char),x*y,fin);
fout = fopen(argv[2],"wb");
fprintf(fout,"P%d\n",Magic);
fprintf(fout,"# My new PGM\n");
fprintf(fout,"%d %d\n",x, y);
fprintf(fout,"%d\n",level);
fwrite(out, sizeof(unsigned char),x*y, fout);
out = (unsigned char *)malloc(sizeof(unsigned char) *x*y);
//2値画像
for (i = 0; i < y; i++) {
for (j = 0; j < x; j++){
if(in[i * x + j] > 120){
out[i * x + j] = 0;
}else if(in[i * x + j] <= 120){
out[i * x + j] = 255;
}
}
}
labeling_main();
printf("Max label number:%d\n",label_count);
free(in);
free(out);
fclose(fin);
fclose(fout);
}
コンパイルは通るのですが実行するとlabeling_mainの
if文でセグメンテーションが出てしまいます。