14

I'm using Ubuntu 14.04 and I'm trying to compile this code, but I get these errors no matter what, I believe it has something to do with including the OpenCV library, but I'm not sure. Could anyone help me out? Errors:

main.cc:66:37: error: ‘CV_RETR_EXTERNAL’ was not declared in this scope

main.cc:66:55: error: ‘CV_CHAIN_APPROX_NONE’ was not declared in this scope

main.cc:81:28: error: ‘CV_BGR2GRAY’ was not declared in this scope

The Code(sorry for the formatting, I just can't get this right):

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <tesseract/baseapi.h>
#include <iostream>
void rgb2cmyk(cv::Mat& src, std::vector<cv::Mat>& cmyk)
{
CV_Assert(src.type() == CV_8UC3);

cmyk.clear();
for (int i = 0; i < 4; ++i)
    cmyk.push_back(cv::Mat(src.size(), CV_32F));

for (int i = 0; i < src.rows; ++i)
{
    for (int j = 0; j < src.cols; ++j)
    {
        cv::Vec3b p = src.at<cv::Vec3b>(i,j);

        float r = p[2] / 255.;
        float g = p[1] / 255.;
        float b = p[0] / 255.;
        float k = (1 - std::max(std::max(r,g),b));

        cmyk[0].at<float>(i,j) = (1 - r - k) / (1 - k); 
        cmyk[1].at<float>(i,j) = (1 - g - k) / (1 - k);
        cmyk[2].at<float>(i,j) = (1 - b - k) / (1 - k);
        cmyk[3].at<float>(i,j) = k;
    }
}
}

int main()
{
cv::Mat im0 = cv::imread("scratchcard.png");
if (!im0.data)
    return -1;

std::vector<cv::Mat> cmyk;
rgb2cmyk(im0, cmyk);

cv::Mat im1;
im1 = cmyk[3].mul(1 - cmyk[1]) > 0.25;

cv::Mat im2;
im1.convertTo(im2, CV_8U);

std::vector<std::vector<cv::Point> > contours;
cv::findContours(im2, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);

double max_area = 0;
int max_idx = 0;
for (int i = 0; i < contours.size(); i++)
{
    double area = cv::contourArea(contours[i]);
    max_idx  = area > max_area ? i : max_idx;
    max_area = area > max_area ? area : max_area;
}

im2.setTo(cv::Scalar(0));
cv::drawContours(im2, contours, max_idx, cv::Scalar(255), -1);

cv::Mat im3;
cv::cvtColor(im0, im3, CV_BGR2GRAY);
im3 = ((255 - im3) & im2) > 200;

cv::Mat dst = im3.clone();
cv::findContours(dst.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
for (int i = 0; i < contours.size(); i++)
{
    if (cv::contourArea(contours[i]) < 100)
        cv::drawContours(dst, contours, i, cv::Scalar(0), -1);
}

tesseract::TessBaseAPI tess;
tess.Init(NULL, "eng", tesseract::OEM_DEFAULT);
tess.SetVariable("tessedit_char_whitelist", "0123456789");
tess.SetPageSegMode(tesseract::PSM_SINGLE_BLOCK);
tess.SetImage((uchar*)dst.data, dst.cols, dst.rows, 1, dst.cols);

char* out = tess.GetUTF8Text();
std::cout << out << std::endl;

cv::imshow("src", im0);
cv::imshow("dst", dst);
cv::waitKey();
return 0;
}

UPDATE: CV_RETR_EXTERNAL and CV_CHAIN_APPROX_NONE errors were fixed by using cv::"whatever". However, CV_BGR2GRAY error persists, if changed to cv::COLOR_BGR2GRAY the whole code gets highlighted as buggy. Anyone has a clue?

Community
  • 1
  • 1
Bernardo Meurer
  • 2,295
  • 5
  • 31
  • 52
  • 1
    Did you try ::CV_RETR_EXTERNAL instead of CV_RETR_EXTERNAL? – ypnos May 28 '14 at 22:05
  • 3
    What version of OpenCV are you using? CV_RETR_EXTERNAL is from the C API, so it might not exist. Try cv::RETR_EXTERNAL instead -- it should exist if your setup is correct. – Aurelius May 28 '14 at 22:14
  • Arelius and ypnos, I' using the latest OpenCV version (2.4.9 if i'm not wrong). I'll test your solutions as soon is i get to my Ubuntu pc. – Bernardo Meurer May 28 '14 at 22:22
  • Using cv::RETR_EXTERNAL fixed part of it, i still get `main.cc:82:27: error: ‘BGR2GRAY’ is not a member of ‘cv’` – Bernardo Meurer May 29 '14 at 01:23

2 Answers2

37

it seems, you are (accidentally) using the 3.0(master) opencv branch.

a lot of constants have changed there, like most of the CV_ prefixes were changed to cv:: namespace, CV_BGR2GRAY is now cv::COLOR_BGR2GRAY, etc.

also all module headers went one up, like opencv2/imgproc.hpp .

if you got the code from the github repo, and want to use the 2.4.9 branch instead,

git checkout 2.4

(in the opencv folder) will take you there. ofc you will have to rerun cmake and recompile

berak
  • 39,159
  • 9
  • 91
  • 89
11

Use COLOR_RGB2GRAY rather than COLOR_BGR2GRAY. It worked for me, though I am running OpenCV 3.0. You may have to use cv::COLOR_RGB2GRAY as the others recommended, if you are running 2.4.

Graeme Rock
  • 601
  • 5
  • 21