A program (in C) to find the distance to an object using two images and computing distances accross these images (thanks to a stereoscopy algorithm).

RGB2grey.c 613B

123456789101112131415161718192021222324252627282930
  1. unsigned char** RGB2grey(unsigned char*** image, int width, int height)
  2. {
  3. unsigned char** grey;
  4. int x,y;
  5. if((grey = malloc(sizeof(*grey) * height)) == NULL)
  6. {
  7. perror("malloc:");
  8. return NULL;
  9. }
  10. for(y = 0; y <= height; y++)
  11. {
  12. if((grey[y] = malloc(sizeof(**grey) * width)) == NULL)
  13. {
  14. perror("malloc:");
  15. return NULL;
  16. }
  17. }
  18. for(y = 0; y <= height; y++)
  19. {
  20. for(x = 0; x <= width; x++)
  21. {
  22. grey[y][x] = (unsigned char) (0.2125*image[y][x][0] + 0.7154*image[y][x][1] + 0.0721*image[y][x][2]);
  23. //Formula found here : https://fr.wikipedia.org/wiki/Niveau_de_gris
  24. }
  25. }
  26. return grey;
  27. }