Menu

cv::IplImage* to array2d<rgb_pixel>

Help
tjusxh
2014-10-20
2015-05-07
  • tjusxh

    tjusxh - 2014-10-20

    hi,I can't figure out how to convert my OpenCV image into dlib-processable img.

    http://dlib.net/face_landmark_detection_ex.cpp.html sp(img,dets[j]

    Cheers

     

    Last edit: tjusxh 2014-10-20
    • Davis

      Davis - 2014-10-20

      You can convert an OpenCV image into a dlib compatible image by doing
      something like this:

      cv_image<bgr_pixel> img(your_opencv_image);

      Then you use img in dlib functions. See also:
      http://dlib.net/imaging.html#cv_image

      Cheers,
      Davis

       
  • tjusxh

    tjusxh - 2014-10-22

    it done!
    Thanks

     
  • ANKUR

    ANKUR - 2014-11-25

    hi, I am new to dlib and so when load image didn't worked out for other than .bmp image so I have tried to use OpenCV to read that and then convert it back to the matrix as well as array2d format of dlib but it throws an exception saying "The pixel type you gave doesn't match pixel used by the open cv Mat object."

    I have written the code as:
    Mat src = imread(argv[1],CV_LOAD_IMAGE_COLOR);
    cv_image<rgb_pixel> img(src);
    save_bmp(img,"input.bmp");

    and similarly I have tried after reading an bmp image read from dlib and then do some binarisation operations in opencv using toMat but same problem faced when I am converting it back to dlib format..

     
    • Davis

      Davis - 2014-11-25

      You probably need to use bgr_pixel instead of rgb_pixel. Also, if you use
      cmake to compile (as shown here http://dlib.net/compile.html) your project
      it will automatically link in libjpeg and libpng which will allow you to
      load and save jpeg and png files with dlib.

      Cheers,
      Davis

       
  • ANKUR

    ANKUR - 2014-11-26

    Thanks Davis , but it didn't worked with bgr_pixel also and then I tried the other way which works for me. Which is I have changed the line 3 of my code as :
    assign_image(img, cv_image<bgr_pixel>(src));

    Now I am getting into another problem of grayscale conversion using same assign_image.
    The code I have tried is:

                matrix<bgr_pixel>rot_img;
        rotate_image(img,rot_img,-0.785); // Rotation 
        save_bmp(rot_img,"rotated_image.bmp");
    
        // Grayscale conversion of image
        matrix<unsigned char> GRAY_CONV;  assign_pixel( GRAY_CONV,rot_img);
        save_bmp(GRAY_CONV, "grayscale_image.bmp");
    

    I have checked the rot_img is good but assign_pixel line is giving some kind of compile time errors which I am not getting is what for. and the compile time errors are:

    Error 1 error C2893: Failed to specialize function template 'enable_if_c<dlib::pixel_traits<P>::hsi&&dlib::pixel_traits<P>::rgb_alpha>::type dlib::assign_pixel_helpers::assign(P1 &,const P2 &)' d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check
    Error 2 error C2893: Failed to specialize function template 'enable_if_c<dlib::pixel_traits<P>::hsi&&dlib::pixel_traits<P>::rgb>::type dlib::assign_pixel_helpers::assign(P1 &,const P2 &)' d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check
    Error 3 error C2893: Failed to specialize function template 'enable_if_c<dlib::pixel_traits<P>::hsi&&dlib::pixel_traits<P>::grayscale>::type dlib::assign_pixel_helpers::assign(P1 &,const P2 &)' d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check
    Error 4 error C2893: Failed to specialize function template 'enable_if_c<dlib::pixel_traits<P>::hsi>::type dlib::assign_pixel_helpers::assign(P1 &,const unsigned char &)' d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check
    Error 5 error C2893: Failed to specialize function template 'enable_if_c<dlib::pixel_traits<P>::rgb_alpha&&dlib::pixel_traits<P>::hsi>::type dlib::assign_pixel_helpers::assign(P1 &,const P2 &)' d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check
    Error 6 error C2893: Failed to specialize function template 'enable_if_c<dlib::pixel_traits<P>::rgb_alpha&&dlib::pixel_traits<P>::rgb>::type dlib::assign_pixel_helpers::assign(P1 &,const P2 &)' d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check
    Error 7 error C2893: Failed to specialize function template 'enable_if_c<dlib::pixel_traits<P>::rgb_alpha&&dlib::pixel_traits<P>::grayscale>::type dlib::assign_pixel_helpers::assign(P1 &,const P2 &)' d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check
    Error 8 error C2893: Failed to specialize function template 'enable_if_c<dlib::pixel_traits<P>::rgb_alpha>::type dlib::assign_pixel_helpers::assign(P1 &,const unsigned char &)' d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check
    Error 9 error C2893: Failed to specialize function template 'enable_if_c<dlib::pixel_traits<P>::rgb&&dlib::pixel_traits<P>::hsi>::type dlib::assign_pixel_helpers::assign(P1 &,const P2 &)' d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check
    Error 10 error C2893: Failed to specialize function template 'enable_if_c<dlib::pixel_traits<P>::rgb&&dlib::pixel_traits<P>::rgb_alpha>::type dlib::assign_pixel_helpers::assign(P1 &,const P2 &)' d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check
    Error 11 error C2893: Failed to specialize function template 'enable_if_c<dlib::pixel_traits<P>::rgb&&dlib::pixel_traits<P>::grayscale>::type dlib::assign_pixel_helpers::assign(P1 &,const P2 &)' d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check
    Error 12 error C2893: Failed to specialize function template 'enable_if_c<dlib::pixel_traits<P>::rgb>::type dlib::assign_pixel_helpers::assign(P1 &,const unsigned char &)' d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check
    Error 13 error C2893: Failed to specialize function template 'enable_if_c<dlib::pixel_traits<P>::grayscale&&dlib::pixel_traits<P>::hsi>::type dlib::assign_pixel_helpers::assign(P1 &,const P2 &)' d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check
    Error 14 error C2893: Failed to specialize function template 'enable_if_c<dlib::pixel_traits<P>::grayscale&&dlib::pixel_traits<P>::rgb_alpha>::type dlib::assign_pixel_helpers::assign(P1 &,const P2 &)' d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check
    Error 15 error C2893: Failed to specialize function template 'enable_if_c<dlib::pixel_traits<P>::grayscale&&dlib::pixel_traits<P>::rgb>::type dlib::assign_pixel_helpers::assign(P1 &,const P2 &)' d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check
    Error 16 error C2893: Failed to specialize function template 'enable_if_c<dlib::pixel_traits<P>::hsi&&dlib::pixel_traits<P>::hsi>::type dlib::assign_pixel_helpers::assign(P1 &,const P2 &)' d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check
    Error 17 error C2893: Failed to specialize function template 'enable_if_c<dlib::pixel_traits<P>::rgb_alpha&&dlib::pixel_traits<P>::rgb_alpha>::type dlib::assign_pixel_helpers::assign(P1 &,const P2 &)' d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check
    Error 18 error C2893: Failed to specialize function template 'enable_if_c<dlib::pixel_traits<P>::rgb&&dlib::pixel_traits<P>::rgb>::type dlib::assign_pixel_helpers::assign(P1 &,const P2 &)' d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check
    Error 19 error C2893: Failed to specialize function template 'enable_if_c<dlib::pixel_traits<P>::grayscale&&dlib::pixel_traits<P>::grayscale>::type dlib::assign_pixel_helpers::assign(P1 &,const P2 &)' d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check
    Error 20 error C2782: 'enable_if_c<dlib::pixel_traits<P>::grayscale>::type dlib::assign_pixel_helpers::assign(P &,const P &)' : template parameter 'P' is ambiguous d:\dlib\dlib-18.11\dlib\pixel.h 966 1 DLib_check

     
  • ANKUR

    ANKUR - 2014-11-26

    One more help I need from you I want to try for morphological operations but I am not been able to define a structuring element for the operation. If possible give me an example for a simple morphological operation in dlib.

    And when I have given a image for the auto_threshold_image but its giving error:

    And when I have given same image for the equalize_histogram it worked out good. I didn't underatnd why is it so?.... The code I have used this:

        matrix<unsigned char>Binarised;
        //auto_threshold_image(rot_img,Binarised);
        equalize_histogram(rot_img,Binarised);
    
     
    • Davis

      Davis - 2014-11-26

      You just do it like this:
      unsigned char structuring_element[3][3];
      // then assign each part of structuring_element to either 0 or 255 (i.e.
      on_pixel or off_pixel) depending on the kind of morphology operation you
      want to do.

      Yeah, there is a bug in the auto_threshold_image() function. It's fixed in
      the source repository though so if you grab a new copy of the file (
      https://github.com/davisking/dlib/blob/master/dlib/image_transforms/thresholding.h)
      it will work fine.

      Cheers,
      Davis

       

      Last edit: Davis 2014-11-26
      • ANKUR

        ANKUR - 2014-11-27

        Thanks Davis, auto_thresold_image worked like a charm but I am still facing the issue in morphological operations. I tried this :

            matrix<unsigned char> Morph_out;
        unsigned char structuring_element[3][3] = {{0,255,0},{255,255,255},{0,255,0}};
            binary_dilation(edge_image,Morph_out,structuring_element);
        

        where edge_image is of

             matrix<unsigned int> edge_image;
        

        and still I am getting some kind of error. Have a look and tell me if I am doing wrong anywhere.

         
        • Davis

          Davis - 2014-11-27

          That should work fine. What error do you get when you try it?

           
          • ANKUR

            ANKUR - 2014-11-27

            Thanks for the quick reply, Davis.

             
  • ANKUR

    ANKUR - 2014-11-27

    I worked it out. I changed the edge_image Pixel type to unsigned char and then it worked out ,i.e., build successfully with intellisense error like png attached before). But I didn't understand why edge_image was failed to be recognised as an binary image when in is_binary_image() of binary_dilation. I obtained the edge_image as a result from edge finding of an binarised Image.

    One more thing the one I have posted before for cv_image that thing I have solved like this:

    assign_image(img, cv_image<bgr_pixel>(src));
    

    as bgr_pixel also didn't worked for me.
    And now I am getting error in rgb to grayscale conversion using same.

            matrix<unsigned char> GRAY_CONV;
        assign_pixel( GRAY_CONV,rot_img);
    
     
    • Davis

      Davis - 2014-11-27

      What is rot_img?

       

      Last edit: Davis 2014-11-27
      • ANKUR

        ANKUR - 2014-11-27
            matrix<bgr_pixel>rot_img;
            rotate_image(blurred_img_test,rot_img,-0.785); // Rotation by 45 deg
            save_bmp(rot_img,"rotated_image.bmp");
        
         
        • Davis

          Davis - 2014-11-27

          It should work then. That is, if you have this in your code:

              matrix<bgr_pixel> img;
              matrix<unsigned char> out;
              // do whatever you want to img and out.
              assign_image(out, img);
          

          Then there should be no problem. What is the smallest program you can write that reproduces the error?

           

          Last edit: Davis 2014-11-27
          • ANKUR

            ANKUR - 2014-11-27

            I have checked it same way but still I am getting error check the .png I have attached with post before for more details.

             
            • Davis

              Davis - 2014-11-27

              The .png isn't very useful. I need to see the exact code you are compiling.

               
  • ANKUR

    ANKUR - 2014-11-27

    ok... then Check Test.cpp attached document file for example code where I am getting error.

     
    • Davis

      Davis - 2014-11-27

      Ah, sorry, I wasn't paying enough attention. You need to call assign_image(), not assign_pixel(). assign_pixel is for assigning pixels, not images.

       
      • ANKUR

        ANKUR - 2014-11-27

        Ah, what a stupidity... Well thanks for help...

         
        • Davis

          Davis - 2014-11-27

          No problem :)

           

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.