Cac*_*ito 5 c c++ opencv pointers class
目的是在C中使用OpenCV3。OpenCV拥有C API,但很久以前就已弃用。
因此,我所做的是C ++中的一种抽象,它将所有指针都class cv::Something转换成void *我无法在C中取消引用的指针,但是可以在extern "C"完成工作的C ++ 函数之间传递。
为了使用这种抽象,我做了一些C函数,该函数应该从文件中读取图像并将其保存到新文件中:
#include "foo.h"
#include "libalx/extra/cv.h"
int foo(const char *restrict src, const char *restrict dest)
{
void *img;
int status;
status = -1;
if (alx_cv_alloc_img(&img))
return -1;
if (alx_cv_init_img(img, 1, 1)) // Segfault is here
goto out_free;
if (alx_cv_imread(img, src))
goto out_free;
if (alx_cv_imwrite(img, dest))
goto out_free;
status = 0;
out_free:
alx_cv_free_img(img);
return status;
}
Run Code Online (Sandbox Code Playgroud)
创建此抽象所需的文件如下:
cv.h:
#pragma once /* libalx/extra/cv.h */
#include <stddef.h>
__attribute__((nonnull))
int alx_cv_alloc_img(void **restrict img);
__attribute__((nonnull))
void alx_cv_free_img (void *restrict img);
__attribute__((nonnull))
int alx_cv_init_img (void *restrict img, ptrdiff_t w, ptrdiff_t h);
__attribute__((nonnull))
int alx_cv_imread (void *restrict img, const char *restrict fname);
__attribute__((nonnull))
void alx_cv_imwrite (const void *restrict img, const char *restrict fname);
Run Code Online (Sandbox Code Playgroud)
cv.hpp:
#pragma once /* libalx/extra/cv.hpp */
#include <cstddef>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#define restrict __restrict__
extern "C"
{
[[gnu::nonnull]]
int alx_cv_alloc_img(void **restrict img);
[[gnu::nonnull]]
void alx_cv_free_img (void *restrict img);
[[gnu::nonnull]]
int alx_cv_init_img (void *restrict img, ptrdiff_t w, ptrdiff_t h);
[[gnu::nonnull]]
int alx_cv_imread (void *restrict img, const char *restrict fname);
[[gnu::nonnull]]
void alx_cv_imwrite (const void *restrict img, const char *restrict fname);
} /* extern "C" */
namespace alx {
namespace CV {
[[gnu::nonnull]]
int alloc_img (class cv::Mat **restrict img);
[[gnu::nonnull]]
void free_img (class cv::Mat *restrict img);
[[gnu::nonnull]]
int init_img (class cv::Mat *restrict rect,
ptrdiff_t w, ptrdiff_t h);
[[gnu::nonnull]]
int imread (class cv::Mat *restrict img,
const char *restrict fname);
[[gnu::nonnull]]
void imwrite (const class cv::Mat *restrict img,
const char *restrict fname);
} /* namespace CV */
} /* namespace alx */
Run Code Online (Sandbox Code Playgroud)
cv.cpp:
#include "libalx/extra/cv.hpp"
#include <cstddef>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "libalx/base/stdlib/alloc/mallocs.hpp"
#define restrict __restrict__
int alx::CV::alloc_img (class cv::Mat **restrict img)
{
return alx_mallocs(img, 1);
}
int alx_cv_alloc_img (void **restrict img)
{
return alx::CV::alloc_img((class cv::Mat **)img);
}
void alx::CV::free_img (class cv::Mat *restrict img)
{
img->release();
free(img);
}
void alx_cv_free_img (void *restrict img)
{
alx::CV::free_img((class cv::Mat *)img);
}
int alx::CV::init_img (class cv::Mat *restrict img,
ptrdiff_t w, ptrdiff_t h)
{
if (w < 1 || h < 1)
return 1;
*img = cv::Mat::zeros(cv::Size(w, h), CV_8UC3); // Segfault is here
return 0;
}
int alx_cv_init_img (void *restrict img, ptrdiff_t w, ptrdiff_t h)
{
return alx::CV::init_img((class cv::Mat *)img, w, h);
}
int alx::CV::imread (class cv::Mat *restrict img,
const char *restrict fname)
{
*img = cv::imread(fname, CV_LOAD_IMAGE_COLOR);
if (img->empty())
return -1;
return 0;
}
int alx_cv_imread (void *restrict img, const char *restrict fname)
{
return alx::CV::imread((class cv::Mat *)img, fname);
}
void alx::CV::imwrite (const class cv::Mat *restrict img,
const char *restrict fname)
{
cv::imwrite(fname, *img);
}
void alx_cv_imwrite (const void *restrict img,
const char *restrict fname)
{
alx::CV::imwrite((const class cv::Mat *)img, fname);
}
Run Code Online (Sandbox Code Playgroud)
在这里,我使用的alx_mallocs是一个封闭的外壳malloc:
mallocs.hpp:
#pragma once /* libalx/base/stdlib/alloc/mallocs.hpp */
#include <cstddef>
/*
* [[gnu::nonnull]]
* int alx_mallocs(type **restrict p, ptrdiff_t nmemb);
*/
#define alx_mallocs(ptr, nmemb) ( \
{ \
auto ptr_ = (ptr); \
void *vp; \
\
vp = alx_mallocs__(nmemb, sizeof(**ptr_)); \
*ptr_ = static_cast<typeof(*ptr_)>(vp); \
\
!(*ptr_); \
} \
)
extern "C"
{
[[gnu::malloc]]
void *alx_mallocs__(ptrdiff_t nmemb, size_t size);
}
Run Code Online (Sandbox Code Playgroud)
mallocs.c:
#include "libalx/base/stdlib/alloc/mallocs.h"
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
void *alx_mallocs__(ptrdiff_t nmemb, size_t size)
{
if (nmemb < 0)
goto ovf;
if (nmemb > (PTRDIFF_MAX / (ptrdiff_t)size))
goto ovf;
return malloc(size * nmemb);
ovf:
errno = EOVERFLOW;
return NULL;
}
Run Code Online (Sandbox Code Playgroud)
但是,似乎仅分配和尝试初始化分配的内存是不够的。我的猜测是有些构造函数没有被调用。
如何做到这一点,以免出现段错误?
而不是尝试用这个调用复制赋值运算符
*img = cv::Mat::zeros(cv::Size(w, h), CV_8UC3);
Run Code Online (Sandbox Code Playgroud)
您可以使用placement-new将对象构造到已分配的内存中
new (img) cv::Mat(cv::Size(w, h), CV_8UC3, 0.0);
Run Code Online (Sandbox Code Playgroud)
但是,您还应该确保在将类型构造到先前分配的内存中时遵守类型的对齐方式。