How to convert Matrix to a type Mat?











up vote
-1
down vote

favorite












I am using EmguCV to do simple image processing. I want to remove small contours from my thresholded image so I used connectedcomponentswithstats. Below is my code where I convert any pixel smaller than the minimum size into black - essentially deleting it. The reason I want to do this is to be able to use it in the latter part since it is not compatible with other types.



public Mat Filtered(Mat in_img, string currentDir)
{
Mat gray_res = new Mat();
Mat labels = new Mat();
Mat stats = new Mat();
Mat centroids = new Mat();

CvInvoke.CvtColor(in_img, gray_res, ColorConversion.Bgr2Gray);

var nlabels = CvInvoke.ConnectedComponentsWithStats(gray_res, labels, stats, centroids, LineType.EightConnected);

int min_size = 250;

int sizes = new int[nlabels];

for (int s = 1; s < nlabels; s++)
{
sizes[s] = BitConverter.ToInt32(stats.GetData(s, (int)ConnectedComponentsTypes.Area), 0);
}

Matrix<double> fltr_img = new Matrix<double>(labels.Rows, labels.Cols);
fltr_img.SetValue(0);

for (int i = 1; i < nlabels; i++)
{
int j = BitConverter.ToInt32(stats.GetData(i, (int)ConnectedComponentsTypes.Left), 0); //left most
int w = BitConverter.ToInt32(stats.GetData(i, (int)ConnectedComponentsTypes.Width), 0); //run width
int k = BitConverter.ToInt32(stats.GetData(i, (int)ConnectedComponentsTypes.Top), 0); //top most
int h = BitConverter.ToInt32(stats.GetData(i, (int)ConnectedComponentsTypes.Height), 0); //run height

if (sizes[i] < min_size)
{
for (int a = k; a < k + h; a++)
{
for (int b = j; b < j + w; b++)
{
fltr_img.Data[a, b] = 255;
}
}
}
}

//Covnert 2D Matrix to Image
CvInvoke.Imwrite(currentDir + "filter.jpg", fltr_img);

Mat subtrahend = CvInvoke.Imread(currentDir + "filter.jpg");

Mat fnl_img = new Mat();

CvInvoke.Subtract(in_img, subtrahend, fnl_img);

return fnl_img;
}


What I did, in the end, is a little dirty trick to convert the mask into a compatible type Mat which is the original image. With this code, I am getting my result but I am still bothered by it.










share|improve this question




























    up vote
    -1
    down vote

    favorite












    I am using EmguCV to do simple image processing. I want to remove small contours from my thresholded image so I used connectedcomponentswithstats. Below is my code where I convert any pixel smaller than the minimum size into black - essentially deleting it. The reason I want to do this is to be able to use it in the latter part since it is not compatible with other types.



    public Mat Filtered(Mat in_img, string currentDir)
    {
    Mat gray_res = new Mat();
    Mat labels = new Mat();
    Mat stats = new Mat();
    Mat centroids = new Mat();

    CvInvoke.CvtColor(in_img, gray_res, ColorConversion.Bgr2Gray);

    var nlabels = CvInvoke.ConnectedComponentsWithStats(gray_res, labels, stats, centroids, LineType.EightConnected);

    int min_size = 250;

    int sizes = new int[nlabels];

    for (int s = 1; s < nlabels; s++)
    {
    sizes[s] = BitConverter.ToInt32(stats.GetData(s, (int)ConnectedComponentsTypes.Area), 0);
    }

    Matrix<double> fltr_img = new Matrix<double>(labels.Rows, labels.Cols);
    fltr_img.SetValue(0);

    for (int i = 1; i < nlabels; i++)
    {
    int j = BitConverter.ToInt32(stats.GetData(i, (int)ConnectedComponentsTypes.Left), 0); //left most
    int w = BitConverter.ToInt32(stats.GetData(i, (int)ConnectedComponentsTypes.Width), 0); //run width
    int k = BitConverter.ToInt32(stats.GetData(i, (int)ConnectedComponentsTypes.Top), 0); //top most
    int h = BitConverter.ToInt32(stats.GetData(i, (int)ConnectedComponentsTypes.Height), 0); //run height

    if (sizes[i] < min_size)
    {
    for (int a = k; a < k + h; a++)
    {
    for (int b = j; b < j + w; b++)
    {
    fltr_img.Data[a, b] = 255;
    }
    }
    }
    }

    //Covnert 2D Matrix to Image
    CvInvoke.Imwrite(currentDir + "filter.jpg", fltr_img);

    Mat subtrahend = CvInvoke.Imread(currentDir + "filter.jpg");

    Mat fnl_img = new Mat();

    CvInvoke.Subtract(in_img, subtrahend, fnl_img);

    return fnl_img;
    }


    What I did, in the end, is a little dirty trick to convert the mask into a compatible type Mat which is the original image. With this code, I am getting my result but I am still bothered by it.










    share|improve this question


























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      I am using EmguCV to do simple image processing. I want to remove small contours from my thresholded image so I used connectedcomponentswithstats. Below is my code where I convert any pixel smaller than the minimum size into black - essentially deleting it. The reason I want to do this is to be able to use it in the latter part since it is not compatible with other types.



      public Mat Filtered(Mat in_img, string currentDir)
      {
      Mat gray_res = new Mat();
      Mat labels = new Mat();
      Mat stats = new Mat();
      Mat centroids = new Mat();

      CvInvoke.CvtColor(in_img, gray_res, ColorConversion.Bgr2Gray);

      var nlabels = CvInvoke.ConnectedComponentsWithStats(gray_res, labels, stats, centroids, LineType.EightConnected);

      int min_size = 250;

      int sizes = new int[nlabels];

      for (int s = 1; s < nlabels; s++)
      {
      sizes[s] = BitConverter.ToInt32(stats.GetData(s, (int)ConnectedComponentsTypes.Area), 0);
      }

      Matrix<double> fltr_img = new Matrix<double>(labels.Rows, labels.Cols);
      fltr_img.SetValue(0);

      for (int i = 1; i < nlabels; i++)
      {
      int j = BitConverter.ToInt32(stats.GetData(i, (int)ConnectedComponentsTypes.Left), 0); //left most
      int w = BitConverter.ToInt32(stats.GetData(i, (int)ConnectedComponentsTypes.Width), 0); //run width
      int k = BitConverter.ToInt32(stats.GetData(i, (int)ConnectedComponentsTypes.Top), 0); //top most
      int h = BitConverter.ToInt32(stats.GetData(i, (int)ConnectedComponentsTypes.Height), 0); //run height

      if (sizes[i] < min_size)
      {
      for (int a = k; a < k + h; a++)
      {
      for (int b = j; b < j + w; b++)
      {
      fltr_img.Data[a, b] = 255;
      }
      }
      }
      }

      //Covnert 2D Matrix to Image
      CvInvoke.Imwrite(currentDir + "filter.jpg", fltr_img);

      Mat subtrahend = CvInvoke.Imread(currentDir + "filter.jpg");

      Mat fnl_img = new Mat();

      CvInvoke.Subtract(in_img, subtrahend, fnl_img);

      return fnl_img;
      }


      What I did, in the end, is a little dirty trick to convert the mask into a compatible type Mat which is the original image. With this code, I am getting my result but I am still bothered by it.










      share|improve this question















      I am using EmguCV to do simple image processing. I want to remove small contours from my thresholded image so I used connectedcomponentswithstats. Below is my code where I convert any pixel smaller than the minimum size into black - essentially deleting it. The reason I want to do this is to be able to use it in the latter part since it is not compatible with other types.



      public Mat Filtered(Mat in_img, string currentDir)
      {
      Mat gray_res = new Mat();
      Mat labels = new Mat();
      Mat stats = new Mat();
      Mat centroids = new Mat();

      CvInvoke.CvtColor(in_img, gray_res, ColorConversion.Bgr2Gray);

      var nlabels = CvInvoke.ConnectedComponentsWithStats(gray_res, labels, stats, centroids, LineType.EightConnected);

      int min_size = 250;

      int sizes = new int[nlabels];

      for (int s = 1; s < nlabels; s++)
      {
      sizes[s] = BitConverter.ToInt32(stats.GetData(s, (int)ConnectedComponentsTypes.Area), 0);
      }

      Matrix<double> fltr_img = new Matrix<double>(labels.Rows, labels.Cols);
      fltr_img.SetValue(0);

      for (int i = 1; i < nlabels; i++)
      {
      int j = BitConverter.ToInt32(stats.GetData(i, (int)ConnectedComponentsTypes.Left), 0); //left most
      int w = BitConverter.ToInt32(stats.GetData(i, (int)ConnectedComponentsTypes.Width), 0); //run width
      int k = BitConverter.ToInt32(stats.GetData(i, (int)ConnectedComponentsTypes.Top), 0); //top most
      int h = BitConverter.ToInt32(stats.GetData(i, (int)ConnectedComponentsTypes.Height), 0); //run height

      if (sizes[i] < min_size)
      {
      for (int a = k; a < k + h; a++)
      {
      for (int b = j; b < j + w; b++)
      {
      fltr_img.Data[a, b] = 255;
      }
      }
      }
      }

      //Covnert 2D Matrix to Image
      CvInvoke.Imwrite(currentDir + "filter.jpg", fltr_img);

      Mat subtrahend = CvInvoke.Imread(currentDir + "filter.jpg");

      Mat fnl_img = new Mat();

      CvInvoke.Subtract(in_img, subtrahend, fnl_img);

      return fnl_img;
      }


      What I did, in the end, is a little dirty trick to convert the mask into a compatible type Mat which is the original image. With this code, I am getting my result but I am still bothered by it.







      c# matrix emgucv mat emgu






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 at 14:07









      Uwe Keim

      27.3k30128210




      27.3k30128210










      asked Nov 13 at 14:05









      cassiopeiaofthemadworld

      116




      116
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You can convert Matrix type by using its Mat property. I don't know what type you need so I used DepthType.Cv8U in my example.



          Mat converted = new Mat();
          fltr_img.Mat.ConvertTo(converted, DepthType.Cv8U);
          Mat fnl_img = new Mat();
          CvInvoke.Subtract(in_img, converted, fnl_img);


          Alternatively, you can also use Mat and/or Matrix together with substract method. Both implement IInpurArray interface.



          CvInvoke.Subtract(in_img,  fltr_img, fnl_img); //Tested with EmguCv 3.4.1





          share|improve this answer























          • It doesn't work. What I want is to convert the Matrix<> format (which I used to create the filter as seen in the for loop above) into a something compatible with the Mat format (which I used to load the image) so that I can use EmguCV subtract to get the desired result.
            – cassiopeiaofthemadworld
            Nov 23 at 2:06










          • Matrix and Mat can be both used in CvInvoke.Subtract(IInputArray src1, IInputArray src2, IOutputArray dst) without compilation errors as both implement IInputArray interface. I tested it with EmguCV 3.4.1. So i do not understand why you have to convert Matrix to Mat.
            – Quergo
            Nov 23 at 9:41










          • When I used your code. It throws this exception ---> Exception thrown: 'Emgu.CV.Util.CvException' in Emgu.CV.World.dll
            – cassiopeiaofthemadworld
            Nov 23 at 9:49












          • Does the exception provides more detailed information?
            – Quergo
            Nov 23 at 10:27











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282795%2fhow-to-convert-matrix-to-a-type-mat%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          You can convert Matrix type by using its Mat property. I don't know what type you need so I used DepthType.Cv8U in my example.



          Mat converted = new Mat();
          fltr_img.Mat.ConvertTo(converted, DepthType.Cv8U);
          Mat fnl_img = new Mat();
          CvInvoke.Subtract(in_img, converted, fnl_img);


          Alternatively, you can also use Mat and/or Matrix together with substract method. Both implement IInpurArray interface.



          CvInvoke.Subtract(in_img,  fltr_img, fnl_img); //Tested with EmguCv 3.4.1





          share|improve this answer























          • It doesn't work. What I want is to convert the Matrix<> format (which I used to create the filter as seen in the for loop above) into a something compatible with the Mat format (which I used to load the image) so that I can use EmguCV subtract to get the desired result.
            – cassiopeiaofthemadworld
            Nov 23 at 2:06










          • Matrix and Mat can be both used in CvInvoke.Subtract(IInputArray src1, IInputArray src2, IOutputArray dst) without compilation errors as both implement IInputArray interface. I tested it with EmguCV 3.4.1. So i do not understand why you have to convert Matrix to Mat.
            – Quergo
            Nov 23 at 9:41










          • When I used your code. It throws this exception ---> Exception thrown: 'Emgu.CV.Util.CvException' in Emgu.CV.World.dll
            – cassiopeiaofthemadworld
            Nov 23 at 9:49












          • Does the exception provides more detailed information?
            – Quergo
            Nov 23 at 10:27















          up vote
          0
          down vote













          You can convert Matrix type by using its Mat property. I don't know what type you need so I used DepthType.Cv8U in my example.



          Mat converted = new Mat();
          fltr_img.Mat.ConvertTo(converted, DepthType.Cv8U);
          Mat fnl_img = new Mat();
          CvInvoke.Subtract(in_img, converted, fnl_img);


          Alternatively, you can also use Mat and/or Matrix together with substract method. Both implement IInpurArray interface.



          CvInvoke.Subtract(in_img,  fltr_img, fnl_img); //Tested with EmguCv 3.4.1





          share|improve this answer























          • It doesn't work. What I want is to convert the Matrix<> format (which I used to create the filter as seen in the for loop above) into a something compatible with the Mat format (which I used to load the image) so that I can use EmguCV subtract to get the desired result.
            – cassiopeiaofthemadworld
            Nov 23 at 2:06










          • Matrix and Mat can be both used in CvInvoke.Subtract(IInputArray src1, IInputArray src2, IOutputArray dst) without compilation errors as both implement IInputArray interface. I tested it with EmguCV 3.4.1. So i do not understand why you have to convert Matrix to Mat.
            – Quergo
            Nov 23 at 9:41










          • When I used your code. It throws this exception ---> Exception thrown: 'Emgu.CV.Util.CvException' in Emgu.CV.World.dll
            – cassiopeiaofthemadworld
            Nov 23 at 9:49












          • Does the exception provides more detailed information?
            – Quergo
            Nov 23 at 10:27













          up vote
          0
          down vote










          up vote
          0
          down vote









          You can convert Matrix type by using its Mat property. I don't know what type you need so I used DepthType.Cv8U in my example.



          Mat converted = new Mat();
          fltr_img.Mat.ConvertTo(converted, DepthType.Cv8U);
          Mat fnl_img = new Mat();
          CvInvoke.Subtract(in_img, converted, fnl_img);


          Alternatively, you can also use Mat and/or Matrix together with substract method. Both implement IInpurArray interface.



          CvInvoke.Subtract(in_img,  fltr_img, fnl_img); //Tested with EmguCv 3.4.1





          share|improve this answer














          You can convert Matrix type by using its Mat property. I don't know what type you need so I used DepthType.Cv8U in my example.



          Mat converted = new Mat();
          fltr_img.Mat.ConvertTo(converted, DepthType.Cv8U);
          Mat fnl_img = new Mat();
          CvInvoke.Subtract(in_img, converted, fnl_img);


          Alternatively, you can also use Mat and/or Matrix together with substract method. Both implement IInpurArray interface.



          CvInvoke.Subtract(in_img,  fltr_img, fnl_img); //Tested with EmguCv 3.4.1






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 at 9:43

























          answered Nov 21 at 11:15









          Quergo

          335213




          335213












          • It doesn't work. What I want is to convert the Matrix<> format (which I used to create the filter as seen in the for loop above) into a something compatible with the Mat format (which I used to load the image) so that I can use EmguCV subtract to get the desired result.
            – cassiopeiaofthemadworld
            Nov 23 at 2:06










          • Matrix and Mat can be both used in CvInvoke.Subtract(IInputArray src1, IInputArray src2, IOutputArray dst) without compilation errors as both implement IInputArray interface. I tested it with EmguCV 3.4.1. So i do not understand why you have to convert Matrix to Mat.
            – Quergo
            Nov 23 at 9:41










          • When I used your code. It throws this exception ---> Exception thrown: 'Emgu.CV.Util.CvException' in Emgu.CV.World.dll
            – cassiopeiaofthemadworld
            Nov 23 at 9:49












          • Does the exception provides more detailed information?
            – Quergo
            Nov 23 at 10:27


















          • It doesn't work. What I want is to convert the Matrix<> format (which I used to create the filter as seen in the for loop above) into a something compatible with the Mat format (which I used to load the image) so that I can use EmguCV subtract to get the desired result.
            – cassiopeiaofthemadworld
            Nov 23 at 2:06










          • Matrix and Mat can be both used in CvInvoke.Subtract(IInputArray src1, IInputArray src2, IOutputArray dst) without compilation errors as both implement IInputArray interface. I tested it with EmguCV 3.4.1. So i do not understand why you have to convert Matrix to Mat.
            – Quergo
            Nov 23 at 9:41










          • When I used your code. It throws this exception ---> Exception thrown: 'Emgu.CV.Util.CvException' in Emgu.CV.World.dll
            – cassiopeiaofthemadworld
            Nov 23 at 9:49












          • Does the exception provides more detailed information?
            – Quergo
            Nov 23 at 10:27
















          It doesn't work. What I want is to convert the Matrix<> format (which I used to create the filter as seen in the for loop above) into a something compatible with the Mat format (which I used to load the image) so that I can use EmguCV subtract to get the desired result.
          – cassiopeiaofthemadworld
          Nov 23 at 2:06




          It doesn't work. What I want is to convert the Matrix<> format (which I used to create the filter as seen in the for loop above) into a something compatible with the Mat format (which I used to load the image) so that I can use EmguCV subtract to get the desired result.
          – cassiopeiaofthemadworld
          Nov 23 at 2:06












          Matrix and Mat can be both used in CvInvoke.Subtract(IInputArray src1, IInputArray src2, IOutputArray dst) without compilation errors as both implement IInputArray interface. I tested it with EmguCV 3.4.1. So i do not understand why you have to convert Matrix to Mat.
          – Quergo
          Nov 23 at 9:41




          Matrix and Mat can be both used in CvInvoke.Subtract(IInputArray src1, IInputArray src2, IOutputArray dst) without compilation errors as both implement IInputArray interface. I tested it with EmguCV 3.4.1. So i do not understand why you have to convert Matrix to Mat.
          – Quergo
          Nov 23 at 9:41












          When I used your code. It throws this exception ---> Exception thrown: 'Emgu.CV.Util.CvException' in Emgu.CV.World.dll
          – cassiopeiaofthemadworld
          Nov 23 at 9:49






          When I used your code. It throws this exception ---> Exception thrown: 'Emgu.CV.Util.CvException' in Emgu.CV.World.dll
          – cassiopeiaofthemadworld
          Nov 23 at 9:49














          Does the exception provides more detailed information?
          – Quergo
          Nov 23 at 10:27




          Does the exception provides more detailed information?
          – Quergo
          Nov 23 at 10:27


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282795%2fhow-to-convert-matrix-to-a-type-mat%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Sphinx de Gizeh

          Dijon

          Langue