dip::欧几里得骨架不起作用(未处理的异常)
dip::EuclideanSkeleton doesn't work (Unhandled exception)
我正在尝试制作一个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);
}
in:
dataType_ {dt=UINT8 (1) } dip::DataType
out:
dataType_ {dt=SFLOAT (9) } dip::DataType
Skel_endpoints.exe中0x00007FF85A0F3B29处未处理的异常:Microsoft C++异常:内存位置0x000000BAF730EDD8处的dip::ParameterError。
问题是dip::EuclideanSkeleton
需要一个二进制映像,但OpenCV不知道二进制类型。thresh
是8位无符号整数类型,当转换为DIPlib对象时,转换无法知道这是8位uint图像还是二进制图像。此外,OpenCV使用0和255的值来表示二进制图像,而DIPlib使用0和1的值。文档中对此进行了描述。
解决方案是强制8位uint图像为二进制。最简单的方法是简单地与零进行比较: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';
}
}
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 为什么重载运算符上的异常说明符'<<'不适用于任何 std::ostream 对象,但对库中定义的运算符不起作用?
- boost::bind 在 VC++ 2010 中绑定引发异常的函数时不起作用
- 异常处理不起作用
- C++ 异常的双重释放损坏错误(复制构造函数不起作用)
- 异常处理+多态性,如果异常方法在同一类中不起作用,则不起作用