Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "letter.h"
- #include "resourcemanager.h"
- #include "opencv2/imgproc/imgproc.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include <FL/Fl.H>
- #include <FL/Fl_Window.H>
- #include <FL/Fl_Box.H>
- #include <FL/fl_draw.H>
- #include <memory.h>
- #include <string>
- #include <vector>
- #include <FL/Enumerations.H>
- class OpenCV_Fl_RGB_Image_Factory {
- public:
- static std::unique_ptr<Fl_RGB_Image> makeImageFrom8U(cv::Mat &src, int mapChans[], int chancount) {
- int rows = src.rows;
- int cols = src.cols;
- cv::Mat img = cv::Mat::zeros(rows, cols, CV_8UC4);
- img.setTo(cv::Scalar(0,0,0,255));
- //int mapChans[] = {0,0,0,3};
- //std vector<int> mapChans;
- //if (copytoR)
- cv::Mat dst[] = {img};
- cv::mixChannels(&src,1,dst,1,mapChans,chancount);
- unsigned char *idata = (unsigned char *)malloc(cols * rows * 4);
- memcpy(idata,(char *)img.data,src.rows*src.cols*4);
- auto ptr = std::make_unique<Fl_RGB_Image>(idata,cols, rows, 4);
- ptr->alloc_array = 1;
- return ptr;
- }
- };
- class MyWindow:public Fl_Window {
- private:
- cv::Mat threshImg_8U;
- cv::Mat checkPixelsImg_8U;
- cv::Mat grayscaleSrcImg_8U; //maybe 8u?
- cv::Mat contours_8U;
- cv::Mat rotThreshImg_8U; //getRectSubPix on rrect and rotate
- cv::RotatedRect rrect;
- std::unique_ptr<Fl_RGB_Image> checkPixels_Fl_Image_;
- std::unique_ptr<Fl_RGB_Image> grayscaleSrc_Fl_Image_;
- std::unique_ptr<Fl_RGB_Image> thresh_Fl_Image_;
- std::unique_ptr<Fl_RGB_Image> contours_Fl_Image_;
- std::unique_ptr<Fl_RGB_Image> rotThreshImg_Fl_Image_;
- public:
- MyWindow(int w, int h, const char* title=0) : Fl_Window(w,h,title) {
- cv::Mat src;
- src = cv::imread("sample2.png");
- //if (!src.data) return -1;
- cv::GaussianBlur(src, src, cv::Size(3,3), 0,0,cv::BORDER_DEFAULT);
- cv::cvtColor(src,grayscaleSrcImg_8U,CV_BGR2GRAY);
- cv::threshold(grayscaleSrcImg_8U,threshImg_8U,96, 255,cv::THRESH_BINARY_INV);
- contours_8U = threshImg_8U.clone();
- //problem domain reduction - find contours and bounding rect
- std::vector<std::vector<cv::Point>> contours;
- std::vector<cv::Vec4i> hierarchy;
- findContours(contours_8U, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
- if (contours.size() > 0) { //use max function?
- double maxAreaFound = 0.0;
- int maxAreaContourIndex = 0;
- int vpos = 0;
- for (auto &contour:contours) { //find rect of contour with largest area
- double a = cv::contourArea(contour);
- if (a > maxAreaContourIndex) {
- maxAreaFound = a;
- maxAreaContourIndex = vpos;
- }
- vpos++;
- }
- rrect = minAreaRect(contours[maxAreaContourIndex]);
- }
- cv::Mat rotThreshImgTemp_8U;
- cv::Mat rotMat = cv::getRotationMatrix2D(rrect.center,rrect.angle,1.0); //get rotation matrix
- cv::warpAffine(threshImg_8U, rotThreshImgTemp_8U, rotMat, threshImg_8U.size(), cv::INTER_CUBIC);
- cv::getRectSubPix(rotThreshImgTemp_8U, rrect.size, rrect.center, rotThreshImg_8U);
- //cv::namedWindow("Test",CV_WINDOW_AUTOSIZE);
- checkPixelsImg_8U = cv::Mat::zeros(src.rows, src.cols, CV_8U);
- unsigned char *data = (unsigned char*) (threshImg_8U.data);
- int enter_x_pos1, enter_y_pos1, exit_x_pos1, exit_y_pos1 = -1;
- /*
- for (int y=0;y < src.cols; y++) {
- enter_x_pos1 = -1;
- for (int x=0;x < src.rows;x++) {
- if (data[(y * src.rows) + x] > 0 && enter_x_pos1 == -1) {
- enter_x_pos1 = x;
- }
- else if (data[(y * src.rows) + x] == 0 && enter_x_pos1 != -1) {
- if (enter_x_pos1 != -1) {
- int widthcheck = x - enter_x_pos1;
- int my = y;
- int destx = x - (widthcheck / 2);
- //std::cout << widthcheck << "\n";
- //if (widthcheck > 16) {
- checkPixelsImg_8U.data[(my * src.rows) + destx] = 255;
- enter_x_pos1 = -1;
- //}
- }
- }
- }
- }*/
- CVToFLTK();
- }
- void CVToFLTK() {
- int rows = grayscaleSrcImg_8U.rows;
- int cols = grayscaleSrcImg_8U.cols;
- int a[] = {0,0,0,3};
- int b[] = {0,0,0,1,0,2};
- int c[] = {0,2,0,3};
- int d[] = {0,0,0,1,0,3};
- checkPixels_Fl_Image_ = OpenCV_Fl_RGB_Image_Factory::makeImageFrom8U(checkPixelsImg_8U, a,2);
- grayscaleSrc_Fl_Image_ = OpenCV_Fl_RGB_Image_Factory::makeImageFrom8U(grayscaleSrcImg_8U,b,3);
- thresh_Fl_Image_ = OpenCV_Fl_RGB_Image_Factory::makeImageFrom8U(threshImg_8U,c,2);
- contours_Fl_Image_ = OpenCV_Fl_RGB_Image_Factory::makeImageFrom8U(contours_8U,d,3);
- rotThreshImg_Fl_Image_ = OpenCV_Fl_RGB_Image_Factory::makeImageFrom8U(rotThreshImg_8U,d,3);
- }
- void draw() {
- //std::cout << " Drawme\n";
- grayscaleSrc_Fl_Image_->draw(0,0);
- //thresh_Fl_Image_->draw(0,0);
- //thresh_Fl_Image_->draw(0,0);
- rotThreshImg_Fl_Image_->draw(0,0);
- std::cout << (uint32_t)rotThreshImg_8U.data[(32 * rotThreshImg_8U.cols) + (32)] << "\n";
- const char *tdat = rotThreshImg_Fl_Image_->data()[0];
- uint32_t a = *(tdat + (32 * rotThreshImg_8U.cols * 4) + (32 * 4));
- std::cout << a << "\n";
- //sobel_Fl_Image_->draw(0,0);
- //checkPixels_Fl_Image_->draw(0,0);
- cv::Point2f rrectPts[4];
- rrect.points(rrectPts);
- fl_color(FL_MAGENTA);
- 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);
- //fl_draw_image(grayscaleSrcImg_.data,0,0,grayscaleSrcImg_.cols,grayscaleSrcImg_.rows,4);
- //fl_draw_image(sobelImg_.data,0,0,sobelImg_.cols,sobelImg_.rows,4);
- //fl_draw_image(checkImg_.data,0,0,checkImg_.cols,checkImg_.rows,4);
- end();
- }
- };
- int main() {
- HWRec::Letter letter;
- HWRec::ResourceManager::getInstance();
- MyWindow window(1024,768);
- window.end();
- window.show();
- Fl::run();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement