opencv二值化(opencv二值化处理)
本篇文章给大家谈谈opencv二值化,以及opencv二值化处理对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、opencv中怎么获取二值化图像的每个像素点的值
- 2、图像二值化的OpenCV
- 3、OPENCV有关16位图像二值化的问题
- 4、openCV里怎么求二值化图像的面积和轮廓周长
- 5、C++ opencv 图片二值化最佳阈值确定(大津法,OTSU算法)
opencv中怎么获取二值化图像的每个像素点的值
for (int j=0; jsrc.rows; j++)
羡老没{
for (int i=0; isrc.cols; i++)
{
含灶 int m = src.atuchar(j,i);
}
兄纳 }
m 就是像素值
[img]图像二值化的OpenCV
OpenCV中有两个函数可以实现图片的二值化:
(1)cvThreshold( dst, dst,230 , 255, CV_THRESH_BINARY_INV);
(2)cvAdaptiveThreshold( dst, dst, 255, CV_ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY, 9, -10);
方法(1)是手动指定一个阈值,以此阈值御烂缓来进行二值化处理。其中的第四个参数决定了该方法的结果:
threshold_type=CV_THRESH_BINARY:
dst(x,y) = max_value, if src(x,y)threshold 0, otherwise.
threshold_type=CV_THRESH_BINARY_INV:
dst(x,y) = 0, if src(x,y)threshold; dst(x,y) = max_value, otherwise.
threshold_type=CV_THRESH_TRUNC:
dst(x,y) = threshold, if src(x,y)threshold; dst(x,y) = src(x,y), otherwise.
threshold_type=CV_THRESH_TOZERO:
dst(x,y) = src(x,y), if (x,y)threshold ; dst(x,y) = 0, otherwise.
threshold_type=CV_THRESH_TOZERO_INV:
dst(x,y) = 0, if src(x,y)threshold ; dst(x,y) = src(x,y), otherwise.
值得一说的是threshold_type可以使用CV_THRESH_OTSU类型,这样该函数镇模就会使用大律法OTSU得到的全局自适应阈值来进行二值化图片,而参数中的threshold不再起 作用。比如:cvThreshold( dst, dst,300 , 255, CV_THRESH_OTSU | CV_THRESH_BINARY_INV);这种方法对于灰度直方图呈现二峰特征的图片处理历棚起来效果很好。
方法(2)是一个自适应阈值二值化方法,通过设定最后两个参数来调整效果。
OPENCV有关16位图像二值化的问题
THRESH_BINARY类型可以自己写个清含函数二值化好了,效率一样很高
for( 扒穗int r = 0; r m_img16.rows; ++r )
for( int c 答此笑= 0; c m_img16.cols; ++c )
{
unsigned short v = m_img16.atunsigned short(r, c);
v = v = 1000 ? 65535 : 0;
}
openCV里怎么求二值化图像的面积和轮廓周长
可以先用findContours()函数找到你氏帆需要轮手的轮廓,然后用contourArea()函数求面积,用arcLength()函数求轮廓长度,也就是周长。腊核嫌
C++ opencv 图片二值化最佳阈值确定(大津法,OTSU算法)
//opencv
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/顷汪imgproc.hpp"
/******************************************************************************************
Function: OtsuThreshold
Description: 图片二值化最佳阈值确滚乎哗定(大津法,OTSU算法)
Input: src:原图片
Return: 阈值
******************************************************************************************/
int OtsuThreshold(IplImage* src)
{
int threshold;
try
{
int height = src-height;
int width = src-width;
//histogram 大行
float histogram[256] = { 0 };
for (int i = 0; i height; i++) {
unsigned char* p = (unsigned char*)src-imageData + src-widthStep*i;
for (int j = 0; j width; j++) {
histogram[*p++]++;
}
}
//normalize histogram
int size = height*width;
for (int i = 0; i 256; i++) {
histogram[i] = histogram[i] / size;
}
//average pixel value
float avgValue = 0;
for (int i = 0; i 256; i++) {
avgValue += i*histogram[i];
}
float maxVariance = 0;
float w = 0, u = 0;
for (int i = 0; i 256; i++) {
w += histogram[i];
u += i*histogram[i];
float t = avgValue*w - u;
float variance = t*t / (w*(1 - w));
if (variance maxVariance) {
maxVariance = variance;
threshold = i;
}
}
}
catch (cv::Exception e)
{
}
return threshold;
}
关于opencv二值化和opencv二值化处理的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。