如何在OpenCv中将cv :: Mat转换为灰度? [英] How can I convert a cv::Mat to a gray scale in OpenCv?

查看:520
本文介绍了如何在OpenCv中将cv :: Mat转换为灰度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将cv :: Mat转换为灰度?

How can I convert a cv::Mat to a gray scale?

我试图从opencv运行drawKeyPoints func,但是我一直得到一个Assertion Filed错误。我猜想它需要在参数中接收灰度图像而不是彩色图像。

I am trying to run drawKeyPoints func from opencv, however I have been getting an Assertion Filed error. My guess is that it needs to receive a gray scale image rather than a color image in the parameter.

void SurfDetector(cv::Mat img){
vector<cv::KeyPoint> keypoints;
cv::Mat featureImage;

cv::drawKeypoints(img, keypoints, featureImage, cv::Scalar(255,255,255) ,cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

cv::namedWindow("Picture");
cv::imshow("Picture", featureImage);

}

推荐答案

使用C ++ API,函数名稍有改变,现在写入:

Using the C++ API, the function name has slightly changed and it writes now:

#include <opencv2/imgproc/imgproc.hpp>

cv::Mat greyMat, colorMat;
cv::cvtColor(colorMat, greyMat, CV_BGR2GRAY);

主要的困难是函数在 imgproc 在核心),默认情况下,cv :: Mat是蓝绿红(BGR)顺序,而不是更常见的RGB。

The main difficulties are that the function is in the imgproc module (not in the core), and by default cv::Mat are in the Blue Green Red (BGR) order instead of the more common RGB.

OpenCV 3

从OpenCV 3.0开始,还有另一个约定。
转换代码嵌入在命名空间 cv :: 中,并以 COLOR 为前缀。
所以,示例变为:

Starting with OpenCV 3.0, there is yet another convention. Conversion codes are embedded in the namespace cv:: and are prefixed with COLOR. So, the example becomes then:

#include <opencv2/imgproc/imgproc.hpp>

cv::Mat greyMat, colorMat;
cv::cvtColor(colorMat, greyMat, cv::COLOR_BGR2GRAY);

据我所见,包含的文件路径没有改变)。

As far as I have seen, the included file path hasn't changed (this is not a typo).

这篇关于如何在OpenCv中将cv :: Mat转换为灰度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆