Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to make a "face detection" program in C. The inputs are the number of "faces" detected, height and width of the picture (not larger than 120), and then the coordinates of the face. After that, the next inputs are three arrays which represent the RGB values of the picture.

The output of the program should be the original picture, but with the coordinates connecting as a green rectangle. The alpha value in the rectangle should be 255, and outside should be 127.

We get a few test cases to work with. The first test case is a simple 5x5 black square with random coordinates, the output for this test case is fine.

The problem arises in the other 3 test cases, which are actual pictures. What happens is, instead of a green rectangle, there are random green lines and dots all around the picture (not even on the coordinates). And the alpha values are seemingly radom as well, where a few lines are alpha 127, and a few 255.

Edit: I definitely worded everything wrong. The program needs to draw a green rectangle and modify the alpha values around the coordinates in the input. The program itself doesn't identify the face.

C++
#include <stdio.h>
#define MAX 120

int main() {
	FILE* fin = fopen("input.txt", "r");
	FILE* fout = fopen("output.txt", "w");

	int R[MAX][MAX], G[MAX][MAX], B[MAX][MAX], x1[MAX], y1[MAX], x2[MAX], y2[MAX], A[MAX][MAX];
	int h, w, n, i, j, k;

	fscanf(fin, "%d %d %d", &n, &h, &w);

	for (i = 0; i < n; i++) {
		fscanf(fin, "%d %d %d %d", &x1[i], &y1[i], &x2[i], &y2[i]);
	}

	for (i = 0; i < w; i++) {
		for (j = 0; j < h; j++) {
			fscanf(fin, "%d ", &R[i][j]);
		}
	}
	for (i = 0; i < w; i++) {
		for (j = 0; j < h; j++) {
			fscanf(fin, "%d ", &G[i][j]);
		}
	}
	for (i = 0; i < w; i++) {
		for (j = 0; j < h; j++) {
			fscanf(fin, "%d ", &B[i][j]);
		}
	}
	for (i = 0; i < w; i++) {
		for (j = 0; j < h; j++) {
			A[i][j] = 127;
		}
	}
	for (k = 0; k < n; k++) {
		for (i = x1[k]; i <= x2[k]; ++i) {
			G[i][y1[k]] = 255; 
			G[i][y2[k]] = 255;  
			A[i][y1[k]] = 255;
			A[i][y2[k]] = 255;
			R[i][y1[k]] = 0;
			R[i][y2[k]] = 0;
			B[i][y1[k]] = 0;
			B[i][y2[k]] = 0;
		}
		for (j = y1[k]; j <= y2[k]; ++j) {
			G[x1[k]][j] = 255;
			G[x2[k]][j] = 255; 
			A[x1[k]][j] = 255;
			A[x2[k]][j] = 255;
			R[x1[k]][j] = 0;
			R[x2[k]][j] = 0;
			B[x1[k]][j] = 0;
			B[x2[k]][j] = 0;
		}
	}
	for (k = 0; k < n; k++) {
		for (i = x1[k] + 1; i <= x2[k] - 1; i++) {
			for (j = y1[k] + 1; j <= y2[k] - 1; j++) {
				A[i][j] = 255;
			}
		}
	}
	fprintf(fout, "/image %d %d RGBA\n", h, w);
	for (i = 0; i < w; i++) {
		for (j = 0; j < h; j++) {
			fprintf(fout, "%d %d %d %d ", R[i][j], G[i][j], B[i][j], A[i][j]);
		}
	}

	return 0;
}


What I have tried:

The inputs of the program are read correctly, as I've tried printing them out and they come out fine. So I've tried rewriting the for loops in several different ways, but no matter what paremeters I set, the result comes out the same.
Posted
Updated 17-Nov-19 19:41pm
v3

1 solution

 
Share this answer
 
Comments
Marko Zaninovic 17-Nov-19 8:52am    
I tried going through the files, but since I'm a beginner (barely started functions) I'm having a hard time understanding most of the code :/
RickZeeland 17-Nov-19 9:15am    
I noticed that the example uses the OpenCV library, but you might not be familiar with that?
RickZeeland 17-Nov-19 9:17am    
Also I think face detection is not a good project for beginners, as it is very complex ...
Marko Zaninovic 17-Nov-19 9:19am    
I probably worded it wrong, the purpose of the program is to just make a green rectangle and modify the alpha value around the coordinates from the input. As in, we already get the two coordinates we need.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900