#include "stdafx.h"
#include
#include
#include
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, 1.0, b, 1./3., 0.0, s );
// Truncate values above 100.
cvThreshold( s, dst, 100, 255, CV_THRESH_TRUNC );//对大于100的像素值进行截断,大于100则为255,不大于100的为原值
cvReleaseImage( &r );
cvReleaseImage( &g );
cvReleaseImage( &b );
cvReleaseImage( &s );
}
int main(int argc, char** argv)
{
const char *pchar="E://图片素材//Desert.jpg";
// Create a named window with a the name of the file.
cvNamedWindow( pchar, 1 );
// Load the image from the given file name.
IplImage* src = cvLoadImage(pchar );
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 );
// Split image onto the color planes.
cvSplit( src, r, g, b, NULL );
cvShowImage("r",r);
cvShowImage("g",g);
cvShowImage("b",b);
IplImage* dst = cvCreateImage( cvGetSize(src), src->depth, 1);
sum_rgb( src, dst);
cvThreshold( dst, dst, 200, 255, CV_THRESH_BINARY );
// Show the image in the named window
cvShowImage( pchar, 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( pchar);
cvReleaseImage( &src );
cvReleaseImage( &dst );
cvReleaseImage(&r);
cvReleaseImage(&g);
cvReleaseImage(&b);
}