Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

}

like image 431
user1319603 Avatar asked Apr 27 '12 03:04

user1319603


People also ask

How do I convert to grayscale in OpenCV?

Step 1: Import OpenCV. Step 2: Read the original image using imread(). Step 3: Convert to grayscale using cv2. cvtcolor() function.

How do I convert an image to gray scale?

Right-click the picture that you want to change, and then click Format Picture on the shortcut menu. Click the Picture tab. Under Image control, in the Color list, click Grayscale or Black and White.

How do I convert an image to grayscale in OpenCV C++?

To convert an image to its GrayScale in C++ with OpenCV Then we read the image. We use function cvtcolor() to convert the image to it's Grayscale equivalent. This function does require changes to the Red, Green, Blue variants of color. Imshow() function is used to show the converted image and the previous one as well.

What is the function of OpenCV to grayscale image mat?

If you need a grayscale image, use: C++ Mat img = imread(filename, IMREAD_GRAYSCALE);


2 Answers

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

like image 103
sansuiso Avatar answered Sep 23 '22 09:09

sansuiso


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;     }    }  } 
like image 44
Chanaka Fernando Avatar answered Sep 21 '22 09:09

Chanaka Fernando