使用opencv在网络摄像头上的灰度图像(不能显示完整图像)

时间:2014-05-30 04:25:54

标签: opencv image-processing webcam

我正在尝试使用OpenCV(cvCapture)通过网络摄像头创建灰度操作。这是代码

void h_grayscale( unsigned char* h_in, unsigned char* h_out)
{
for(int i=0;i<height;i++){
    for(int j=0;j<width;j++){
       int index = h_in[i*widthStep + j*channels];
       int gray = 0.3*(index)+0.6*(index+1)+0.1*(index+2);
       h_out[i*widthStepOutput+j]=gray;
   }
}

}

主要代码

int main(int argc, char** argv)
{
int c;  
IplImage *image_input; 
CvCapture* capture = cvCaptureFromCAM(1); 


while(1)
{
    image_input=cvQueryFrame(capture);
    IplImage* image_output = cvCreateImage(cvGetSize(image_input),IPL_DEPTH_8U,image_input->nChannels);

    unsigned char *h_out = (unsigned char*)image_output->imageData;
    unsigned char *h_in =  (unsigned char*)image_input->imageData;

    width     = image_input->width;
    height    = image_input->height;
    channels  = image_input->nChannels;
    widthStep = image_input->widthStep;
    widthStepOutput = image_input->widthStep;

   h_grayscale ( h_in , h_out ) ;
    //negatif_parallel(h_in, h_out, width, height, widthStep, channels);

    cvShowImage("Original", image_input);
    cvShowImage("CPU", image_output);

    c=cvWaitKey(10);
    if(c == 27)
        break;

   }
return 0;
}

但是当执行的图像看起来是整个图片的三分之一。我不知道哪个部分是错的。 结果如下: result

请在T_T..thx之前帮助我:))

1 个答案:

答案 0 :(得分:0)

您的输出图像是3通道图像,这就是为什么您只有1/3的图像。

尝试使用以下方法创建它:

IplImage* image_output = cvCreateImage(cvGetSize(image_input),IPL_DEPTH_8U,1); 

此代码有效(尝试查找差异):

int height=0;
int width=0;
int channels=0;
int widthStep=0;
int widthStepOutput=0;
void h_grayscale( unsigned char* h_in, unsigned char* h_out);
int main(int argc, char** argv)
{
    int c;  
    IplImage *image_input; 
    CvCapture* capture = cvCaptureFromCAM(0); 


    while(1)
    {
        image_input=cvQueryFrame(capture);
        IplImage* image_output = cvCreateImage(cvSize(image_input->width,image_input->height),IPL_DEPTH_8U,1);

        unsigned char *h_out = (unsigned char*)image_output->imageData;
        unsigned char *h_in =  (unsigned char*)image_input->imageData;

        width     = image_input->width;
        height    = image_input->height;
        channels  = image_input->nChannels;
        widthStep = image_input->widthStep;
        widthStepOutput = image_output->widthStep;

        h_grayscale ( h_in , h_out ) ;
        //negatif_parallel(h_in, h_out, width, height, widthStep, channels);

        cvShowImage("Original", image_input);
        cvShowImage("CPU", image_output);

        c=cvWaitKey(10);
        if(c == 27)
            break;

    }
    return 0;
}

void h_grayscale( unsigned char* h_in, unsigned char* h_out)
{
    for(int i=0;i<height;i++){
        for(int j=0;j<width;j++){
            int index = h_in[i*widthStep + j*channels];
            int gray = 0.3*(index)+0.6*(index+1)+0.1*(index+2);
            h_out[i*widthStepOutput+j]=gray;
        }
    }
}