Canny 边缘检测 - 灰度图像总是以 3 通道的形式出现,无法使用?

发布于 2024-08-31 21:32:04 字数 649 浏览 9 评论 0原文

我正在阅读 O'Reilly 系列的《学习 OpenCV》一书,并尝试执行精明的边缘检测示例。

我选择的任何灰度图像似乎都有 3 个通道,据我所知,canny 只适用于单通道图像,所以这总是失败。我什至使用 OpenCV 提供的图像。

这是我的代码..

IplImage* doCanny(IplImage* in, double lowThresh, double highThresh, double aperture)
{
    if(in->nChannels != 1)
        return(0); //canny only handles gray scale images
    IplImage* out = cvCreateImage(cvSize(in->width, in->height), IPL_DEPTH_8U, 1);
    cvCanny(in, out, lowThresh, highThresh, aperture);
    return(out);
};

IplImage* img = cvLoadImage("someGrayscaleImage.jpg");
IplImage* out = doCanny(img, 10, 100, 3);

为什么这总是给我 3 通道图像?我该如何解决这个问题?

I am working through the book "Learning OpenCV" from the O'Reilly series and am trying to perform a canny edge detection sample.

Any grayscale image I choose seems to come up as having 3 channels, and to the best of my knowledge, canny only works with single channel images, so this always fails. I am even using the images provided by OpenCV.

Here is my code..

IplImage* doCanny(IplImage* in, double lowThresh, double highThresh, double aperture)
{
    if(in->nChannels != 1)
        return(0); //canny only handles gray scale images
    IplImage* out = cvCreateImage(cvSize(in->width, in->height), IPL_DEPTH_8U, 1);
    cvCanny(in, out, lowThresh, highThresh, aperture);
    return(out);
};

IplImage* img = cvLoadImage("someGrayscaleImage.jpg");
IplImage* out = doCanny(img, 10, 100, 3);

Why might this always give me 3-channel images? How can I solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伊面 2024-09-07 21:32:04

您可以将此方法与另一个参数一起使用。

IplImage* cvLoadImage(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR)

#define CV_LOAD_IMAGE_COLOR       1

#define CV_LOAD_IMAGE_GRAYSCALE   0

#define CV_LOAD_IMAGE_UNCHANGED  -1

默认参数是加载带有颜色的图像。您要做的就是用灰度加载它

这是一个示例

cvLoadImage("yourimage.jpg", CV_LOAD_IMAGE_GRAYSCALE);

这是该方法的详细说明。您可以在这里查看更多详细信息:
Open CV 2.0 参考

scolor – 加载的特定颜色类型图片:如果$> 0$,加载的图像强制为3通道彩色图像;如果为0,则加载的图像强制为灰度;如果 $ < 0 $,加载的图像将按原样加载(请注意,在当前实现中,alpha 通道(如果有)将从输出图像中剥离,例如 4 通道 RGBA 图像将作为 RGB 加载)。

You can use this method with another parameter

IplImage* cvLoadImage(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR)

#define CV_LOAD_IMAGE_COLOR       1

#define CV_LOAD_IMAGE_GRAYSCALE   0

#define CV_LOAD_IMAGE_UNCHANGED  -1

The default parameter is load image with color. What you have to do is to load it with grayscale

Here is an example

cvLoadImage("yourimage.jpg", CV_LOAD_IMAGE_GRAYSCALE);

Here is the detail explanation for that method. You can look at here for more details:
Open CV 2.0 References

scolor – Specific color type of the loaded image: if $ > 0 $, the loaded image is forced to be a 3-channel color image; if 0, the loaded image is forced to be grayscale; if $ < 0 $, the loaded image will be loaded as is (note that in the current implementation the alpha channel, if any, is stripped from the output image, e.g. 4-channel RGBA image will be loaded as RGB).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文