利用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;
}
#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;
}
相关文章推荐
- 利用最大熵进行阈值分割从而实现灰度图像的二值化的原理概要及OpenCV代码
- OpenCV利用矩阵实现图像旋转
- 利用数组操作实现灰度图像放大两倍(C++&opencv)
- 利用OpenCV实现图像的叠加
- opencv开发笔记(八):利用感兴趣区域ROI实现图像叠加
- Android Jni 利用OpenCV 实现图像任意角度旋转
- 利用opencv实现图像滑动窗口操作
- Opencv下利用SIFT、SURF、ORB三种特征点实现图像匹配
- 利用OpenCV实现局部动态阈值分割
- opencv系列之一 利用透视变换实现图像的俯视图(正视图)
- 利用OpenCV实现旋转文本图像矫正的原理及OpenCV代码
- 利用python opencv实现图像自适应二值化
- Opencv下利用SIFT、SURF、ORB三种特征点实现图像匹配
- 利用OpenCV和C++实现由RGB图像转化为灰度图,再将灰度图转化为二值图的程序
- 利用Qt与OpenCV简单实现摄像头图像捕捉
- [转]利用OpenCV实现图像的叠加
- Android Jni 利用OpenCV 实现图像尺寸缩放(一)
- 图像处理中,SIFT,FAST,MSER,STAR等特征提取算法的比较与分析(利用openCV实现)
- 利用OpenCV实现图像边缘直方图输出
- 利用SVM支持向量机对彩色图像进行分割并使用OpenCV进行实现