0.功能
对二值图像(灰度图)进行的5种阈值操作,每种效果如下:
/* 0: Binary 大于阈值为max_binary_value,小于为0
1: Binary Inverted 与"Binary"相反
2: Threshold Truncated 大于阈值被截断,其余保留
3: Threshold to Zero 小于阈值设为0
4: Threshold to Zero Inverted 大于阈值设为0
*/
官方参考:https://docs.opencv.org/master/db/d8e/tutorial_threshold.html
1.工作空间准备
为了使这个单一的cpp文件能够执行,进行如下操作.
mkdir -p opencv_hv/src
cd opencv_hv/src
catkin_create_pkg image_proc std_msgs roscpp rospy cv_bridge
cd ../..
gedit hv.launch
在hv.launch文件中粘贴下述代码段,保存并关闭
<launch>
<node pkg="image_processing" type="basic_thresholding_operations_node" name="basic_thresholding_operations" output="screen" />
</launch>
工作空间结构如下:
# meng @ meng in ~/opencv_hv [22:28:30]
$ tree
.
├── hv.launch
└── src
└── image_proc
├── CMakeLists.txt
├── include
│ └── image_proc
├── package.xml
└── src
5 directories, 3 files
2.程序准备与解读
在/home/meng/opencv_hv/src/image_proc/src 文件夹下添加如下文件:
basic_thresholding_operations_node.cpp
#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 char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";
const char* trackbar_value = "Value";
static void Threshold_Demo( int, void* )
{
/* 0: Binary 大于阈值为max_binary_value,小于为0
1: Binary Inverted 与"Binary"相反
2: Threshold Truncated 大于阈值被截断,其余保留
3: Threshold to Zero 小于阈值设为0
4: Threshold to Zero Inverted 大于阈值设为0
*/
threshold( src_gray, dst, threshold_value, max_binary_value, threshold_type );
imshow( window_name, dst );
}
int main( int argc, char** argv )
{
String imageName("stuff.jpg"); // by default
if (argc > 1)
{
imageName = argv[1];
}
//src = imread( samples::findFile( imageName ), IMREAD_COLOR ); // Load an image
src = imread("/home/meng/my_learning/opencv_test/data/stuff.jpg", 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
namedWindow("src_gray",WINDOW_AUTOSIZE );
imshow("src_gray",src_gray);
namedWindow( window_name, WINDOW_AUTOSIZE ); // Create a window to display results
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
Threshold_Demo( 0, 0 ); // Call the function to initialize
waitKey();
return 0;
}
同时修改/home/meng/opencv_hv/src/image_proc/CMakeLists.txt如下:
cmake_minimum_required(VERSION 3.0.2)
project(image_processing)
find_package(catkin REQUIRED COMPONENTS
cv_bridge
roscpp
rospy
std_msgs
)
find_package(OpenCV REQUIRED)
catkin_package()
include_directories(
# include
${catkin_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS}
)
add_executable(basic_thresholding_operations_node src/basic_thresholding_operations_node.cpp)
target_link_libraries(basic_thresholding_operations_node
${catkin_LIBRARIES} ${OpenCV_LIBS}
)
3.程序运行
回到:/home/meng/opencv_hv,
catkin_make
source devel/setup.bash
#或source devel/setup.zsh
roslaunch hv.launch
调节阈值类型和阈值,效果如图下:
原图
truncate
binary
详细操作见视频;https://www.bilibili.com/video/BV1vQ4y127Mr/