How to write a Float Mat to a file in OpenCV

Using pure OpenCV API calls: // Declare what you need cv::FileStorage file(“some_name.ext”, cv::FileStorage::WRITE); cv::Mat someMatrixOfAnyType; // Write to file! file << “matName” << someMatrixOfAnyType; The file extension can be xml or yml. In both cases you get a small header that you can easily remove/parse, then you have access to the data in a floating … Read more

load image with openCV Mat c++

I’ve talked about this so many times before, I guess it’s pointless to do it again, but code defensively: if a method/function call can fail, make sure you know when it happens: Mat I = imread(“C:\\images\\apple.jpg”, 0); if (I.empty()) { std::cout << “!!! Failed imread(): image not found” << std::endl; // don’t let the execution … Read more

How to read a v7.3 mat file via h5py?

Matlab 7.3 file format is not extremely easy to work with h5py. It relies on HDF5 reference, cf. h5py documentation on references. >>> import h5py >>> f = h5py.File(‘test.mat’) >>> list(f.keys()) [‘#refs#’, ‘struArray’] >>> struArray = f[‘struArray’] >>> struArray[‘name’][0, 0] # this is the HDF5 reference <HDF5 object reference> >>> f[struArray[‘name’][0, 0]].value # this is … Read more

C++ OpenCV image sending through socket

Get Mat.data and directly send to the socket, the data order is BGR BGR BGR…. On the receiving side assumes you know the size of image. After receiving just assign the received buffer(BGR BGR… array) to a Mat. Client:- Mat frame; frame = (frame.reshape(0,1)); // to make it continuous int imgSize = frame.total()*frame.elemSize(); // Send … Read more

cv::Mat to QImage and back

Code looks fine with one exception. Memory management. cv::Mat doesn’t work like QImage in this mater. Remember that QImage is using copy on write mechanism and shares memory for each copy. cv::Mat also shares memory but it doesn’t do copy on write (I’m also new with open cv (2 weeks) so I can’t explain yet … Read more

How to capture the desktop in OpenCV (ie. turn a bitmap into a Mat)?

After MUCH trial and error, I managed to write a function to do it. here it is for anyone else who might want it: #include “stdafx.h” #include “opencv2/core/core.hpp” #include “opencv2/imgproc/imgproc.hpp” #include <opencv2/highgui/highgui.hpp> #include <Windows.h> #include <iostream> #include <string> using namespace std; using namespace cv; Mat hwnd2mat(HWND hwnd){ HDC hwindowDC,hwindowCompatibleDC; int height,width,srcheight,srcwidth; HBITMAP hbwindow; Mat src; … Read more