46

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

}

mtvec
  • 16,168
  • 4
  • 47
  • 79
user1319603
  • 735
  • 1
  • 9
  • 12

2 Answers2

89

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

sansuiso
  • 8,519
  • 1
  • 36
  • 55
  • 4
    Under OpenCV 3, the conversion type constant has been changed to COLOR_BGR2GRAY – John Stephen Jan 26 '15 at 19:12
  • Thanks, I'll update the answer. OpenCV 3 is rolling out soon, it will be useful. – sansuiso Jan 27 '15 at 07: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! – Rika Jul 29 '20 at 12:29
  • 1
    Ok, I guess I found it```cv::cvtColor(img, greyMat, cv::COLOR_BGR2GRAY); cv::cvtColor(greyMat, greyMat, cv::COLOR_GRAY2BGR);``` – Rika Jul 29 '20 at 12:36
1

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

  }

}
Chanaka Fernando
  • 1,760
  • 13
  • 19