I need to transfer the image data from a Mat object (OpenCV) to a const unsigned char*
within a ZXing::ImageView object; typically, I just use (assuming the object is named "object") object.data
at my own risk and go from there if there are issues with the transfer. However, in this case, the data member variable is empty. This Mat object does produce an image with imshow
though so I'm not sure where else to look. I have gone through the documentation but my limited experience and lack of knowledge keeps me from effectively going through it at a reasonable pace or in a relevant direction. Here is my code:
#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;
}
The function readBarCode()
is where the issue lies. And apologies for the probably terrible code everywhere else, I have a lot to learn. :)
EDIT: The accepted solution was the only one officially given, but all of the comments collectively helped me realize my error in thinking about the data variable in question. I see the data variable as a pointer now, and will take shallow vs deep copying into consideration as a potential solution. I have a better understanding of what's going on with my Mat object and consider my question answered. Thanks everyone.
Try to pass the reference to your Mat
object to the functions, or if you want to copy the data for creating a new image, use explicitly the clone()
method to get deep copy of your image.
Like either:
Mat applyThreshold(Mat& gradient) {
Mat dest, gray;
cvtColor(gradient, gray, COLOR_BGR2GRAY);
threshold(gray, dest, 0, 255, THRESH_BINARY + THRESH_OTSU);
return dest;
}
or:
// ...
Mat thresh_applied = applyThreshold(src.clone());
// ...