Yes, there is:

ret,thresh1 = cv.threshold(img,127,255,cv.THRESH_BINARY)

I got from this link.

On C++, we have

threshold( src_gray, dst, threshold_value, max_BINARY_value,threshold_type );

Here if you want further information

Full code from the last link (C++ tutorial)

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>

using namespace cv;

/// Global variables

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";

/// Function headers
void Threshold_Demo( int, void* );

/**
 * @function main
 */
int main( int argc, char** argv )
{
  /// Load an image
  src = imread( argv[1], 1 );

  /// Convert the image to Gray
  cvtColor( src, src_gray, CV_BGR2GRAY );

  /// 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; }
   }

}


/**
 * @function Threshold_Demo
 */
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 );
}
Answer from Leonardo Alves Machado on Stack Overflow
๐ŸŒ
OpenCV
opencv.org โ€บ home โ€บ news โ€บ image thresholding using opencv
Image Thresholding using OpenCV
April 24, 2025 - C = 2: A constant subtracted from the mean or weighted mean to fine-tune the result. The threshold value is 0 and OpenCV computes the optimal value automatically for Otsuโ€™s technique.
๐ŸŒ
LearnOpenCV
learnopencv.com โ€บ home โ€บ image processing โ€บ image thresholding in opencv
Image Thresholding in OpenCV
March 1, 2024 - Letโ€™s first look at the code that will demonstrate several thresholding rules. We will discuss each line in detail so that you understand it fully. # import opencv import cv2 # Read image src = cv2.imread("threshold.png", cv2.IMREAD_GRAYSCALE); # Basic threhold example th, dst = cv2.threshold(src, 0, 255, cv2.THRESH_BINARY); cv2.imwrite("opencv-threshold-example.jpg", dst); # Thresholding with maxValue set to 128 th, dst = cv2.threshold(src, 0, 128, cv2.THRESH_BINARY); cv2.imwrite("opencv-thresh-binary-maxval.jpg", dst); # Thresholding with threshold value set 127 th, dst = cv2.threshold(src
Top answer
1 of 1
1

Yes, there is:

ret,thresh1 = cv.threshold(img,127,255,cv.THRESH_BINARY)

I got from this link.

On C++, we have

threshold( src_gray, dst, threshold_value, max_BINARY_value,threshold_type );

Here if you want further information

Full code from the last link (C++ tutorial)

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>

using namespace cv;

/// Global variables

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";

/// Function headers
void Threshold_Demo( int, void* );

/**
 * @function main
 */
int main( int argc, char** argv )
{
  /// Load an image
  src = imread( argv[1], 1 );

  /// Convert the image to Gray
  cvtColor( src, src_gray, CV_BGR2GRAY );

  /// 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; }
   }

}


/**
 * @function Threshold_Demo
 */
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 );
}
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ opencv โ€บ opencv_adaptive_threshold.htm
OpenCV - Adaptive Threshold
ADAPTIVE_THRESH_GAUSSIAN_C โˆ’ threshold value is the weighted sum of neighborhood values where weights are a Gaussian window.
๐ŸŒ
dangphongvanthanh
dangphongvanthanh.wordpress.com โ€บ 2017 โ€บ 07 โ€บ 07 โ€บ opencv-threshold-python-c
OpenCV Threshold ( Python , C++ ) โ€“ dangphongvanthanh
July 7, 2017 - If src ( x , y ) is greater than thresh, it sets the value of the destination image pixel dst ( x , y ) to maxValue, otherwise it sets it to zero. Here is what the pseudo code looks like ยท More generally, there are many types of thresholding based on different threshold rules applied to src ( x , y ) to get dst ( x , y ). Letโ€™s look at the various threshold types available in OpenCV.
๐ŸŒ
Reddit
reddit.com โ€บ r/opencv โ€บ [blog]: opencv threshold ( python , c++ )
r/opencv on Reddit: [Blog]: OpenCV Threshold ( Python , C++ )
September 28, 2020 -

Sometimes the easiest solution is the most suitable one. This is especially true in the Computer Vision domain. Thresholding is one of the most commonly used image processing techniques and despite its ease of use, it has proven to be highly effective even in advanced computer vision problems.

In this blog, we will explore different thresholding techniques and implement them using OpenCV in Python and C++.

https://www.learnopencv.com/opencv-threshold-python-cpp/

Find elsewhere
๐ŸŒ
YouTube
youtube.com โ€บ computer vision lab
OPENCV & C++ TUTORIALS - 23 | cv::threshold() - YouTube
This video includes threshold operation with cv::threshold() function in OpenCV.I will continue to this tutorial series with this plan : https://docs.opencv....
Published: March 29, 2022
Views: 1K
Top answer
1 of 1
1

I don't know an easier way to find which library you need for a given function than to use nm. So, first you need to find where the OpenCV libraries are installed, i.e. the bit after -L on your compilation command-line. As you have pkg-config, you can use:

pkg-config --libs-only-L opencv

Sample Output

-L/usr/local/Cellar/opencv3/3.1.0_4/lib

So, in the above case, you would do:

cd /usr/local/Cellar/opencv3/3.1.0_4/lib

Now you want to run nm to get all the exported symbols, with the filename, in all libraries ignoring all the ones that end in version numbers as they are mainly just symlinks anyway and they confuse the issue:

nm -g -APU *[A-Za-z].dylib | grep threshold

Sample Output

libopencv_imgproc.dylib: __ZN2cv9thresholdERKNS_11_InputArrayERKNS_12_OutputArrayEddi T 1861788 0

So that tells me you need imgproc library - which you already have. So what is going on???

The problem is that the way OpenCV was built must match the way you are linking with it so that the linker can resolve symbols.

So, if your OpenCV was built with clang++ -std=c++11 -stdlib=libstdc++ you need to build your application the same way.

Whereas if your OpenCV was built with -stdlib=libc++, you will need to use the same when linking your application.

There must be an easier way to tell how something was built (i.e. which library it links against) and maybe someone will tell me, but this works:

First, find your libopencv_imgproc.dylib:

find /usr /opt -name libopencv_imgproc.dylib 

then run nm -m on it to find which symbols are missing and where it wants to get them from:

nm -m libopencv_imgproc.dylib | grep "from libc"

                 (undefined) external __ZNKSt3__120__vector_base_commonILb1EE20__throw_length_errorEv (from libc++)
                 (undefined) external __ZNSt3__16__sortIRNS_6__lessIiiEEPiEEvT0_S5_T_ (from libc++)
                 (undefined) external __ZSt9terminatev (from libc++)
                 (undefined) external __ZTVN10__cxxabiv117__class_type_infoE (from libc++)
                 (undefined) external __ZTVN10__cxxabiv120__si_class_type_infoE (from libc++)
                 (undefined) external __ZTVN10__cxxabiv121__vmi_class_type_infoE (from libc++)
                 (undefined) weak external __ZdaPv (from libc++)
                 (undefined) weak external __ZdlPv (from libc++)
                 (undefined) weak external __Znam (from libc++)
                 (undefined) weak external __Znwm (from libc++)
                 (undefined) external ___cxa_begin_catch (from libc++)
                 (undefined) external ___cxa_end_catch (from libc++)
                 (undefined) external ___cxa_guard_acquire (from libc++)
                 (undefined) external ___cxa_guard_release (from libc++)
                 (undefined) external ___cxa_pure_virtual (from libc++)
                 (undefined) external ___cxa_rethrow (from libc++)
                 (undefined) external ___gxx_personality_v0 (from libc++)

So, I can see that my libraries are expecting to find unresolved symbols in libc++ so I need to use that library when compiling.

๐ŸŒ
Master Data Science
datahacker.rs โ€บ opencv-thresholding
OpenCV #007 Thresholding - Master Data Science
October 26, 2019 - We can use ADAPTIVE_THRESH_MEAN_C if we want the threshold value to be the mean of pixel intensities in a neighbourhood area. In addition, ADAPTIVE_THRESH_GAUSSIAN_C can be used if we want that the neighbourhood pixel values are multiplied with weights obtained from a Gaussian kernel. In addition to that, we must define the block size (neighbourhood area), and the constant that will be subtracted from the mean/weighted mean. #include <opencv2...
๐ŸŒ
Medium
medium.com โ€บ @manik.butex41 โ€บ an-introduction-to-thresholding-operations-in-opencv-64aff0cb2826
An Introduction to Thresholding Operations in OpenCV | by Manik Hossain | Medium
November 6, 2023 - Global thresholding: In global ... in the image as either foreground or background. The cv2.threshold() function in OpenCV can be used for global thresholding....
๐ŸŒ
PyImageSearch
pyimagesearch.com โ€บ home โ€บ blog โ€บ opencv thresholding ( cv2.threshold )
OpenCV Thresholding ( cv2.threshold ) - PyImageSearch
June 8, 2023 - We supply our original input image as the first two arguments, and then our inverted thresholded image as our mask. Remember, a mask only considers pixels in the original image where the mask is greater than zero. Ready to see the results of applying basic thresholding with OpenCV?
๐ŸŒ
GitHub
github.com โ€บ opencv โ€บ opencv โ€บ blob โ€บ master โ€บ modules โ€บ imgproc โ€บ src โ€บ thresh.cpp
opencv/modules/imgproc/src/thresh.cpp at master ยท opencv/opencv
January 23, 2019 - static bool ocl_threshold( InputArray _src, OutputArray _dst, InputArray _mask, double & thresh, double maxval, int thresh_type )
Author: opencv
๐ŸŒ
YouTube
youtube.com โ€บ learnopencv
Image Thresholding in OpenCV - YouTube
๐Ÿ“š Blog post Link: https://learnopencv.com/opencv-threshold-python-cpp/๐Ÿ“š Check out our FREE Courses at OpenCV University : https://opencv.org/university/fre
Published: February 26, 2024
Views: 1K
๐ŸŒ
YouTube
youtube.com โ€บ cloudxlab official
6 Thresholding with OpenCV - YouTube
In this video we discuss Thresholding with OpenCV. We will discuss multiple methods of thresholding including Simple, Adaptive and Otsu Thresholding.The repo...
Published: October 1, 2020
Views: 1K
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ opencv โ€บ opencv_simple_threshold.htm
OpenCV - Simple Threshold
You can perform simple threshold operation on an image using the method threshold() of the Imgproc class, Following is the syntax of this method. ... maxval โˆ’ A variable of double type representing the value that is to be given if pixel value ...
๐ŸŒ
copypasteearth
copypasteearth.com โ€บ home โ€บ blog โ€บ thresholding with opencv
Thresholding with OpenCV - copypasteearth
February 17, 2024 - We can use the cv2.threshold function to apply simple thresholding. The function takes four arguments: the source image, the threshold value, the maximum value, and the thresholding type.
๐ŸŒ
PyImageSearch
pyimagesearch.com โ€บ home โ€บ blog โ€บ adaptive thresholding with opencv ( cv2.adaptivethreshold )
Adaptive Thresholding with OpenCV ( cv2.adaptiveThreshold ) - PyImageSearch
June 9, 2023 - From there weโ€™ll configure our development environment and review our project directory structure. Iโ€™ll then show you how to implement adaptive thresholding using OpenCV and the cv2.adaptiveThreshold function.