/src/openCVJpegIO.cpp

http://overlapping-object-recognition.googlecode.com/ · C++ · 113 lines · 78 code · 17 blank · 18 comment · 9 complexity · a29591d920a8275d88e24e5fa42b5a95 MD5 · raw file

  1. #include "openCVJpegIO.h"
  2. void JpegIO::write(ColorImage* img, char* savePath){
  3. //cout << "Pisem " << savePath << endl;
  4. IplImage * pRGBImg = 0;
  5. pRGBImg = cvCreateImage
  6. ( cvSize(img->getWidth(),img->getHeight() ),
  7. IPL_DEPTH_8U, RGB_IMAGE_CHANNELS );
  8. copyImgToIplColor(pRGBImg, img);
  9. if( !cvSaveImage(savePath, pRGBImg) ){
  10. cerr << "Failed to write image file.\n";
  11. return;
  12. }
  13. cvReleaseImage(&pRGBImg);
  14. //cout << "Gotovo pisanje " << savePath << endl;
  15. return;
  16. }
  17. void JpegIO::write(GrayImage* img, char* savePath){
  18. IplImage * pGrayImg = 0;
  19. pGrayImg = cvCreateImage
  20. ( cvSize(img->getWidth(),img->getHeight() ),
  21. IPL_DEPTH_8U, GRAY_IMAGE_CHANNELS );
  22. copyImgToIplGray(pGrayImg, img);
  23. if( !cvSaveImage(savePath, pGrayImg) ){
  24. cerr << "Failed to write image file." << endl;
  25. return;
  26. }
  27. cvReleaseImage(&pGrayImg);
  28. return;
  29. }
  30. ColorImage* JpegIO::read(char* imgPath){
  31. //cout << "Ucitavam " << imgPath << endl;
  32. IplImage * pRGBImg = 0;
  33. // Load the RGB image from file
  34. pRGBImg = cvLoadImage(imgPath, CV_LOAD_IMAGE_UNCHANGED);
  35. if(!pRGBImg){
  36. cerr << "Failed to load input image." << endl;
  37. return NULL;
  38. }
  39. ColorImage *img = new ColorImage(pRGBImg->width, pRGBImg->height);
  40. copyIplToImgColor(pRGBImg, img);
  41. //cvNamedWindow("Color test 1");
  42. //cvShowImage("Color test 1", pRGBImg);
  43. //cvWaitKey(0);
  44. // cvReleaseImage(&pRGBImg);
  45. //cout << "Gotovo ucitavanje " << imgPath << endl;
  46. return img;
  47. }
  48. void JpegIO::copyImgToIplColor(IplImage *iplImg, ColorImage *img){
  49. int red=0, green=0, blue=0;
  50. for(int x = 0; x < img->getWidth(); x++){
  51. for(int y = 0; y < img->getHeight(); y++){
  52. red = img->get(x,y).v1;
  53. green = img->get(x,y).v2;
  54. blue = img->get(x,y).v3;
  55. /*
  56. *TODO: pogledati kako obaviti ovo castanje iz int u uchar.
  57. * mo?da promijeniti tip varijable ColorPixel.v*
  58. */
  59. //cout << "colourtest 1. red= " << red << ", green=" << green << "blue=" << blue << endl;
  60. ((uchar*)(iplImg->imageData + iplImg->widthStep*y))[x*3] = (uchar)blue;
  61. ((uchar*)(iplImg->imageData + iplImg->widthStep*y))[x*3+1] = (uchar)green;
  62. ((uchar*)(iplImg->imageData + iplImg->widthStep*y))[x*3+2] = (uchar)red;
  63. }
  64. }
  65. return;
  66. }
  67. void JpegIO::copyImgToIplGray(IplImage *iplImg, GrayImage *img){
  68. int value = 0;
  69. for(int x = 0; x < img->getWidth(); x++){
  70. for(int y = 0; y < img->getHeight(); y++){
  71. value = img->get(x,y).v;
  72. /*
  73. *TODO: pogledati kako obaviti ovo castanje iz int u uchar.
  74. * mo?da promijeniti tip varijable GrayPixel.v
  75. */
  76. ((uchar*)(iplImg->imageData + iplImg->widthStep*y))[x] = (uchar)value;
  77. }
  78. }
  79. return;
  80. }
  81. void JpegIO::copyIplToImgColor(IplImage *iplImg, ColorImage *img){
  82. int red = 0, green = 0, blue = 0;
  83. ColorPixel pixel;
  84. for(int x = 0; x < img->getWidth(); x++){
  85. for(int y = 0; y < img->getHeight(); y++){
  86. red = ((uchar*)(iplImg->imageData + iplImg->widthStep*y))[x*3+2];
  87. green = ((uchar*)(iplImg->imageData + iplImg->widthStep*y))[x*3+1];
  88. blue = ((uchar*)(iplImg->imageData + iplImg->widthStep*y))[x*3];
  89. pixel.v1 = red;
  90. pixel.v2 = green;
  91. pixel.v3 = blue;
  92. img->set(x,y,pixel);
  93. }
  94. }
  95. return;
  96. }