首页 > 代码库 > InRange 测试像素的值是否在给定范围内

InRange 测试像素的值是否在给定范围内

程序:

wKioL1PsYc2R7UvxAAFS0dlFBbI130.jpg

核心函数:

cvInRange,cvInRangeS

代码:

#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include <iostream>
int InRangeS(int argc,char** argv)
{
IplImage *src=http://www.mamicode.com/cvLoadImage("e:\\picture\\4.jpg");
IplImage *dst=cvCreateImage(cvGetSize(src),8,1);
IplImage *dst2=cvCreateImage(cvGetSize(src),8,1);
cvZero(dst);
IplImage *lower=cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
IplImage *upper=cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
for(int rows=0;rows<lower->height;rows++)  //给图像的每个点赋值,也可以用cvSet
{
uchar* pt=(uchar*)(lower->imageData+rows*lower->widthStep);
for(int cols=0;cols<lower->width;cols++)
{
uchar* pt2=(uchar*)(pt+cols*lower->nChannels);
*pt2=100;
*(pt2+1)=100;
*(pt2+2)=100;
}
}
for(int rows=0;rows<upper->height;rows++)
{
uchar* pt=(uchar*)(upper->imageData+rows*upper->widthStep);
for(int cols=0;cols<upper->width;cols++)
{
uchar* pt2=(uchar*)(pt+cols*upper->nChannels);
*pt2=200;
*(pt2+1)=200;
*(pt2+2)=200;
}
}
//cvSet(lower,cvScalar(100,100,100,NULL));
//cvSet(upper,cvScalar(200,200,200,NULL));
cvInRangeS(src,cvScalar(100,100,100),cvScalar(200,200,200),dst);  //测试原图像的像素值是否在给定范围内方法1
cvInRange(src,lower,upper,dst2);                                 ////测试原图像的像素值是否在给定范围内方法2
cvNamedWindow("src");
cvNamedWindow("dst");
cvNamedWindow("lower");
cvNamedWindow("upper");
cvNamedWindow("dst2");
cvShowImage("src",src);
cvShowImage("dst",dst);
cvShowImage("lower",lower);
cvShowImage("upper",upper);
cvShowImage("dst2",dst2);
cvWaitKey(0);
cvDestroyWindow("src");
cvDestroyWindow("dst");
cvDestroyWindow("lower");
cvDestroyWindow("upper");
cvDestroyWindow("dst2");
cvReleaseImage(&src);
cvReleaseImage(&dst);
cvReleaseImage(&lower);
cvReleaseImage(&upper);
cvReleaseImage(&dst2);
return 0;
}


本文出自 “flyclc” 博客,请务必保留此出处http://flyclc.blog.51cto.com/1385758/1539951