如何在OpenCV中阈值
How to threshold an image in OpenCV?
我在openCV中具有二进制图像,代表蒙版。这个面膜有一定程度的几何噪声,我想平滑,所以为此,我使用模糊。
现在我有了灰度图像。我想将所有像素都超过一定阈值,使它们变白,所有其他像素必须变成黑色。
在OpenCV中有一种简单的方法?
是的,有:
ret,thresh1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
我从这个链接中得到。
在C 上,我们有
threshold( src_gray, dst, threshold_value, max_BINARY_value,threshold_type );
如果您想要更多信息,请在这里
上一个链接(C 教程)的完整代码
#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 );
}
相关文章:
- 传感器值的阈值
- OpenCV Otsu的阈值:计算多个Mat对象的单个阈值
- Java opencv inRange 阈值函数将我的图像分成三个不同的图像
- Android -OpenCV模板与阈值匹配
- 如何在OpenCV中阈值
- 将自适应阈值应用于范围函数 opencv c++
- 如何在 OpenCv 中使用 HSV 阈值和轮廓从图像中提取皮肤
- OpenCV / C++ - 如何在矢量上使用自适应阈值而不是垫子
- opencv阈值或不等于运算符
- 在OpenCV中使用HSV有效地阈值红色
- OpenCV 均值函数崩溃,掩码从 OpenCV 阈值函数创建
- OpenCV/C++ 中适当的阈值函数
- C++ / OpenCV 中的本地阈值实现
- OpenCV:如何使用阈值正确分割图像/使用二进制图像对原始图像进行分割
- 自适应阈值opencv
- 使用OpenCV的最大熵阈值
- OpenCV:设置图像阈值并反转图像
- (自适应)opencv中的阈值错误(cvarrToMat中的错误参数(未知数组类型))
- 如何阈值之间的多个范围在opencv
- 如何修改这个python opencv程序来从阈值图像中获取xy坐标