Opencv cvThreshould 函数

/* Types of thresholding */
#define CV_THRESH_BINARY      0  /* value = value > threshold ? max_value : 0       */
#define CV_THRESH_BINARY_INV  1  /* value = value > threshold ? 0 : max_value       */
#define CV_THRESH_TRUNC       2  /* value = value > threshold ? threshold : value   */
#define CV_THRESH_TOZERO      3  /* value = value > threshold ? value : 0           */
#define CV_THRESH_TOZERO_INV  4  /* value = value > threshold ? 0 : value           */
#define CV_THRESH_MASK        7


#define CV_THRESH_OTSU        8  /* use Otsu algorithm to choose the optimal threshold value;
                                    combine the flag with one of the above CV_THRESH_* values */


/* Applies fixed-level threshold to grayscale image.
   This is a basic operation applied before retrieving contours */
CVAPI(double)  cvThreshold( const CvArr*  src, CvArr*  dst,
                            double  threshold, double  max_value,
                            int threshold_type );


#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include <iostream>
#include <stdio.h>
using namespace std;


void sum_rgb( IplImage* src, IplImage* dst );

int main(int argc, char** argv)
{
	cvNamedWindow( "example", 1 ); //create a window

	IplImage* src = cvLoadImage("C:\\Users\\Administrator\\Desktop\\1002.png" );
	IplImage* dst = cvCreateImage( cvGetSize(src), src->depth, 1 );  //
	sum_rgb( src, dst );

	cvShowImage( "example", dst );

	while (1)
	{
		if( (cvWaitKey( 10 )) == 27 )
			break;
	}

	cvDestroyWindow( argv[1] );
	cvReleaseImage( &src );
	cvReleaseImage( &src );

	return 0;
}

void sum_rgb( IplImage* src, IplImage* dst )
{
	//allocate individual image planes
	IplImage* r = cvCreateImage( cvGetSize( src ), IPL_DEPTH_8U, 1 );
	IplImage* g = cvCreateImage( cvGetSize( src ), IPL_DEPTH_8U, 1 );
	IplImage* b = cvCreateImage( cvGetSize( src ), IPL_DEPTH_8U, 1 );

	//temporary storage
	IplImage* s = cvCreateImage( cvGetSize( src ), IPL_DEPTH_8U, 1 );

	//split the image onto the color planes, signal channel
	cvSplit( src, r, g, b, NULL );//将三通道数据分别加载到各自的内存片中

	//add eaually weighted rgb values
	
/*
void cvAddWeighted( const CvArr* src1, double alpha,const CvArr* src2, double beta,double gamma, CvArr* dst );
src1 
第一个原数组. 
alpha 
第一个数组元素的权值 
src2 
第二个原数组 
beta 
第二个数组元素的权值 
dst 
输出数组 
gamma 
添加的常数项。 
函数 cvAddWeighted 计算两数组的加权值的和: 

dst(I)=src1(I)*alpha+src2(I)*beta+gamma
所有的数组必须的相同的类型相同的大小(或ROI大小) 
*/
	cvAddWeighted( r, 1./3, g, 1./3, 0.0, s );  //add r & g
	cvAddWeighted( s, 2./3, b, 1./3, 0.0, s );

	//truncate values above 100
	cvThreshold( s, dst, 150, 100, CV_THRESH_TRUNC );//单通道的图像阈值操作

	cvReleaseImage( &r );
	cvReleaseImage( &g );
	cvReleaseImage( &b );
	cvReleaseImage( &s );
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值