Basic Thresholding Operations
Примеры с cv2.imshow требуют сборки OpenCV с GUI. В ноутбуке с headless-версией используйте вывод изображений через Matplotlib.
Basic Thresholding Operations
Prev Tutorial: Image Pyramids
Next Tutorial: Thresholding Operations using inRange
| Original author | Ana Huamán |
| Compatibility | OpenCV >= 3.0 |
In this tutorial you will learn how to:
This thresholding operation can be expressed as:
This thresholding operation can be expressed as:
This thresholding operation can be expressed as:
This operation can be expressed as:
This operation can be expressed as:
The tutorial code's is shown lines below. You can also download it from here
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
using namespace cv;
using std::cout;
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;
const char* window_name = "Threshold Demo";
const * = ;
* = ;
( , * )
{
( , , , , );
( , );
}
( , ** )
{
();
( > )
{
= [];
}
= ( ::( ), );
(.())
{
<< << << ::;
-;
}
( , , );
( , );
( ,
, &,
, );
( ,
, &,
, );
( , );
();
;
}The tutorial code's is shown lines below. You can also download it from here
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Image;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import ...;
...;
...;
...;
...;
...;
{
= ;
= ;
= ;
= ;
=
+
+ ;
= ;
= ;
= ;
;
= ();
= ();
;
;
([] ) {
= ;
(. > ) {
= [];
}
= .();
(.()) {
..( + );
.();
}
.(, , .);
= ();
.(.);
= .();
(.(), );
.();
.();
}
( , ) {
(!(.() )) {
.( ());
;
}
= ();
.( (, .));
.( ());
= (, , );
.();
.();
.();
.();
.();
.( ());
= (, , );
.();
.();
.();
.();
.();
.( () {
@
( ) {
= () .();
= .();
();
}
});
.( () {
@
( ) {
= () .();
= .();
();
}
});
.(, .);
= ( ());
.(, .);
}
() {
.(, , , , );
= .();
.( ());
.();
}
([] ) {
.(.);
...( () {
@
() {
();
}
});
}
}The tutorial code's is shown lines below. You can also download it from here
from __future__ import print_function
import cv2 as cv
import argparse
max_value = 255
max_type = 4
max_binary_value = 255
trackbar_type = 'Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted'
trackbar_value = 'Value'
window_name = 'Threshold Demo'
def Threshold_Demo(val):
#0: Binary
#1: Binary Inverted
#2: Threshold Truncated
#3: Threshold to Zero
#4: Threshold
= .(, )
= .(, )
, = .(, , , )
.(, )
= .(=)
.(, =, =)
= .()
= .(..(.))
:
(, .)
()
#
= .(, .)
.()
.(, , , , )
#
.(, , , , )
#
()
#
.()Let's check the general structure of the program:
String imageName("stuff.jpg"); // by default
if (argc > 1)
{
imageName = argv[1];
}
src = imread( samples::findFile( imageName ), IMREAD_COLOR ); // Load an image
if (src.empty())
{
cout << "Cannot read the image: " << imageName << std::endl;
return -1;
}
cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to Gray String imagePath = "../data/stuff.jpg";
if (args.length > 0) {
imagePath = args[0];
}
// Load an image
src = Imgcodecs.imread(imagePath);
if (src.empty()) {
System.out.println("Empty image: " + imagePath);
System.exit(0);
}
// Convert the image to Gray
Imgproc.cvtColor(src, srcGray, Imgproc.COLOR_BGR2GRAY);src = cv.imread(cv.samples.findFile(args.input))
if src is None:
print('Could not open or find the image: ', args.input)
exit(0)
# Convert the image to Gray
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) namedWindow( window_name, WINDOW_AUTOSIZE ); // Create a window to display results // Create and set up the window.
frame = new JFrame(WINDOW_NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set up the content pane.
Image img = HighGui.toBufferedImage(srcGray);
addComponentsToPane(frame.getContentPane(), img);
// Use the content pane's default BorderLayout. No need for
// setLayout(new BorderLayout());
// Display the window.
frame.pack();
frame.setVisible(true);cv.namedWindow(window_name) createTrackbar( trackbar_type,
window_name, &threshold_type,
max_type, Threshold_Demo ); // Create a Trackbar to choose type of Threshold
createTrackbar( trackbar_value,
window_name, &threshold_value,
max_value, Threshold_Demo ); // Create a Trackbar to choose Threshold value sliderPanel.add(new JLabel(TRACKBAR_TYPE));
// Create Trackbar to choose type of Threshold
JSlider sliderThreshType = new JSlider(0, MAX_TYPE, thresholdType);
sliderThreshType.setMajorTickSpacing(1);
sliderThreshType.setMinorTickSpacing(1);
sliderThreshType.setPaintTicks(true);
sliderThreshType.setPaintLabels(true);
sliderPanel.add(sliderThreshType);
sliderPanel.add(new JLabel(TRACKBAR_VALUE));
// Create Trackbar to choose Threshold value
JSlider sliderThreshValue = new JSlider(0, MAX_VALUE, 0);
sliderThreshValue.();
.();
.();
.();
.();cv.createTrackbar(trackbar_type, window_name , 3, max_type, Threshold_Demo)
# Create Trackbar to choose Threshold value
cv.createTrackbar(trackbar_value, window_name , 0, max_value, Threshold_Demo)
static 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 );
} private void update() {
Imgproc.threshold(srcGray, dst, thresholdValue, MAX_BINARY_VALUE, thresholdType);
Image img = HighGui.toBufferedImage(dst);
imgLabel.setIcon(new ImageIcon(img));
frame.repaint();
}def Threshold_Demo(val):
#0: Binary
#1: Binary Inverted
#2: Threshold Truncated
#3: Threshold to Zero
#4: Threshold to Zero Inverted
threshold_type = cv.getTrackbarPos(trackbar_type, window_name)
threshold_value = cv.getTrackbarPos(trackbar_value, window_name)
_, dst = cv.threshold(src_gray, threshold_value, max_binary_value, threshold_type )
cv.imshow(window_name, dst)As you can see, the function cv::threshold is invoked. We give \(5\) parameters in C++ code: