我需要将图像数据从Mat对象(OpenCV)传输到ZXing::ImageView对象中的const unsigned char*
;通常,我只使用object.data
(假设该对象名为“对象”),但需自担风险,如果传输存在问题,请从那里去。但是,在这种情况下,数据成员变量为空。这个Mat对象的确会生成带有imshow
的图像,所以我不确定其他地方。我已经阅读了文档,但是由于经验有限和缺乏知识,我无法以合理的步伐或朝着相关方向有效地阅读文档。这是我的代码:
#include <opencv2/opencv.hpp>
#include <ZXing/ReadBarcode.h>
using namespace cv;
Mat applyThreshold(Mat gradient);
Mat erodeAnddilate(Mat threshold_applied);
void readBarCode(Mat dest);
int main() {
std::string file = "C:\\Users\\these\\Desktop\\cropped.JPG";
namedWindow("imageview", WINDOW_NORMAL);
Mat src = imread(file, IMREAD_COLOR);
Mat thresh_applied = applyThreshold(src);
Mat dest = erodeAnddilate(thresh_applied);
readBarCode(dest);
imshow("imageview", dest);
waitKey(0);
return 0;
}
Mat applyThreshold(Mat gradient) {
Mat dest, gray;
cvtColor(gradient, gray, COLOR_BGR2GRAY);
threshold(gray, dest, 0, 255, THRESH_BINARY + THRESH_OTSU);
return dest;
}
Mat erodeAnddilate(Mat threshold_applied) {
Mat dest;
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
morphologyEx(threshold_applied, dest, MORPH_CLOSE, kernel, Point(-1, -1), 2);
return dest;
}
void readBarCode(Mat dest) {
ZXing::ImageView test(dest.data, dest.size().width, dest.size().height, ZXing::ImageFormat::None);
ZXing::Result truth = ZXing::ReadBarcode(test);
int momentoftruth = 0;
}
函数readBarCode()
是问题所在。对于其他地方可能很糟糕的代码,我们深表歉意。 :)编辑:接受的解决方案是唯一正式给出的解决方案,但是所有评论共同帮助我意识到了我在考虑有关数据变量时的错误。我现在将数据变量视为指针,并且将浅复制与深复制作为潜在的解决方案。我对Mat对象发生了什么有更好的了解,并考虑了我的问题得到了回答。感谢大家。
最佳答案
尝试将对Mat
对象的引用传递给函数,或者如果要复制数据以创建新图像,则显式使用clone()
方法获取图像的深拷贝。
就像:
Mat applyThreshold(Mat& gradient) {
Mat dest, gray;
cvtColor(gradient, gray, COLOR_BGR2GRAY);
threshold(gray, dest, 0, 255, THRESH_BINARY + THRESH_OTSU);
return dest;
}
要么:// ...
Mat thresh_applied = applyThreshold(src.clone());
// ...
关于c++ - Mat对象不在数据变量中时,该对象将数据存储在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64806705/