Open-CV - Not loading correctly

2019-04-11 22:24发布

问题:

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?

回答1:

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



回答2:

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.