Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializing OpenCV Mat problem #1070

Closed
ArkadiuszRaj opened this issue Apr 30, 2018 · 7 comments
Closed

Serializing OpenCV Mat problem #1070

ArkadiuszRaj opened this issue Apr 30, 2018 · 7 comments
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@ArkadiuszRaj
Copy link

ArkadiuszRaj commented Apr 30, 2018

Hello,

Trying to serialise Mat object taken from camera

        json j;	
        Mat image;
	j["PREVIEW"] = {
		{"GAIN", gain()},
		{"EXPOSURE", exposure()},
		{"IMAGE", image},
	};

Of course specialised serializer is prepared, for the sake of clarity only one Mat depth is shown:

void to_json(json& j, const Mat& m)  
{
	json array = json::array();

	int cols = m.cols;
	int rows = m.rows;
	int depth = m.depth();
	
	if( m.isContinuous() ) {
		cols *= rows,
		rows = 1;
	}
	switch(depth) {
		case CV_8U:
			for(int row = 0; row < rows; row++) {
				auto vp = m.ptr<uchar>(row);
				for(int col=0; col < cols; col++, vp++)
					array.push_back(*vp); 
			}
			break;
	}
	j = json{
		{"COLS", cols},
		{"ROWS", rows},
		{"TYPE", depth},
		{"DATA", array}
	};
}

But code throws:

OpenCV(4.0.0-pre) /usr/local/include/opencv2/core/mat.inl.hpp:183: error: (-210:Unsupported format or combination of formats) in function '_OutputArray'
> std::vector<bool> cannot be an output array

It seems that somewhere under the hood the Mat type is changed while calling serializer.

But code works when I am explicitly calling serializer by myself:

	j["PREVIEW"] = {
		{"GAIN", gain()},
		{"EXPOSURE", exposure()}
	};
       to_json(j["PREVIEW"]["IMAGE"], image) ;

Why first version does not?
Additionally, while pushing to array, json dump() (or serializing to stream in general) puts single value per line, making file unreadable. Is there possibility to set number of values per line?
And can I serialize in such a way 3Mpx image? yes, finally I will put some base64 encoding yet still I need to serialize full image from time to time

@nlohmann
Copy link
Owner

I did not find the time to check the first issue so far. But since the parameter is a const reference, there should not be any change. (Maybe @theodelrieu has an idea)

On the second issue: the dump() function either produces a minimal serialization with minimal whitespace, or you can pass an integer to control the indentation depth.

@theodelrieu
Copy link
Contributor

What makes the code throw exactly? Could you post the code leading to to_json being called?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label May 3, 2018
@nlohmann
Copy link
Owner

@ArkadiuszRaj Could you provide more information?

@ArkadiuszRaj
Copy link
Author

Hello, exactly as stated in first example.
Including image (OpenCV's Mat object) directly when constructing json fails.

        json j;	
        Mat image = Mat::zeros(5, 5, CV_32F);
	j["PREVIEW"] = {
		{"GAIN", 1},
		{"EXPOSURE", 1},
		{"IMAGE", image},
	};

But...
Explicitly calling to_json() works as intended, i.e.

        j["PREVIEW"] = {
		{"GAIN", 1},
		{"EXPOSURE", 1}
	};
       to_json(j["PREVIEW"]["IMAGE"], image) ;

What additional info do you need?

@theodelrieu
Copy link
Contributor

theodelrieu commented May 23, 2018

Ok I got it, Mat is in the cv namespace, so your to_json function is not found via ADL.

Please take a look at the readme section about third party types, you have to specialize adl_serializer to make it work.

@ArkadiuszRaj
Copy link
Author

That is true. I have populated other types in ADL but not Mat.
Will do that shortly and it will help this means it was my mistake - I will close this issue.

@ArkadiuszRaj
Copy link
Author

Thanks!

@nlohmann nlohmann added solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed state: needs more info the author of the issue needs to provide more details labels May 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants