关闭

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

标签: dstimagenullfileuserup
1348人阅读 评论(2) 收藏 举报
分类:
对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;

    }




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15131次
    • 积分:250
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:3篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论