0

我有一个使用 C++ 的简单代码(与 OpenCV 结合)这里是代码(主要)

int main(int argc, char** argv)
{

    IplImage* image_input = cvLoadImage("test.jpg", CV_LOAD_IMAGE_UNCHANGED);
    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;

    int width     = image_input->width;
    int height    = image_input->height;

    h_grayscale ( h_in , h_out ) ;
    cvShowImage("Original", image_input);
    cvShowImage("CPU", image_output);
    cvReleaseImage(&image_input);
    cvReleaseImage(&image_output);
    waitKey(0);
}

这是灰度的功能

void h_grayscale( unsigned char* h_in, unsigned char* h_out){
    for (unsigned int i=0; i< width*height; i++){
        unsigned int index = i*3;
        double temp = 0.3*h_in[index]+0.6*h_in[index+1]+0.1*h_in[index+2];
        h_out[i] = (unsigned char)temp;
    }
}

但图像的结果用灰度图像分成 3 部分 结果

请告诉我我的代码有什么问题??T_T谢谢

4

1 回答 1

0

您需要编辑代码以仅创建一个通道输出图像,因为它是灰度图像:

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

此外,通过“使用 C++(使用 OpenCV)的简单代码”,我想像这样:

int main()
{
    Mat image_input = imread("test.jpg");
    Mat image_output;

    cvtColor(image_input, image_output, CV_BGR2GRAY);

    imshow("Original", image_input);
    imshow("CPU", image_output);
    waitKey(0);

    return 0;
}
于 2013-11-11T09:13:21.280 回答