Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flycv与opencv同时读图,opencv读图失败,数据为空 #116

Open
caoxiang520 opened this issue Feb 1, 2023 · 3 comments
Open

flycv与opencv同时读图,opencv读图失败,数据为空 #116

caoxiang520 opened this issue Feb 1, 2023 · 3 comments
Assignees

Comments

@caoxiang520
Copy link

测试环境:台式ubuntu20.04,opencv3.4.14,flycv1.0.0
效果:fcv和opencv各自注释掉对方,独立的运行没问题,但是一起执行,opencv读图失败,图像数据为空

测试参考代码:
#include
#include <opencv2/opencv.hpp>
#include <dirent.h>
#include "flycv.h"
#include "pthread.h"
void calc_param(float& ratio, float& dww, float& dhh, int org_width, int org_height, int new_width, int new_hieght,
bool is_auto, bool scale_fill, bool scale_up, int stride) {
float r = std::min(new_width * 1.0 / org_width, new_hieght * 1.0 / org_height);
if (!scale_up)
r = std::min(r, float(1.0));

// ratio_w_ = r;
// ratio_h_ = r;
ratio = r;
int new_unpad_w_ = int(org_width * r);
int new_unpad_h_ = int(org_height * r);
int dw = new_width - new_unpad_w_;
int dh = new_hieght - new_unpad_h_;
if (is_auto) {
dw = dw % stride;
dh = dh % stride;
} else if (scale_fill) {
dw = 0, dh = 0;
new_unpad_w_ = new_width;
new_unpad_h_ = new_hieght;
float ratio_w_ = new_width * 1.0 / org_width;
float ratio_h_ = new_hieght * 1.0 / org_height;
}
// dw_ = dw / 2.0;
// dh_ = dh / 2.0;
dww = dw / 2.0;
dhh = dh / 2.0;
}
int main(int argc, char** argv) {

// // fcv的处理
fcv::Mat src_fcv = fcv::imread("../100.jpg");
std::cout << src_fcv.width() << " " << src_fcv.height() << std::endl;
float ratio_fcv, dww_fcv, dhh_fcv;
calc_param(ratio_fcv, dww_fcv, dhh_fcv, src_fcv.width(), src_fcv.height(), 640, 640, false, false, true, 2);
fcv::Mat resize_im_fcv;
double r_fcv = ratio_fcv;
fcv::resize(src_fcv, resize_im_fcv, fcv::Size(), r_fcv, r_fcv);
fcv::Mat padding_img_fcv;
int top_fcv = int(round(dhh_fcv - 0.1));
int bottom_fcv = int(round(dhh_fcv + 0.1));
int left_fcv = int(round(dww_fcv - 0.1));
int right_fcv = int(round(dww_fcv + 0.1));
fcv::Scalar color_fcv(114, 114, 114);
fcv::copy_make_border(resize_im_fcv, padding_img_fcv, top_fcv, bottom_fcv, left_fcv, right_fcv, fcv::BorderType::BORDER_CONSTANT, color_fcv);
fcv::imwrite("padding_img_fcv.jpg", padding_img_fcv);

// //opencv的处理
cv::Mat src_cv = cv::imread("../200.jpg");
std::cout << src_cv.cols << "  " << src_cv.rows << std::endl;
float ratio, dww, dhh;
calc_param(ratio, dww, dhh, src_cv.cols, src_cv.rows, 640, 640, false, false, true, 2);
cv::Mat resize_im_cv;
cv::resize(src_cv, resize_im_cv, cv::Size(), ratio, ratio);
cv::Mat padding_img_cv;
int top = int(round(dhh - 0.1));
int bottom = int(round(dhh + 0.1));
int left = int(round(dww - 0.1));
int right = int(round(dww + 0.1));
cv::Scalar color_cv(114, 114, 114);
cv::copyMakeBorder(resize_im_cv, padding_img_cv, top, bottom, left, right, cv::BORDER_CONSTANT, color_cv);
cv::imwrite("padding_img_cv.jpg", padding_img_cv);
return 0;

}

@VoidSalts
Copy link
Collaborator

方便的话,可以在
// //opencv的处理 cv::Mat src_cv = cv::imread("../200.jpg"); std::cout << src_cv.cols << " " << src_cv.rows << std::endl;

这里前面加上一句
std::cout << "file read access : "<< access("../200.jpg", R_OK) << std::endl;
(头文件加下#include <unistd.h>)
看下输出么

@caoxiang520
Copy link
Author

file read access : 0

@VoidSalts
Copy link
Collaborator

我这边在x86的linux系统上试了下,确实有这个现象
目前推测原因是flycv与opencv的图像编解码实现都基于libpng,libjpeg/libturbojpeg等开源三方库,在一起链接时,如果版本不一致导致的问题
类似现象参考(开源库dlib与opencv一同使用时,imread为空):https://stackoverflow.com/questions/59945307/cvimread-fails-in-c-project-when-we-mix-opencv-and-dlib
短期解决方案:两者共用时,图像IO仅使用其中一种

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants