使用opencv做图像拼接,总是返回ERR_NEED_MORE_IMGS,求解

aqbeyond 2020-07-01 05:53:16
新手用opencv做图像拼接,总是返回ERR_NEED_MORE_IMGS错误。
调用代码简单如下:

cv::Ptr<cv::Stitcher> stitcher = cv::Stitcher::create(cv::Stitcher::Mode::PANORAMA);
cv::Stitcher::Status status = stitcher->stitch(vImg, rImg);

if (cv::Stitcher::OK == status)
cv::imshow("Stitching Result", rImg);
else
printf("Stitching fail.");

opencv版本是4.1.1
拼接测试用图:
...全文
12303 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
C++ 版本opencv用你上面的两张图片拼接不成功,还是一样的报错
aqbeyond 2020-08-17
  • 打赏
  • 举报
回复
引用 6 楼 dongtinghong 的回复:
我发现stitcher算法并不是很鲁棒,估计得进里面修改代码,有些情况得自己处理
我从别人哪里知道用opencv是可以做的,而且就是拿的上面的图像。现在不能做自动拼接,就找方法做手动拼接了。 不过用addWeighted方法实现拼接效果不太理想。接缝太明显。
dongtinghong 2020-08-14
  • 打赏
  • 举报
回复
我发现stitcher算法并不是很鲁棒,估计得进里面修改代码,有些情况得自己处理
dongtinghong 2020-08-07
  • 打赏
  • 举报
回复
这两张图片确实不行,可能太暗了,不是彩色的。我是在mac上android studio上跑的,opencv4.1.2。要不你先看看他们之间的特征匹配点数是不是够多
aqbeyond 2020-08-07
  • 打赏
  • 举报
回复
引用 3 楼 dongtinghong 的回复:
找到原因了,更换一下图片顺序,android似乎要按一定顺序才行,iOS的就不需要,可能是android的opencv stitching比较底层,stitching分高度抽象版和细节版,细节版可以设置更多参数,有更高的自由度。Android版,我的代码如下: extern "C" JNIEXPORT jobject JNICALL Java_com_zy_fullimagemerge_ImageMergeTool_nativeStitchImages(JNIEnv *env, jclass clazz, jobjectArray bitmaps) { int count = env->GetArrayLength(bitmaps); std::vector<Mat> imgs; for (int i = 0; i < count; i++) { jobject bitmap = env->GetObjectArrayElement(bitmaps, i); int width = 0; int height = 0; int depth = 0; unsigned char* pixels = getPixelsFromBitmap(env, bitmap, &width, &height, &depth); int type = depth==4? CV_8UC4 : depth==3? CV_8UC3 : depth==1? CV_8UC1 : CV_8UC4; Mat mat = Mat(height, width, type, pixels); Mat gray; if (type == CV_8UC4) { cvtColor(mat, gray, COLOR_RGBA2RGB); imgs.push_back(gray); //imgs.push_back(mat); } else if (type == CV_8UC3) { imgs.push_back(mat); } else { cvtColor(mat,gray,COLOR_GRAY2RGB); imgs.push_back(gray); } free(pixels); } Mat result; Ptr<Stitcher> stitcher = Stitcher::create(); //这部分可以注释掉 Ptr<detail::BestOf2NearestMatcher> matcher = detail::BestOf2NearestMatcher::create(); stitcher->setFeaturesMatcher(matcher); stitcher->setBundleAdjuster(new detail::BundleAdjusterRay); stitcher->setSeamFinder(detail::NoSeamFinder::createDefault(detail::SeamFinder::NO)); stitcher->setExposureCompensator(detail::NoExposureCompensator::createDefault(detail::NoExposureCompensator::NO)); stitcher->setBlender(new detail::FeatherBlender()); Stitcher::Status status = stitcher->stitch(imgs, result); int type = result.type(); int depth = type == CV_8UC4? 4 : type == CV_8UC3? 3 : type == CV_8UC1? 1 : 4; jobject bitmap = createJavaBitmapWithPixels(env, result.data, result.cols, result.rows, depth); return bitmap; } 图片要有重合部分,如果失败,更换一下图片顺序
你用的是我上面两个图片做拼接吗?Opencv提供的接口很多图片是可以拼接的,就上面的图片拼接出错了。 另外有C++版本的代码吗,这边是在windows上跑的。
dongtinghong 2020-08-05
  • 打赏
  • 举报
回复
找到原因了,更换一下图片顺序,android似乎要按一定顺序才行,iOS的就不需要,可能是android的opencv stitching比较底层,stitching分高度抽象版和细节版,细节版可以设置更多参数,有更高的自由度。Android版,我的代码如下:
extern "C"
JNIEXPORT jobject JNICALL
Java_com_zy_fullimagemerge_ImageMergeTool_nativeStitchImages(JNIEnv *env, jclass clazz,
jobjectArray bitmaps) {

int count = env->GetArrayLength(bitmaps);
std::vector<Mat> imgs;
for (int i = 0; i < count; i++) {
jobject bitmap = env->GetObjectArrayElement(bitmaps, i);
int width = 0;
int height = 0;
int depth = 0;
unsigned char* pixels = getPixelsFromBitmap(env, bitmap, &width, &height, &depth);

int type = depth==4? CV_8UC4 : depth==3? CV_8UC3 : depth==1? CV_8UC1 : CV_8UC4;
Mat mat = Mat(height, width, type, pixels);


Mat gray;
if (type == CV_8UC4) {
cvtColor(mat, gray, COLOR_RGBA2RGB);
imgs.push_back(gray);
//imgs.push_back(mat);
} else if (type == CV_8UC3) {
imgs.push_back(mat);
} else {
cvtColor(mat,gray,COLOR_GRAY2RGB);
imgs.push_back(gray);
}

free(pixels);
}

Mat result;



Ptr<Stitcher> stitcher = Stitcher::create();

//这部分可以注释掉
Ptr<detail::BestOf2NearestMatcher> matcher = detail::BestOf2NearestMatcher::create();
stitcher->setFeaturesMatcher(matcher);
stitcher->setBundleAdjuster(new detail::BundleAdjusterRay);
stitcher->setSeamFinder(detail::NoSeamFinder::createDefault(detail::SeamFinder::NO));
stitcher->setExposureCompensator(detail::NoExposureCompensator::createDefault(detail::NoExposureCompensator::NO));
stitcher->setBlender(new detail::FeatherBlender());


Stitcher::Status status = stitcher->stitch(imgs, result);

int type = result.type();
int depth = type == CV_8UC4? 4 : type == CV_8UC3? 3 : type == CV_8UC1? 1 : 4;


jobject bitmap = createJavaBitmapWithPixels(env, result.data, result.cols, result.rows, depth);

return bitmap;
}


图片要有重合部分,如果失败,更换一下图片顺序
aqbeyond 2020-07-27
  • 打赏
  • 举报
回复
这里都没什么人吗?

4,269

社区成员

发帖
与我相关
我的任务
社区描述
OpenCV相关技术交流专区
计算机视觉人工智能opencv 技术论坛(原bbs) 广东省·深圳市
社区管理员
  • OpenCV
  • 幻灰龙
  • OpenCV中国团队
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

OpenCV中国团队官方博客:https://blog.csdn.net/opencvchina

试试用AI创作助手写篇文章吧