cvcvtcolor源码_cvccvv

hacker|
92

文章目录:

opencv 中cvtColor报错处理

先调用cvCvtColor将图像转到HSV颜色空间,如:cvCvtColor(rgb,hsv,CV_BGR2HSV); 然后调用cvSplit函数,就可以将H分量分离出来,再来单独访问H分量,H对于的通道是0。

android中如何用opencv处理倾斜校正的问题,求源代码

#include "cv.h"

#include "highgui.h"

#include "cxcore.h"

#include "cvcam.h"

//图像的像素直接提取

#define _I(img,x,y) ((unsigned char*)((img)-imageData + (img)-widthStep*(y)))[(x)]

//亚像素级灰度值

#define _IF(image,x,y) ( ((int)(x+1)-(x))*((int)(y+1)-(y))*_I((image),(int)(x),(int)(y)) + ((int)(x+1)-(x))*((y)-(int)(y))*_I((image),(int)(x),(int)(y+1)) + ((x)-(int)(x))*((int)(y+1)-(y))*_I((image),(int)(x+1),(int)(y)) + ((x)-(int)(x))*((y)-(int)(y))*_I((image),(int)(x+1),(int)(y+1)) )//插值后的像素值(IN表示interpolation),x、y可以为小数

void callback(IplImage* image);

void main()

{

int ncams = cvcamGetCamerasCount( );//返回可以访问的摄像头数目

HWND mywin;

cvcamSetProperty(0, CVCAM_PROP_ENABLE, CVCAMTRUE);

cvcamSetProperty(0, CVCAM_PROP_RENDER, CVCAMTRUE);

mywin = (HWND)cvGetWindowHandle("cvcam window");

cvcamSetProperty(0, CVCAM_PROP_WINDOW, mywin);

cvcamSetProperty(0, CVCAM_PROP_CALLBACK, callback);

//cvcamGetProperty(0, CVCAM_VIDEOFORMAT,NULL);

cvNamedWindow( "径向矫正1", 1 );//创建窗口

cvNamedWindow( "径向矫正2", 1 );//创建窗口

cvcamInit( );

cvcamStart( );

cvWaitKey(0);

cvcamStop( );

cvcamExit( );

cvDestroyWindow( "径向矫正1" );//销毁窗口

cvDestroyWindow( "径向矫正2" );//销毁窗口

}

void callback(IplImage* image)

{

IplImage* Show1 = cvCreateImage( cvSize(320,240), IPL_DEPTH_8U, 1);

IplImage* Show2 = cvCreateImage( cvSize(420,340), IPL_DEPTH_8U, 1);

IplImage* ImageC1 = cvCreateImage( cvSize(320,240), IPL_DEPTH_8U, 1);

//转换为灰度图

cvCvtColor( image, ImageC1, CV_RGB2GRAY);

cvFlip( ImageC1, NULL, 0);

double *mi;

double *md;

mi = new double[3*3];

md = new double[4];

CvMat intrinsic_matrix,distortion_coeffs;

//摄像机内参数

cvInitMatHeader(intrinsic_matrix,3,3,CV_64FC1,mi);

//镜头畸变参数

cvInitMatHeader(distortion_coeffs,1,4,CV_64FC1,md);

/////////////////////////////////////////////////

////////////////////////////320*240 120度广角镜头

//参数由matlab获得

double fc1,fc2,cc1,cc2,kc1,kc2,kc3,kc4;

fc1 = 667.23923/2.5;

fc2 = 669.78156/2.5;

cc1 = 429.96933/2.5;

cc2 = 351.48350/2.5;

kc1 = -0.40100;

kc2 = 0.19463;

kc3 = 0.00508;

kc4 = -0.00051;

cvmSet(intrinsic_matrix, 0, 0, fc1);

cvmSet(intrinsic_matrix, 0, 1, 0);

cvmSet(intrinsic_matrix, 0, 2, cc1);

cvmSet(intrinsic_matrix, 1, 0, 0);

cvmSet(intrinsic_matrix, 1, 1, fc2);

cvmSet(intrinsic_matrix, 1, 2, cc2);

cvmSet(intrinsic_matrix, 2, 0, 0);

cvmSet(intrinsic_matrix, 2, 1, 0);

cvmSet(intrinsic_matrix, 2, 2, 1);

cvmSet(distortion_coeffs, 0, 0, kc1);

cvmSet(distortion_coeffs, 0, 1, kc2);

cvmSet(distortion_coeffs, 0, 2, kc3);

cvmSet(distortion_coeffs, 0, 3, kc4);

////////////////////////////320*240 120度广角镜头

/////////////////////////////////////////////////

//矫正畸变(opencv)

cvUndistort2( ImageC1, Show1, intrinsic_matrix, distortion_coeffs);

//矫正畸变

for (int nx=0; nx420; nx++)

{

for (int ny=0; ny340; ny++)

{

double x=nx-50;

double y=ny-50;

double xx=(x-cc1)/fc1;

double yy=(y-cc2)/fc2;

double r2=pow(xx,2)+pow(yy,2);

double r4=pow(r2,2);

double xxx=xx*(1+kc1*r2+kc2*r4)+2*kc3*xx*yy+kc4*(r2+2*xx*xx);

double yyy=yy*(1+kc1*r2+kc2*r4)+2*kc4*xx*yy+kc3*(r2+2*yy*yy);

double xxxx = xxx*fc1+cc1;

double yyyy = yyy*fc2+cc2;

if (xxxx0 xxxx320 yyyy0 yyyy240)

{

_I(Show2,nx,ny) = (int)_IF(ImageC1,xxxx,yyyy);

}

else

{

_I(Show2,nx,ny) = 0;

}

}

}

//画线

cvLine( Show1, cvPoint(0,10), cvPoint(320,10), cvScalar(255,255,255),3 );

cvLine( Show1, cvPoint(0,230), cvPoint(320,230), cvScalar(255,255,255),3 );

cvLine( Show1, cvPoint(10,0), cvPoint(10,240), cvScalar(255,255,255),3 );

cvLine( Show1, cvPoint(310,0), cvPoint(310,240), cvScalar(255,255,255),3 );

cvLine( Show1, cvPoint(0,0), cvPoint(320,240), cvScalar(255,255,255),3 );

cvLine( Show1, cvPoint(0,240), cvPoint(320,0), cvScalar(255,255,255),3 );

cvLine( Show1, cvPoint(0,10), cvPoint(320,10), cvScalar(0,0,0) );

cvLine( Show1, cvPoint(0,230), cvPoint(320,230), cvScalar(0,0,0) );

cvLine( Show1, cvPoint(10,0), cvPoint(10,240), cvScalar(0,0,0) );

cvLine( Show1, cvPoint(310,0), cvPoint(310,240), cvScalar(0,0,0) );

cvLine( Show1, cvPoint(0,0), cvPoint(320,240), cvScalar(0,0,0) );

cvLine( Show1, cvPoint(0,240), cvPoint(320,0), cvScalar(0,0,0) );

//显示

cvShowImage("径向矫正1", Show1);

cvShowImage("径向矫正2", Show2);

cvWaitKey(1);

cvReleaseImage( Show1 );

cvReleaseImage( Show2 );

cvReleaseImage( ImageC1 );

}

来自:

opencv中cvCvtColor函数在哪个库

下载opencv source,RGB2Gray部分源码在opencv-4.0.1\modules\imgproc\src\color_rgb.cpp文件中,如下:

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中,代码如下:

//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或者源代码,来了解具体的实现。

1条大神的评论

  • avatar
    访客 2022-07-09 上午 11:57:22

    Show1, cvPoint(10,0), cvPoint(10,240), cvScalar(0,0,0) ); cvLine( Show1, cvPoint(310,0), cvPoint(310,240), c

发表评论