Advertisement
Guest User

Untitled

a guest
Jan 5th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include "letter.h"
  4. #include "resourcemanager.h"
  5. #include "opencv2/imgproc/imgproc.hpp"
  6. #include "opencv2/highgui/highgui.hpp"
  7. #include <FL/Fl.H>
  8. #include <FL/Fl_Window.H>
  9. #include <FL/Fl_Box.H>
  10. #include <FL/fl_draw.H>
  11. #include <memory.h>
  12. #include <string>
  13. #include <vector>
  14. #include <FL/Enumerations.H>
  15.  
  16. class OpenCV_Fl_RGB_Image_Factory {
  17. public:
  18. static std::unique_ptr<Fl_RGB_Image> makeImageFrom8U(cv::Mat &src, int mapChans[], int chancount) {
  19. int rows = src.rows;
  20. int cols = src.cols;
  21.  
  22. cv::Mat img = cv::Mat::zeros(rows, cols, CV_8UC4);
  23. img.setTo(cv::Scalar(0,0,0,255));
  24. //int mapChans[] = {0,0,0,3};
  25. //std vector<int> mapChans;
  26. //if (copytoR)
  27. cv::Mat dst[] = {img};
  28. cv::mixChannels(&src,1,dst,1,mapChans,chancount);
  29. unsigned char *idata = (unsigned char *)malloc(cols * rows * 4);
  30. memcpy(idata,(char *)img.data,src.rows*src.cols*4);
  31. auto ptr = std::make_unique<Fl_RGB_Image>(idata,cols, rows, 4);
  32. ptr->alloc_array = 1;
  33. return ptr;
  34. }
  35.  
  36. };
  37.  
  38. class MyWindow:public Fl_Window {
  39. private:
  40. cv::Mat threshImg_8U;
  41. cv::Mat checkPixelsImg_8U;
  42. cv::Mat grayscaleSrcImg_8U; //maybe 8u?
  43. cv::Mat contours_8U;
  44. cv::Mat rotThreshImg_8U; //getRectSubPix on rrect and rotate
  45.  
  46. cv::RotatedRect rrect;
  47.  
  48. std::unique_ptr<Fl_RGB_Image> checkPixels_Fl_Image_;
  49. std::unique_ptr<Fl_RGB_Image> grayscaleSrc_Fl_Image_;
  50. std::unique_ptr<Fl_RGB_Image> thresh_Fl_Image_;
  51. std::unique_ptr<Fl_RGB_Image> contours_Fl_Image_;
  52. std::unique_ptr<Fl_RGB_Image> rotThreshImg_Fl_Image_;
  53.  
  54. public:
  55. MyWindow(int w, int h, const char* title=0) : Fl_Window(w,h,title) {
  56. cv::Mat src;
  57.  
  58. src = cv::imread("sample2.png");
  59. //if (!src.data) return -1;
  60.  
  61. cv::GaussianBlur(src, src, cv::Size(3,3), 0,0,cv::BORDER_DEFAULT);
  62. cv::cvtColor(src,grayscaleSrcImg_8U,CV_BGR2GRAY);
  63. cv::threshold(grayscaleSrcImg_8U,threshImg_8U,96, 255,cv::THRESH_BINARY_INV);
  64. contours_8U = threshImg_8U.clone();
  65. //problem domain reduction - find contours and bounding rect
  66. std::vector<std::vector<cv::Point>> contours;
  67. std::vector<cv::Vec4i> hierarchy;
  68.  
  69. findContours(contours_8U, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
  70. if (contours.size() > 0) { //use max function?
  71. double maxAreaFound = 0.0;
  72. int maxAreaContourIndex = 0;
  73. int vpos = 0;
  74. for (auto &contour:contours) { //find rect of contour with largest area
  75. double a = cv::contourArea(contour);
  76. if (a > maxAreaContourIndex) {
  77. maxAreaFound = a;
  78. maxAreaContourIndex = vpos;
  79. }
  80. vpos++;
  81. }
  82. rrect = minAreaRect(contours[maxAreaContourIndex]);
  83. }
  84. cv::Mat rotThreshImgTemp_8U;
  85.  
  86.  
  87. cv::Mat rotMat = cv::getRotationMatrix2D(rrect.center,rrect.angle,1.0); //get rotation matrix
  88. cv::warpAffine(threshImg_8U, rotThreshImgTemp_8U, rotMat, threshImg_8U.size(), cv::INTER_CUBIC);
  89.  
  90. cv::getRectSubPix(rotThreshImgTemp_8U, rrect.size, rrect.center, rotThreshImg_8U);
  91.  
  92. //cv::namedWindow("Test",CV_WINDOW_AUTOSIZE);
  93.  
  94.  
  95. checkPixelsImg_8U = cv::Mat::zeros(src.rows, src.cols, CV_8U);
  96.  
  97. unsigned char *data = (unsigned char*) (threshImg_8U.data);
  98.  
  99. int enter_x_pos1, enter_y_pos1, exit_x_pos1, exit_y_pos1 = -1;
  100.  
  101. /*
  102. for (int y=0;y < src.cols; y++) {
  103. enter_x_pos1 = -1;
  104. for (int x=0;x < src.rows;x++) {
  105.  
  106. if (data[(y * src.rows) + x] > 0 && enter_x_pos1 == -1) {
  107. enter_x_pos1 = x;
  108. }
  109. else if (data[(y * src.rows) + x] == 0 && enter_x_pos1 != -1) {
  110. if (enter_x_pos1 != -1) {
  111. int widthcheck = x - enter_x_pos1;
  112. int my = y;
  113. int destx = x - (widthcheck / 2);
  114. //std::cout << widthcheck << "\n";
  115. //if (widthcheck > 16) {
  116. checkPixelsImg_8U.data[(my * src.rows) + destx] = 255;
  117. enter_x_pos1 = -1;
  118. //}
  119.  
  120. }
  121. }
  122. }
  123.  
  124. }*/
  125. CVToFLTK();
  126. }
  127.  
  128. void CVToFLTK() {
  129. int rows = grayscaleSrcImg_8U.rows;
  130. int cols = grayscaleSrcImg_8U.cols;
  131. int a[] = {0,0,0,3};
  132. int b[] = {0,0,0,1,0,2};
  133. int c[] = {0,2,0,3};
  134. int d[] = {0,0,0,1,0,3};
  135. checkPixels_Fl_Image_ = OpenCV_Fl_RGB_Image_Factory::makeImageFrom8U(checkPixelsImg_8U, a,2);
  136. grayscaleSrc_Fl_Image_ = OpenCV_Fl_RGB_Image_Factory::makeImageFrom8U(grayscaleSrcImg_8U,b,3);
  137. thresh_Fl_Image_ = OpenCV_Fl_RGB_Image_Factory::makeImageFrom8U(threshImg_8U,c,2);
  138. contours_Fl_Image_ = OpenCV_Fl_RGB_Image_Factory::makeImageFrom8U(contours_8U,d,3);
  139. rotThreshImg_Fl_Image_ = OpenCV_Fl_RGB_Image_Factory::makeImageFrom8U(rotThreshImg_8U,d,3);
  140. }
  141.  
  142. void draw() {
  143. //std::cout << " Drawme\n";
  144. grayscaleSrc_Fl_Image_->draw(0,0);
  145. //thresh_Fl_Image_->draw(0,0);
  146.  
  147. //thresh_Fl_Image_->draw(0,0);
  148. rotThreshImg_Fl_Image_->draw(0,0);
  149.  
  150. std::cout << (uint32_t)rotThreshImg_8U.data[(32 * rotThreshImg_8U.cols) + (32)] << "\n";
  151. const char *tdat = rotThreshImg_Fl_Image_->data()[0];
  152. uint32_t a = *(tdat + (32 * rotThreshImg_8U.cols * 4) + (32 * 4));
  153. std::cout << a << "\n";
  154.  
  155. //sobel_Fl_Image_->draw(0,0);
  156. //checkPixels_Fl_Image_->draw(0,0);
  157.  
  158. cv::Point2f rrectPts[4];
  159. rrect.points(rrectPts);
  160. fl_color(FL_MAGENTA);
  161. fl_loop(rrectPts[0].x,rrectPts[0].y,rrectPts[1].x,rrectPts[1].y,rrectPts[2].x,rrectPts[2].y,rrectPts[3].x,rrectPts[3].y);
  162. //fl_draw_image(grayscaleSrcImg_.data,0,0,grayscaleSrcImg_.cols,grayscaleSrcImg_.rows,4);
  163. //fl_draw_image(sobelImg_.data,0,0,sobelImg_.cols,sobelImg_.rows,4);
  164. //fl_draw_image(checkImg_.data,0,0,checkImg_.cols,checkImg_.rows,4);
  165. end();
  166.  
  167. }
  168. };
  169.  
  170. int main() {
  171.  
  172. HWRec::Letter letter;
  173. HWRec::ResourceManager::getInstance();
  174.  
  175. MyWindow window(1024,768);
  176. window.end();
  177. window.show();
  178. Fl::run();
  179.  
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement