Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opencv2 cvtColor() does not work

Tags:

opencv

qt

I am using OpenCV2 on Ubuntu 12.04. I can successfully run image read-display codes. However I am not able to run codes with inbuilt functions eg. cvtColor()

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <stdio.h>

int main(int argc, char *argv[])
{
    cv::Mat image = cv::imread("img.jpg");

    if( image.data == NULL )
    {
        printf( "file cannot be loaded\n");
        return 1;
    }

    cv::namedWindow("My");
    cv::imshow("My", image);

    cv::Mat result;
    cv::cvtColor(image, result, CV_BGR2Luv);

    cv::imwrite("outImg.jpg", result);

    cv::waitKey(0);

    return 0;
}

I am using Qt-creator for my OpenCV After compiling with --libs, --cflags I get following compiler error:

make: Entering directory `/home/swaroop/Work/ai-junkies/cuda/uc_davis/opencv2.x/OpenCV2Test'
g++ -g -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4 -I/usr/include/opencv -I. -o main.o main.cpp
main.cpp: In function 'int main(int, char**)':
main.cpp:22:29: error: 'CV_BGR2Luv' was not declared in this scope
main.cpp:22:39: error: 'cvtColor' was not declared in this scope

Please help me fix this.

like image 758
mkuse Avatar asked Apr 16 '13 09:04

mkuse


People also ask

What is the purpose of cv2 cvtColor () function?

cv2. cvtColor() method is used to convert an image from one color space to another. There are more than 150 color-space conversion methods available in OpenCV.

What does cv2 cvtColor return?

It returns the Coverted color space image. The default color format in OpenCV is often referred to as RGB, but it is actually BGR (the bytes are reversed). So the first byte in a standard (24-bit) color image will be an 8-bit Blue component, the second byte will be Green, and the third byte will be Red.

Why do we convert BGR to RGB?

When the image file is read with the OpenCV function imread() , the order of colors is BGR (blue, green, red). On the other hand, in Pillow, the order of colors is assumed to be RGB (red, green, blue). Therefore, if you want to use both the Pillow function and the OpenCV function, you need to convert BGR and RGB.

What is RGB to HSV color space in opencv2?

Since OpenCV represents our image in BGR order rather than RGB, we specify the cv2. COLOR_BGR2HSV flag to indicate that we want to convert from BGR to HSV.


2 Answers

cvtColor declared in opencv2/imgproc/imgproc.hpp

keep in mind it's #include not #import

#include <opencv2/imgproc/imgproc.hpp>
like image 70
Vitaly S. Avatar answered Sep 22 '22 22:09

Vitaly S.


Alternatively, if you are testing things and not concerned with overdoing the includes, you can simply have one line:

#include <opencv2/opencv.hpp>

and it will include most opencv2 headers.

like image 36
scuac Avatar answered Sep 19 '22 22:09

scuac