49

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

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);

}

0

2 Answers 2

98

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);

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

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).

4
  • 5
    Under OpenCV 3, the conversion type constant has been changed to COLOR_BGR2GRAY Commented Jan 26, 2015 at 19:12
  • Thanks, I'll update the answer. OpenCV 3 is rolling out soon, it will be useful.
    – sansuiso
    Commented Jan 27, 2015 at 7:50
  • @sansuiso Thanks, do you know how I can get a grayscaled image with the same channels as the input image? currently this method, converts the whole image into 1 channeled balck and white image. what should I do to just make my image black and white and also not 1 channeled!
    – Hossein
    Commented Jul 29, 2020 at 12:29
  • 2
    Ok, I guess I found itcv::cvtColor(img, greyMat, cv::COLOR_BGR2GRAY); cv::cvtColor(greyMat, greyMat, cv::COLOR_GRAY2BGR);
    – Hossein
    Commented Jul 29, 2020 at 12:36
2

May be helpful for late comers.

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"

using namespace cv;
using namespace std;

int main(int argc, char *argv[])
{
  if (argc != 2) {
    cout << "Usage: display_Image ImageToLoadandDisplay" << endl;
    return -1;
}else{
    Mat image;
    Mat grayImage;

    image = imread(argv[1], IMREAD_COLOR);
    if (!image.data) {
        cout << "Could not open the image file" << endl;
        return -1;
    }
    else {
        int height = image.rows;
        int width = image.cols;

        cvtColor(image, grayImage, CV_BGR2GRAY);


        namedWindow("Display window", WINDOW_AUTOSIZE);
        imshow("Display window", image);

        namedWindow("Gray Image", WINDOW_AUTOSIZE);
        imshow("Gray Image", grayImage);
        cvWaitKey(0);
        image.release();
        grayImage.release();
        return 0;
    }

  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.