質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

Q&A

1回答

2171閲覧

テンプレートマッチングが上手くいかない

ncsitrotobu

総合スコア0

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

0グッド

1クリップ

投稿2020/12/26 17:19

前提・実現したいこと

テンプレートマッチングの実装

大学の課題でテンプレートマッチングをc言語において実装してくださいというものがあったのですが、makeは通ったのですが、いざ実行してみると出力画像は真っ黒なものしか出てこず、いろいろと調べたりコードを弄ったりしたのですが出力画像は真っ黒なままで、どうすればよいのかわからなくなってしまったので今回質問させていただきました。そもそものアルゴリズム自体がおかしいかもしれないので、もしそうでしたらその部分も指摘していただけるとありがたいです。

ソースコード

### 該当のソースコード 「bitmap.h」 #ifndef BITMAP_H_INCLUDED_ #define BITMAP_H_INCLUDED_ #define FILEHEADERSIZE 14 #define INFOHEADERSIZE 40 #define HEADERSIZE (FILEHEADERSIZE + INFOHEADERSIZE) typedef struct{ unsigned char b; unsigned char g; unsigned char r; }Rgb; typedef struct{ unsigned int height; unsigned int width; Rgb *data; }Image; Image *Read_Bmp(char *filename); int Write_Bmp(char *filename, Image * img); Image *Create_Image(int width, int height); void Free_Image(Image *img); #endif /*_BITMAP_H_INCLUDED_*/ 「editfile.c」 #include <stdio.h> #include <stdlib.h> #include <String.h> #include <math.h> #include "bitmap.h" Image *Read_Bmp(char *filename){ int i, j; int real_width; unsigned int width, height; unsigned int color; FILE *fp; unsigned char header_buf[HEADERSIZE]; unsigned char *bmp_line_data; Image *img; if((fp = fopen(filename, "rb")) == NULL){ fprintf(stderr, "Error: %s could not read.", filename); return NULL; } fread(header_buf, sizeof(unsigned char), HEADERSIZE, fp); if(strncmp((char *)header_buf, "BM", 2)){ fprintf(stderr, "Error: %s is notBitMap file", filename); return NULL; } memcpy(&width, header_buf + 18, sizeof(width)); memcpy(&height, header_buf + 22, sizeof(height)); memcpy(&color, header_buf + 28, sizeof(unsigned int)); if(color != 24){ fprintf(stderr, "Error: %s is not 24bit color image", filename); return NULL; } real_width = width*3 + width%4; if((bmp_line_data = (unsigned char *)malloc(sizeof(unsigned char)*real_width)) == NULL){ fprintf(stderr, "Error: Allocation error.\n"); return NULL; } if((img = Create_Image(width, height)) == NULL){ free(bmp_line_data); fclose(fp); return NULL; } for(i=0; i<height; i++){ fread(bmp_line_data, 1, real_width, fp); for(j=0; j<width; j++){ img->data[(height-i-1)*width + j].b = bmp_line_data[j*3]; img->data[(height-i-1)*width + j].g = bmp_line_data[j*3 + 1]; img->data[(height-i-1)*width + j].r = bmp_line_data[j*3 + 2]; } } free(bmp_line_data); fclose(fp); return img; } int Write_Bmp(char *filename, Image *img) { int i, j; FILE *fp; int real_width; unsigned char *bmp_line_data; unsigned char header_buf[HEADERSIZE]; unsigned int file_size; unsigned int offset_to_data; unsigned long info_header_size; unsigned int planes; unsigned int color; unsigned long compress; unsigned long data_size; long xppm; long yppm; if((fp = fopen(filename, "wb")) == NULL){ fprintf(stderr, "Error: %s could not open.", filename); return 1; } real_width = img->width*3 + img->width%4; file_size = img->height * real_width + HEADERSIZE; offset_to_data = HEADERSIZE; info_header_size = INFOHEADERSIZE; planes = 1; color = 24; compress = 0; data_size = img->height * real_width; xppm = 1; yppm = 1; header_buf[0] = 'B'; header_buf[1] = 'M'; memcpy(header_buf + 2, &file_size, sizeof(file_size)); header_buf[6] = 0; header_buf[7] = 0; header_buf[8] = 0; header_buf[9] = 0; memcpy(header_buf + 10, &offset_to_data, sizeof(file_size)); header_buf[11] = 0; header_buf[12] = 0; header_buf[13] = 0; memcpy(header_buf + 14, &info_header_size, sizeof(info_header_size)); header_buf[15] = 0; header_buf[16] = 0; header_buf[17] = 0; memcpy(header_buf + 18, &img->width, sizeof(img->width)); memcpy(header_buf + 22, &img->height, sizeof(img->height)); memcpy(header_buf + 26, &planes, sizeof(planes)); memcpy(header_buf + 28, &color, sizeof(color)); memcpy(header_buf + 30, &compress, sizeof(compress)); memcpy(header_buf + 34, &data_size, sizeof(data_size)); memcpy(header_buf + 38, &xppm, sizeof(xppm)); memcpy(header_buf + 42, &yppm, sizeof(yppm)); header_buf[46] = 0; header_buf[47] = 0; header_buf[48] = 0; header_buf[49] = 0; header_buf[50] = 0; header_buf[51] = 0; header_buf[52] = 0; header_buf[53] = 0; fwrite(header_buf, sizeof(unsigned char), HEADERSIZE, fp); if((bmp_line_data = (unsigned char *)malloc(sizeof(unsigned char)*real_width)) == NULL){ fprintf(stderr, "Error: Allocation error.\n"); fclose(fp); return 1; } for(i=0; i<img->height; i++){ for(j=0; j<img->width; j++){ bmp_line_data[j*3] = img->data[(img->height - i - 1)*img->width + j].b; bmp_line_data[j*3 + 1] = img->data[(img->height - i - 1)*img->width + j].g; bmp_line_data[j*3 + 2] = img->data[(img->height - i - 1)*img->width + j].r; } for(j=img->width*3; j<real_width; j++){ bmp_line_data[j] = 0; } fwrite(bmp_line_data, sizeof(unsigned char), real_width, fp); } free(bmp_line_data); fclose(fp); return 0; } Image *Create_Image(int width, int height) { Image *img; if((img = (Image *)malloc(sizeof(Image))) == NULL){ fprintf(stderr, "Allocation error\n"); return NULL; } if((img->data = (Rgb*)malloc(sizeof(Rgb)*width*height)) == NULL){ fprintf(stderr, "Allocation error\n"); free(img); return NULL; } img->width = width; img->height = height; return img; } void Free_Image(Image *img) { free(img->data); free(img); } 「main.c」 #include "bitmap.h" #include <stdlib.h> #include <stdio.h> #include <string.h> #define M 100 #define N 10 int main(int argc, char *argv[]){ if(argc != 3){ fprintf(stderr, "Usage: program <inputfile> <outputfile>\n"); exit(1); } Image *colorimg; if((colorimg = Read_Bmp(argv[1])) == NULL){ exit(1); } Image *outputimg; int i, j, k, l, m, n; double color1[N*N]; //部分画像のR double color2[N*N]; //部分画像のG double color3[N*N]; //部分画像のB double color4[N*N]; //テンプレートマッチングのR double color5[N*N]; //テンプレートマッチングのG double color6[N*N]; //テンプレートマッチングのB size_t n = N*N; outputimg = Create_Image(colorimg->width, colorimg->height); memset(color1, 0, sizeof(color1)); memset(color2, 0, sizeof(color2)); memset(color3, 0, sizeof(color3)); memset(color4, 0, sizeof(color4)); memset(color5, 0, sizeof(color5)); memset(color6, 0, sizeof(color6)); //部分画像を取得 for(i = M; i < M + N; i++ ){ for(j = M; j < M + N; j++){ color1[3*(i - M) + (j - M)]+= (double)(unsigned char)colorimg->data[i*colorimg->width + j].r; color2[3*(i - M) + (j - M)]+= (double)(unsigned char)colorimg->data[i*colorimg->width + j].g; color3[3*(i - M) + (j - M)]+= (double)(unsigned char)colorimg->data[i*colorimg->width + j].b; } } //テンプレートマッチング for(k = 0; k < colorimg->height - N; k++){ for(l = 0; l < colorimg->width - N; l++){ for(m = 0; m < N; m++){ for(n = 0; n < N; n++){ color4[3*m + n]+= (double)(unsigned char)colorimg->data[(k + m)*colorimg->width + (l + n)].r; color5[3*m + n]+= (double)(unsigned char)colorimg->data[(k + m)*colorimg->width + (l + n)].g; color6[3*m + n]+= (double)(unsigned char)colorimg->data[(k + m)*colorimg->width + (l + n)].b; } } if((memcmp(color1, color4, n) == 0) && (memcmp(color2, color5, n) == 0) &&(memcmp(color3, color6, n) == 0)){ for(m = 0; m < N; m++){ for(n = 0; n < N; n++){ outputimg->data[(k + m)*outputimg->width + (l + n)].r = color4[3*m + n]; outputimg->data[(k + m)*outputimg->width + (l + n)].g = color5[3*m + n]; outputimg->data[(k + m)*outputimg->width + (l + n)].b = color6[3*m + n]; goto END; } } } } } END: if(Write_Bmp(argv[2], outputimg)){ //画像の出力 exit(1); } Free_Image(outputimg); Free_Image(colorimg); return 0; }

言語:c言語 環境:windows10

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

1T2R3M4

2020/12/26 23:47

>いろいろと調べたりコードを弄ったりした 具体的な内容を試したことに追記できませんか。
dodox86

2020/12/27 02:04

makeやコンパイルが通るのは最低限、前提の話です。ビットマップファイルを開いて希望のデータが取れているのか、マッチング用のデータが合っているのか、マッチ処理は意図通りに動いているのか、出力のデータは意図通りに出力されているのか、その他色々、それぞれの箇所をひとつずつ確認し、質問のポイントを絞りましょう。今のままではほぼ、丸投げです。 [質問するときのヒント] https://teratail.com/help/question-tips
hoshi-takanori

2020/12/27 03:10

どうせ丸投げするならサンプルデータも提示すればいいのに…、と思ってソースコードを眺めてみたら、入力画像を一つしか取らないようですが、全体画像とテンプレート画像の 2 つ必要なのでは?
guest

回答1

0

C言語のコードを書くなら、デバッグできる環境を整えましょう。
Eclipseや、WindowsならVisualStudioなど。
コードの任意の場所で実行を止め、変数のナカミを見ることができます。そこから1行づつ実行して、コードの流れを見れるようになります
そうすれば、アテズッポでコードを書かなくて済むようになります。

投稿2020/12/27 00:00

y_waiwai

総合スコア87782

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ncsitrotobu

2020/12/27 02:56

ご回答ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問