/pi/image.c

https://bitbucket.org/leandromattioli/monnaie · C · 102 lines · 46 code · 13 blank · 43 comment · 6 complexity · ec9118543a403b44491de87c087e0274 MD5 · raw file

  1. /**
  2. * @file image.c
  3. * @brief Basic operations on images (persistence and color at coordinate)
  4. */
  5. #include <stdio.h>
  6. #include <opencv/cv.h>
  7. #include <opencv/highgui.h>
  8. #include <stdbool.h>
  9. #include "image.h"
  10. /**
  11. * Loads an image from a file
  12. * @param filename Path to the file
  13. * @return The new Image entity
  14. */
  15. Image image_create_from_file(char* filename) {
  16. return cvLoadImage(filename, 1);
  17. }
  18. /**
  19. * Destroys an Image entity.
  20. * This function should not be called to destroy images created by cvRetrieveFrame.
  21. * @param img An Image entity
  22. */
  23. void image_destroy(Image* img) {
  24. cvReleaseImage(img);
  25. }
  26. /**
  27. * Saves the image to a file
  28. * @param img An Image entity
  29. * @param file A file path
  30. */
  31. void image_save(Image img, char* file) {
  32. cvSaveImage(file, img, NULL); //@NOTE Omit last parameter for older versions of OpenCV
  33. }
  34. /**
  35. * Gets the RGB color of a pixel
  36. * @param rgbImg The RGB image to work with
  37. * @param rgb The color structure to save the results
  38. * @param x Pixel's x coordinate
  39. * @param y Pixel's y coordinate
  40. * @return 1 for success or 0 for failure
  41. */
  42. short int image_get_rgb_color_at(Image rgbImg, Color *rgb, int x, int y) {
  43. int pixelPosition;
  44. uchar* ptr_rgb;
  45. if(!image_valid_coords(rgbImg, x, y))
  46. return 0;
  47. ptr_rgb = (uchar*) (rgbImg->imageData + y * rgbImg->widthStep);
  48. pixelPosition = 3*x;
  49. rgb->c1 = ptr_rgb[pixelPosition];
  50. rgb->c2 = ptr_rgb[pixelPosition + 1];
  51. rgb->c3 = ptr_rgb[pixelPosition + 2];
  52. return 1;
  53. }
  54. /**
  55. * Gets the HSV color of a pixel
  56. * @param rgbImg The RGB image to work with
  57. * @param hsv The color structure to save the results
  58. * @param x Pixel's x coordinate
  59. * @param y Pixel's y coordinate
  60. * @return 1 for success or 0 for failure (invalid coordinates)
  61. * TODO Check whether convert operation is hardware based (otherwise it will be slow)
  62. */
  63. short int image_get_hsv_color_at(Image rgbImg, Color *hsv, int x, int y) {
  64. int pixelPosition;
  65. IplImage *hsvImg;
  66. if(!image_valid_coords(rgbImg, x, y))
  67. return 0;
  68. hsvImg = cvCreateImage(cvGetSize(rgbImg), IPL_DEPTH_8U, 3);
  69. cvCvtColor(rgbImg, hsvImg, CV_RGB2HSV);
  70. uchar* ptr_hsv = (uchar*) (hsvImg->imageData + y * hsvImg->widthStep);
  71. pixelPosition = 3*x;
  72. hsv->c1 = ptr_hsv[pixelPosition];
  73. hsv->c2 = ptr_hsv[pixelPosition + 1];
  74. hsv->c3 = ptr_hsv[pixelPosition + 2];
  75. cvReleaseImage(&hsvImg);
  76. return 1;
  77. }
  78. /**
  79. * Checks whether a pair of pixel coordinates is valid for a given image
  80. * @param img The image to work with
  81. * @param x Pixel's x coordinate
  82. * @param y Pixel's y coordinate
  83. * @return 1 for valid coordinates or 0 for invalid (outside the image)
  84. */
  85. bool image_valid_coords(Image img, int x, int y) {
  86. if(x < 0 || x >= img->width || y < 0 || y >= img->height)
  87. return false;
  88. return true;
  89. }