将颜色图像转换为OpenCV中的灰度问题

Converting a color image to grayscale issue in OpenCV

本文关键字:灰度 问题 OpenCV 颜色 图像 转换      更新时间:2023-10-16

当我尝试将颜色图像转换为灰度时,我正在面临问题。错误是:" cvgetSize中的坏参数(数组应为cvmat或iplimage)",但是当我评论所有与灰度相关的行时,我可以设法加载原始颜色映像并显示它。如何解决此错误?

#include <opencvcv.h>
#include <opencvhighgui.h>
#include <iostream>
#include <stdio.h>

int main(int argc, char** argv)
{
    //Loading the color image 
    IplImage* frame = cvLoadImage("lena.jpg");
    //Converting the color image to grayscale
    IplImage* grayframe = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
    cvCvtColor(frame, grayframe, CV_RGB2GRAY);
    //Creating a window for color image
    cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
    //Creating a window for grayscale image
    cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE);
    // Showing the color image
    cvShowImage("Example1", frame);
    // Showing the grayscale image
    cvShowImage("Example2", grayframe);
    //Showeing for X seconds
    cvWaitKey(2000);
    cvReleaseImage(&frame);
    cvDestroyWindow("Example1");
    cvReleaseImage(&grayframe);
    cvDestroyWindow("Example2");
    return 0;
}

为什么要iPlimage?

如果您想在以后扩展示例,请尝试使用MAT。

Mat image = imread("lena.jpg");
Mat gray;
cvtColor(image,gray,CV_BGR2GRAY);

这会轻松,将为您提供灰度图像。

但是,如果有使用C API的特定原因,那么问题在

IplImage* grayframe = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);

我不知道这样做的确切原因,但是,我可以为您提供运行代码的替代方法。

int x= frame->width, y=frame->height;
IplImage* grayframe = cvCreateImage(cvSize(x,y), IPL_DEPTH_8U, 1);

tyr,它可能对您有用