我正试着做一个dip::EuclideanSkeleton。但是,在执行时,它会抛出一个未处理的异常。返回类型与传递给函数的类型之间是否不匹配?如果是的话,我怎样才能解决这个问题?我再也猜不到了。没有dip::EuclideanSkeleton,这个程序就能工作。
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <string>
#include "diplib.h"
#include "diplib/file_io.h"
#include "diplib/regions.h"
#include "diplib/display.h"
#include <dip_opencv_interface.h>
#include "diplib/binary.h"
int main() {
cv::Mat img = cv::imread("Cables.jpg", cv::IMREAD_GRAYSCALE);
if (img.empty()) {
return -1;
}
cv::Mat thresh = img.clone();
medianBlur(thresh, thresh, 7);
threshold(thresh, thresh, 127, 255, cv::THRESH_BINARY);
dip::Image image = dip_opencv::MatToDip(thresh);
dip::Image conv = dip::EuclideanSkeleton(image);
cv::Mat final = dip_opencv::DipToMat(conv);
cv::imshow("", final);
cv::waitKey(0);
}在:
退出:
Skel_endpoints.exe中0x00007FF85A0F3B29处的未处理异常: C++异常: dip::ParameterError位于内存位置0x000000BAF730EDD8。
发布于 2020-09-23 15:16:14
问题是dip::EuclideanSkeleton需要一个二进制映像,但是OpenCV不知道二进制类型。thresh是类型为8位的无符号整数,当转换为DIPlib对象时,转换不能知道这是指8位uint图像还是二进制图像。此外,OpenCV使用0和255的值来表示二进制图像,而DIPlib使用0和1的值,这是在文档中描述。
解决方案是强制8位uint图像为二进制.最简单的方法是简单地将0:dip_opencv::MatToDip(thresh) > 0进行比较。如果这样做,可以跳过对cv::threshold的调用。
接下来,转换dip_opencv::DipToMat(conv)将生成一个CV_8U映像,但包含值0和1,而不是其他OpenCV函数所期望的0和255。同样,一种简单的方法就是将结果乘以255:dip_opencv::DipToMat(conv) * 255。
该程序将如下所示:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "diplib.h"
#include <dip_opencv_interface.h>
#include "diplib/binary.h"
int main() {
cv::Mat img = cv::imread("erika.tif", cv::IMREAD_GRAYSCALE);
if (img.empty()) {
return -1;
}
cv::Mat thresh = img.clone();
medianBlur(thresh, thresh, 7);
dip::Image image = dip_opencv::MatToDip(thresh) > 127;
dip::Image conv = dip::EuclideanSkeleton(image);
cv::Mat final = dip_opencv::DipToMat(conv) * 255;
cv::imwrite("foo.png", final);
}不幸的是,MSVC没有自动显示未显示异常的what()字符串。我建议您在所有代码周围放置一个try/catch块,以便显示此字符串。DIPlib在引发的异常中提供了有用的信息:
int main() {
try {
// your code here
} catch (std::exception const& stde) {
std::cout << "Caught exception:\n" << stde.what() << '\n';
}
}https://stackoverflow.com/questions/64029804
复制相似问题