Threshold operation


double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
Applies a fixed-level threshold to each array element
Parameters:
  • src – input array (single-channel, 8-bit or 32-bit floating point).
  • dst – output array of the same size and type as src.
  • threshthreshold value.
  • maxval – maximum value to use with the THRESH_BINARY and THRESH_BINARY_INV thresholding types.
  • typethresholding type
  • THRESH_BINARY
    \texttt{dst} (x,y) =  \fork{\texttt{maxval}}{if $\texttt{src}(x,y) > \texttt{thresh}$}{0}{otherwise}
  • THRESH_BINARY_INV
    \texttt{dst} (x,y) =  \fork{0}{if $\texttt{src}(x,y) > \texttt{thresh}$}{\texttt{maxval}}{otherwise}
  • THRESH_TRUNC
    \texttt{dst} (x,y) =  \fork{\texttt{threshold}}{if $\texttt{src}(x,y) > \texttt{thresh}$}{\texttt{src}(x,y)}{otherwise}
  • THRESH_TOZERO
    \texttt{dst} (x,y) =  \fork{\texttt{src}(x,y)}{if $\texttt{src}(x,y) > \texttt{thresh}$}{0}{otherwise}
  • THRESH_TOZERO_INV
    \texttt{dst} (x,y) =  \fork{0}{if $\texttt{src}(x,y) > \texttt{thresh}$}{\texttt{src}(x,y)}{otherwise}
Find an example in OpenCV documentaion.

Steps:

  1. Load an image
  2. Create a window to display results
  3. Create Trackbar to choose type of Threshold
  4. Call the function "Threshold_Demo" to perform threshold operation.

Functions:

    Example:

    ------------
    #include "opencv2/imgproc/imgproc.hpp"
    #include "opencv2/highgui/highgui.hpp"
    #include <stdlib.h>
    #include <stdio.h>
    
    using namespace cv;
    
    int threshold_value = 0;
    int threshold_type = 3;;
    int const max_value = 255;
    int const max_type = 4;
    int const max_BINARY_value = 255;
    
    Mat src, src_gray, dst;
    char* window_name = "Threshold Demo";
    
    char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";
    char* trackbar_value = "Value";
    
    void Threshold_Demo( int, void* );
    
    int main( int argc, char** argv )
    {
      /// Load an image
      src = imread( "shape.jpg", 1 );
    
      /// Convert the image to Gray
      cvtColor( src, src_gray, CV_RGB2GRAY );
    
      /// Create a window to display results
      namedWindow( window_name, CV_WINDOW_AUTOSIZE );
    
      /// Create Trackbar to choose type of Threshold
      createTrackbar( trackbar_type,
                      window_name, &threshold_type,
                      max_type, Threshold_Demo );
    
      createTrackbar( trackbar_value,
                      window_name, &threshold_value,
                      max_value, Threshold_Demo );
    
      /// Call the function to initialize
      Threshold_Demo( 0, 0 );
    
      /// Wait until user finishes program
      while(true)
      {
        int c;
        c = waitKey( 20 );
        if( (char)c == 27 )
          { break; }
       }
    }
    
    
    void Threshold_Demo( int, void* )
    {
      /* 0: Binary
         1: Binary Inverted
         2: Threshold Truncated
         3: Threshold to Zero
         4: Threshold to Zero Inverted
       */
    
      threshold( src_gray, dst, threshold_value, max_BINARY_value,threshold_type );
    
      imshow( window_name, dst );
    } 
    ------------

    Result:


    Sources:
    http://docs.opencv.org/doc/tutorials/imgproc/threshold/threshold.html

    19 comments:

    1. Great post, I never knew that, this is by far one of the most comprehensive posts i've seen here and look forward to more of the you have always nice things to post how to do zero gravity google

      ReplyDelete
    2. Great Work!!!
      Its the most comprehensive and simplified post I have ever seen about opencv

      ReplyDelete
    3. OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /build/buildd/opencv-2.4.8+dfsg1/modules/imgproc/src/color.cpp, line 3737
      terminate called after throwing an instance of 'cv::Exception'
      what(): /build/buildd/opencv-2.4.8+dfsg1/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function cvtColor

      Aborted (core dumped)
      I am getting this error message while running this code.kindly help

      ReplyDelete
      Replies
      1. You need a "shape.jpg" file in your folder(any image should be fine as long as it is a jpeg file).
        This is where the problem happens -> src = imread("shape.jpg", 1);

        Delete
      2. i've got the same error, then i just realize that i dont have shape.jpg
        Now it works

        Delete

    4. Go to Roku page record enter Roku com association code appeared on Roku TV. roku.com/link My Roku com associate not working use new Roku code.Roku is a streaming device, which is a reasonable roku setup and other Set-up Box. roku.com/link Roku is a bundle of amusement, where client can stream for boundless motion appears, web setup, news, roku.com/link animation and a lot more projects.

      Giving users an unparalleled streaming experience, Team roku.com/link takes great pride in being the number one streaming service roku.com/link providers in the world.Sanction Roku associate, go to roku.com/link record enter Roku interface code appeared on Roku TV. My roku com interface not working use new Roku code. Roku.com/link To download click for more subtleties.

      ReplyDelete
    5. ==========================

      This blog is very informative the stuff you provide I really enjoyed reading 국산야동
      Also visit my web site 국산야동

      ReplyDelete
    6. Are you facing trouble with youtube tv activation on your Roku streaming device? Looking for helpdesk services for Roku? Don't get worried anymore. Now activate youtube on roku using tv.youtube.com/start code . Talk to our experts through live chat process. Get in touch with us for more information.

      ReplyDelete
    7. Wow! Thank you! I continuously needed to write on my site something like that. Can I include a part of your post to my site?
      토토

      ReplyDelete
    8. If you're looking for the official Arlo Com Login links, you've come to the right place... arlo login | Arlo Login My Account | Arlo Netgear | Arlo Camera Login

      ReplyDelete
    9. Why is my Arlo Firmware not updating? — How long does it take for Arlo firmware update? Get all answer at how do i update arlo camera firmware

      ReplyDelete
    10. Canon Printer Setup Support to Keep Your Printer Running Smoothly Both commercials, as well as home printers, are the most temperamental and complex pieces of electronic devices. Almost all businesses depend on printers to complete their daily operations. Training materials, invoices, brochures, and other crucial documents, to print all these things, you will need a help.

      ReplyDelete
    11. Arlo is best recognized for both wired and wireless security cameras, and like any other electrical item, you'll need to upgrade the firmware on your Arlo. The Arlo firmware upgrade appears to be causing problems for the majority of consumers. But that is no longer the case, as you can now contact the Arlo customer support team at any moment to rectify the issue. To get the most out of your Arlo products, make sure the firmware is up to current.

      ReplyDelete
    12. Regardless of the type of doorbell or camera issue you're having, you may call the Netgear Arlo customer support number at any moment to get helpful guidance from the IT professionals. In fact, rather than attempting to solve the difficulties using our many DIY solutions, we recommend that you get professional help.

      ReplyDelete