opencv Example 5-3

相关接口:
cvThreshold()
对灰度图像应用固定的阈值,这是在获取轮廓线之前应用的一个基本操作
函数原型:

函数说明:
第一个参数表示输入图像,必须为单通道灰度图
第二个参数表示输出的边缘图像,为单通道黑白图
第三个参数表示阈值
第四个参数表示最大值。
第五个参数表示运算方法。

/* Threshold types */

enum

{

    CV_THRESH_BINARY      =0,  /* value = value > threshold ? max_value : 0       */

    CV_THRESH_BINARY_INV  =1,  /* value = value > threshold ? 0 : max_value       */

    CV_THRESH_TRUNC       =2,  /* value = value > threshold ? threshold : value   */

    CV_THRESH_TOZERO      =3,  /* value = value > threshold ? value : 0           */

    CV_THRESH_TOZERO_INV  =4,  /* value = value > threshold ? 0 : value           */

    CV_THRESH_MASK        =7,

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

};
cvConvertScale接口

参数

src源阵列
dst目标阵列
scale尺度因子
shift添加到缩放源数组元素的值

使用可选的线性变换将一组阵列转换成另一组。这个函数有几个不同的用途,因此有几个不同的名称。它将一个数组复制到另一个数组,并进行可选缩放,缩放首先执行,和/或可选类型转换,然后执行:

dst(I)=scalesrc(I)+(shift0,shift1,...)

多通道阵列的所有通道都是独立处理的。
转换的类型是四舍五入和饱和的,即如果缩放+转换的结果不能用目标数组元素类型的值精确表示,则将其设置为实轴上最近的可表示值。

cvSAcc接口,实现矩阵的累加

 示例:

#include <stdio.h>
#include <cv.h>
#include <highgui.h>


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_32F, 1);
	  
	// Split image onto the color planes.
	//将多通道数组分割为一组单通道数组或提取特定的[颜色]平面
	cvSplit( src, r, g, b, NULL );
	cvNamedWindow( "red", 1 );
	cvShowImage( "red", r );
	cvNamedWindow( "g", 1 );
	cvShowImage( "g", g );
	cvNamedWindow( "b", 1 );
	cvShowImage( "b", b );
	//Accumulate separate planes, combine and threshold
	cvZero(s);
	cvAcc(b,s);
	cvAcc(g,s);
	cvAcc(r,s);

	//Truncate values above 100 and rescale into dst
	cvThreshold( s, s, 100, 100, CV_THRESH_TRUNC );
	cvConvertScale( s, dst, 1, 0 );

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

int main(int argc, char** argv)
{

  // Create a named window with a the name of the file.
  cvNamedWindow( argv[1], 1 );

  // Load the image from the given file name.
  IplImage* src = cvLoadImage( argv[1] );
  IplImage* dst = cvCreateImage( cvGetSize(src), src->depth, 1);
  sum_rgb( src, dst);

  //show the src picture
  cvNamedWindow("src",1);
  cvShowImage("src",src);
  
  // Show the image in the named window
  cvShowImage( argv[1], dst );
	
  // Idle until the user hits the "Esc" key.
  while( 1 ) { if( (cvWaitKey( 10 )&0x7f) == 27 ) break; }

  // Clean up and don鈥檛 be piggies
  cvDestroyWindow( argv[1] );
  cvDestroyWindow( "src" );
  cvReleaseImage( &src );
  cvReleaseImage( &dst );

}

 

 

 

 

 

猜你喜欢

转载自blog.csdn.net/cindywry/article/details/85699134
5-3
今日推荐