c++ - Open-CV - 加载不正确

标签 c++ opencv image-processing ocr tesseract

我正在使用 Ubuntu 14.04 并尝试编译此代码,但无论如何我都会遇到这些错误,我相信这与包含 OpenCV 库有关,但我不确定。谁能帮帮我? 错误:

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

代码(抱歉格式问题,我就是没弄对):

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

更新:CV_RETR_EXTERNAL 和 CV_CHAIN_APPROX_NONE 错误已通过使用 cv::"whatever"修复。 但是,CV_BGR2GRAY 错误仍然存​​在,如果更改为 cv::COLOR_BGR2GRAY,整个代码将突出显示为错误。有人知道吗?

最佳答案

看来,您正在(不小心)使用 3.0(master) opencv 分支。

那里有很多常量发生了变化,比如大多数 CV_ 前缀被更改为 cv::命名空间, CV_BGR2GRAY 现在是 cv::COLOR_BGR2GRAY 等。

所有的模块头也都上升了,比如 opencv2/imgproc.hpp 。

如果您从 github 存储库中获取了代码,并且想改用 2.4.9 分支,

git checkout 2.4

(在 opencv 文件夹中)将带您到那里。 ofc 你将不得不重新运行 cmake 并重新编译

关于c++ - Open-CV - 加载不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23922620/

相关文章:

android - 二值图像中的 FindContours 和 CountObject

c++ - 将 C 组件集成到 C++ 框架中的最佳实践是什么?

python - 特定区域的opencv颜色检测

c++ - 如何防止构造函数被临时调用

opencv - 查找最适合的图像

opencv - 如何在opencv中使用离散余弦变换(DCT)

c++ - 访问随机图像像素且最多一次的快速方法

OpenCV - 在背景减法器 MOG 中修复前景

c++ - 使用 Pragma 以字节为单位适应套接字的大小

c# - 命名管道适合这种情况吗?