您的位置:首页 > 运维架构

利用OpenCV实现图像的阈值分割

2012-03-26 09:18 465 查看
对lena、baboon图像进行阈值分割,像素大于100的被赋值为255,小于100被赋为0.

#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_8U, 1 );

// Split image onto the color planes.

cvSplit( src, r, g, b, NULL );

// Add equally weighted rgb values.

cvAddWeighted( r, 1./3., g, 1./3., 0.0, s );

cvAddWeighted( s, 2./3., b, 1./3., 0.0, s );

// Truncate values above 100.

cvThreshold( s, dst, 100, 255, CV_THRESH_BINARY );

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 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’t be piggies

cvDestroyWindow( argv[1] );

cvReleaseImage( &src );

cvReleaseImage( &dst );

return 0;

}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: