cvcvtcolor源码(cvtcolor函数opencv3)
本文目录一览:
- 1、基于opencv的静态背景下的运动目标计数,急求源代码,谢谢!
- 2、我也求一份opencv下提取图片sift特征的项目源码,急用,谢谢您了
- 3、opencv中cvCvtColor函数在哪个库
- 4、请问OpenCV中的灰度变换函数cvCvtColor是运用哪种灰度变换?
- 5、opencv 中cvtColor报错处理
基于opencv的静态背景下的运动目标计数,急求源代码,谢谢!
这只是一个静态背景的运动物体检测程序:
至于计数,我建议你使用先提取contour 然后计算coutour的number。
// 2012-5-8 21:05:30
// Moving object recognision
// By David Ding
#include "stdafx.h"
#include opencv2/opencv.hpp
using namespace std;
using namespace cv;
#include iostream
#include string.h
#include vector
#include ctime
#include windows.h
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
//声明IplImage指针
IplImage* pFrame = NULL;
IplImage* pFrImg = NULL;
IplImage* pBkImg = NULL;
CvMat* pFrameMat = NULL;
CvMat* pFrMat = NULL;
CvMat* pBkMat = NULL;
CvCapture* pCapture = NULL;
int nFrmNum = 0;
//创建窗口
cvNamedWindow("video", 1);
cvNamedWindow("background",1);
cvNamedWindow("foreground",1);
//使窗口有序排列
cvMoveWindow("video", 30, 0);
cvMoveWindow("background", 360, 0);
cvMoveWindow("foreground", 690, 0);
if( argc 2 )
{
fprintf(stderr, "Usage: bkgrd [video_file_name]\n");
return -1;
}
//打开摄像头
if (argc ==1)
if( !(pCapture = cvCaptureFromCAM(-1)))
{
fprintf(stderr, "Can not open camera.\n");
return -2;
}
//打开视频文件
if(argc == 2)
if( !(pCapture = cvCaptureFromFile(argv[1])))
{
fprintf(stderr, "Can not open video file %s\n", argv[1]);
return -2;
}
//逐帧读取视频
while(pFrame = cvQueryFrame( pCapture ))
{
nFrmNum++;
//如果是第一帧,需要申请内存,并初始化
if(nFrmNum == 1)
{
pBkImg = cvCreateImage(cvSize(pFrame-width, pFrame-height), IPL_DEPTH_8U,1);
pFrImg = cvCreateImage(cvSize(pFrame-width, pFrame-height), IPL_DEPTH_8U,1);
pBkMat = cvCreateMat(pFrame-height, pFrame-width, CV_32FC1);
pFrMat = cvCreateMat(pFrame-height, pFrame-width, CV_32FC1);
pFrameMat = cvCreateMat(pFrame-height, pFrame-width, CV_32FC1);
//转化成单通道图像再处理
cvCvtColor(pFrame, pBkImg, CV_BGR2GRAY);
cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY);
cvConvert(pFrImg, pFrameMat);
cvConvert(pFrImg, pFrMat);
cvConvert(pFrImg, pBkMat);
}
else
{
cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY);
cvConvert(pFrImg, pFrameMat);
//高斯滤波先,以平滑图像
//cvSmooth(pFrameMat, pFrameMat, CV_GAUSSIAN, 3, 0, 0);
//当前帧跟背景图相减
cvAbsDiff(pFrameMat, pBkMat, pFrMat);
//二值化前景图
cvThreshold(pFrMat, pFrImg, 60, 255.0, CV_THRESH_BINARY);
//进行形态学滤波,去掉噪音
//cvErode(pFrImg, pFrImg, 0, 1);
//cvDilate(pFrImg, pFrImg, 0, 1);
//更新背景
cvRunningAvg(pFrameMat, pBkMat, 0.003, 0);
//将背景转化为图像格式,用以显示
cvConvert(pBkMat, pBkImg);
//显示图像
cvShowImage("video", pFrame);
cvShowImage("background", pBkImg);
cvShowImage("foreground", pFrImg);
//如果有按键事件,则跳出循环
//此等待也为cvShowImage函数提供时间完成显示
//等待时间可以根据CPU速度调整
if( cvWaitKey(2) = 0 )
break;
}
}
//销毁窗口
cvDestroyWindow("video");
cvDestroyWindow("background");
cvDestroyWindow("foreground");
//释放图像和矩阵
cvReleaseImage(pFrImg);
cvReleaseImage(pBkImg);
cvReleaseMat(pFrameMat);
cvReleaseMat(pFrMat);
cvReleaseMat(pBkMat);
cvReleaseCapture(pCapture);
return 0;
}
我也求一份opencv下提取图片sift特征的项目源码,急用,谢谢您了
#include "stdafx.h"
#include opencv2/opencv.hpp
double
compareSURFDescriptors( const float* d1, const float* d2, double best, int length )
{
double total_cost = 0;
assert( length % 4 == 0 );
for( int i = 0; i length; i += 4 )
{
double t0 = d1[i ] - d2[i ];
double t1 = d1[i+1] - d2[i+1];
double t2 = d1[i+2] - d2[i+2];
double t3 = d1[i+3] - d2[i+3];
total_cost += t0*t0 + t1*t1 + t2*t2 + t3*t3;
if( total_cost best )
break;
}
return total_cost;
}
int
naiveNearestNeighbor( const float* vec, int laplacian,
const CvSeq* model_keypoints,
const CvSeq* model_descriptors )
{
int length = (int)(model_descriptors-elem_size/sizeof(float));
int i, neighbor = -1;
double d, dist1 = 1e6, dist2 = 1e6;
CvSeqReader reader, kreader;
cvStartReadSeq( model_keypoints, kreader, 0 );
cvStartReadSeq( model_descriptors, reader, 0 );
for( i = 0; i model_descriptors-total; i++ )
{
const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr;
const float* mvec = (const float*)reader.ptr;
CV_NEXT_SEQ_ELEM( kreader.seq-elem_size, kreader );
CV_NEXT_SEQ_ELEM( reader.seq-elem_size, reader );
if( laplacian != kp-laplacian )
continue;
d = compareSURFDescriptors( vec, mvec, dist2, length );
if( d dist1 )
{
dist2 = dist1;
dist1 = d;
neighbor = i;
}
else if ( d dist2 )
dist2 = d;
}
if ( dist1 0.6*dist2 )
return neighbor;
return -1;
}
void
findPairs( const CvSeq* objectKeypoints, const CvSeq* objectDescriptors,
const CvSeq* imageKeypoints, const CvSeq* imageDescriptors, vectorint ptpairs )
{
int i;
CvSeqReader reader, kreader;
cvStartReadSeq( objectKeypoints, kreader );
cvStartReadSeq( objectDescriptors, reader );
ptpairs.clear();
for( i = 0; i objectDescriptors-total; i++ )
{
const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr;
const float* descriptor = (const float*)reader.ptr;
CV_NEXT_SEQ_ELEM( kreader.seq-elem_size, kreader );
CV_NEXT_SEQ_ELEM( reader.seq-elem_size, reader );
int nearest_neighbor = naiveNearestNeighbor( descriptor, kp-laplacian, imageKeypoints, imageDescriptors );
if( nearest_neighbor = 0 )
{
ptpairs.push_back(i);
ptpairs.push_back(nearest_neighbor);
}
}
}
void
flannFindPairs( const CvSeq*, const CvSeq* objectDescriptors,
const CvSeq*, const CvSeq* imageDescriptors, vectorint ptpairs )
{
int length = (int)(objectDescriptors-elem_size/sizeof(float));
cv::Mat m_object(objectDescriptors-total, length, CV_32F);
cv::Mat m_image(imageDescriptors-total, length, CV_32F);
// copy descriptors
CvSeqReader obj_reader;
float* obj_ptr = m_object.ptrfloat(0);
cvStartReadSeq( objectDescriptors, obj_reader );
for(int i = 0; i objectDescriptors-total; i++ )
{
const float* descriptor = (const float*)obj_reader.ptr;
CV_NEXT_SEQ_ELEM( obj_reader.seq-elem_size, obj_reader );
memcpy(obj_ptr, descriptor, length*sizeof(float));
obj_ptr += length;
}
CvSeqReader img_reader;
float* img_ptr = m_image.ptrfloat(0);
cvStartReadSeq( imageDescriptors, img_reader );
for(int i = 0; i imageDescriptors-total; i++ )
{
const float* descriptor = (const float*)img_reader.ptr;
CV_NEXT_SEQ_ELEM( img_reader.seq-elem_size, img_reader );
memcpy(img_ptr, descriptor, length*sizeof(float));
img_ptr += length;
}
// find nearest neighbors using FLANN
cv::Mat m_indices(objectDescriptors-total, 2, CV_32S);
cv::Mat m_dists(objectDescriptors-total, 2, CV_32F);
cv::flann::Index flann_index(m_image, cv::flann::KDTreeIndexParams(4)); // using 4 randomized kdtrees
flann_index.knnSearch(m_object, m_indices, m_dists, 2, cv::flann::SearchParams(64) ); // maximum number of leafs checked
int* indices_ptr = m_indices.ptrint(0);
float* dists_ptr = m_dists.ptrfloat(0);
for (int i=0;im_indices.rows;++i) {
if (dists_ptr[2*i]0.6*dists_ptr[2*i+1]) {
ptpairs.push_back(i);
ptpairs.push_back(indices_ptr[2*i]);
}
}
}
/* a rough implementation for object location */
int
locatePlanarObject( const CvSeq* objectKeypoints, const CvSeq* objectDescriptors,
const CvSeq* imageKeypoints, const CvSeq* imageDescriptors,
const CvPoint src_corners[4], CvPoint dst_corners[4] )
{
double h[9];
CvMat _h = cvMat(3, 3, CV_64F, h);
vectorint ptpairs;
vectorCvPoint2D32f pt1, pt2;
CvMat _pt1, _pt2;
int i, n;
#ifdef USE_FLANN
flannFindPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#else
findPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#endif
n = (int)(ptpairs.size()/2);
if( n 4 )
return 0;
pt1.resize(n);
pt2.resize(n);
for( i = 0; i n; i++ )
{
pt1[i] = ((CvSURFPoint*)cvGetSeqElem(objectKeypoints,ptpairs[i*2]))-pt;
pt2[i] = ((CvSURFPoint*)cvGetSeqElem(imageKeypoints,ptpairs[i*2+1]))-pt;
}
_pt1 = cvMat(1, n, CV_32FC2, pt1[0] );
_pt2 = cvMat(1, n, CV_32FC2, pt2[0] );
if( !cvFindHomography( _pt1, _pt2, _h, CV_RANSAC, 5 ))
return 0;
for( i = 0; i 4; i++ )
{
double x = src_corners[i].x, y = src_corners[i].y;
double Z = 1./(h[6]*x + h[7]*y + h[8]);
double X = (h[0]*x + h[1]*y + h[2])*Z;
double Y = (h[3]*x + h[4]*y + h[5])*Z;
dst_corners[i] = cvPoint(cvRound(X), cvRound(Y));
}
return 1;
}
int main(int argc, char** argv)
{
const char* object_filename = argc == 3 ? argv[1] : "box.png";
const char* scene_filename = argc == 3 ? argv[2] : "box_in_scene.png";
IplImage* object = cvLoadImage( object_filename, CV_LOAD_IMAGE_GRAYSCALE );
IplImage* image = cvLoadImage( scene_filename, CV_LOAD_IMAGE_GRAYSCALE );
if( !object || !image )
{
fprintf( stderr, "Can not load %s and/or %s\n",
object_filename, scene_filename );
exit(-1);
}
CvMemStorage* storage = cvCreateMemStorage(0);
cvNamedWindow("Object", 1);
cvNamedWindow("Object Correspond", 1);
static CvScalar colors[] =
{
{{0,0,255}},
{{0,128,255}},
{{0,255,255}},
{{0,255,0}},
{{255,128,0}},
{{255,255,0}},
{{255,0,0}},
{{255,0,255}},
{{255,255,255}}
};
IplImage* object_color = cvCreateImage(cvGetSize(object), 8, 3);
cvCvtColor( object, object_color, CV_GRAY2BGR );
CvSeq* objectKeypoints = 0, *objectDescriptors = 0;
CvSeq* imageKeypoints = 0, *imageDescriptors = 0;
int i;
CvSURFParams params = cvSURFParams(500, 1);
double tt = (double)cvGetTickCount();
cvExtractSURF( object, 0, objectKeypoints, objectDescriptors, storage, params );
printf("Object Descriptors: %d\n", objectDescriptors-total);
cvExtractSURF( image, 0, imageKeypoints, imageDescriptors, storage, params );
printf("Image Descriptors: %d\n", imageDescriptors-total);
tt = (double)cvGetTickCount() - tt;
printf( "Extraction time = %gms\n", tt/(cvGetTickFrequency()*1000.));
CvPoint src_corners[4] = {{0,0}, {object-width,0}, {object-width, object-height}, {0, object-height}};
CvPoint dst_corners[4];
IplImage* correspond = cvCreateImage( cvSize(image-width, object-height+image-height), 8, 1 );
cvSetImageROI( correspond, cvRect( 0, 0, object-width, object-height ) );
cvCopy( object, correspond );
cvSetImageROI( correspond, cvRect( 0, object-height, correspond-width, correspond-height ) );
cvCopy( image, correspond );
cvResetImageROI( correspond );
#ifdef USE_FLANN
printf("Using approximate nearest neighbor search\n");
#endif
if( locatePlanarObject( objectKeypoints, objectDescriptors, imageKeypoints,
imageDescriptors, src_corners, dst_corners ))
{
for( i = 0; i 4; i++ )
{
CvPoint r1 = dst_corners[i%4];
CvPoint r2 = dst_corners[(i+1)%4];
cvLine( correspond, cvPoint(r1.x, r1.y+object-height ),
cvPoint(r2.x, r2.y+object-height ), colors[8] );
}
}
vectorint ptpairs;
#ifdef USE_FLANN
flannFindPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#else
findPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#endif
for( i = 0; i (int)ptpairs.size(); i += 2 )
{
CvSURFPoint* r1 = (CvSURFPoint*)cvGetSeqElem( objectKeypoints, ptpairs[i] );
CvSURFPoint* r2 = (CvSURFPoint*)cvGetSeqElem( imageKeypoints, ptpairs[i+1] );
cvLine( correspond, cvPointFrom32f(r1-pt),
cvPoint(cvRound(r2-pt.x), cvRound(r2-pt.y+object-height)), colors[8] );
}
cvShowImage( "Object Correspond", correspond );
for( i = 0; i objectKeypoints-total; i++ )
{
CvSURFPoint* r = (CvSURFPoint*)cvGetSeqElem( objectKeypoints, i );
CvPoint center;
int radius;
center.x = cvRound(r-pt.x);
center.y = cvRound(r-pt.y);
radius = cvRound(r-size*1.2/9.*2);
cvCircle( object_color, center, radius, colors[0], 1, 8, 0 );
}
cvShowImage( "Object", object_color );
cvWaitKey(0);
cvDestroyWindow("Object");
cvDestroyWindow("Object Correspond");
return 0;
}
opencv中cvCvtColor函数在哪个库
下载opencv sourcecvcvtcolor源码,RGB2Gray部分源码在opencv-4.0.1\modules\imgproc\src\color_rgb.cpp文件中cvcvtcolor源码,如下cvcvtcolor源码:
templatetypename _Tp struct RGB2Gray
{
typedef _Tp channel_type;
RGB2Gray(int _srccn, int blueIdx, const float* _coeffs) : srccn(_srccn)
{
static const float coeffs0[] = { R2YF, G2YF, B2YF };
memcpy( coeffs, _coeffs ? _coeffs : coeffs0, 3*sizeof(coeffs[0]) );
if(blueIdx == 0)
std::swap(coeffs[0], coeffs[2]); }
void operator()(const _Tp* src, _Tp* dst, int n) const
{
int scn = srccn;
float cb = coeffs[0], cg = coeffs[1], cr = coeffs[2];
for(int i = 0; i n; i++, src += scn)
dst[i] = saturate_cast_Tp(src[0]*cb + src[1]*cg + src[2]*cr); }
int srccn;
float coeffs[3];
};
其中YF, G2YF, B2YF定义在文件color.hpp中cvcvtcolor源码,代码如下:
//constants for conversion from/to RGB and Gray, YUV, YCrCb according to BT.601
const float B2YF = 0.114f;
const float G2YF = 0.587f;
const float R2YF = 0.299f;
请问OpenCV中的灰度变换函数cvCvtColor是运用哪种灰度变换?
cvCvtColor(...),是Opencv里的颜色空间转换函数,可以实现RGB颜色向HSV,HSI等颜色空间的转换,也可以转换为灰度图像。
参数CV_RGB2GRAY是RGB到gray。
具体用的线性灰度变换函数是:
Gray=0.299*R+0.587*G+0.144*B
你可以通过查看OpenCV的documentation或者源代码,来了解具体的实现。
opencv 中cvtColor报错处理
先调用cvCvtColor将图像转到HSV颜色空间,如:cvCvtColor(rgb,hsv,CV_BGR2HSV); 然后调用cvSplit函数,就可以将H分量分离出来,再来单独访问H分量,H对于的通道是0。