cv :: imdecode如何知道结果的尺寸?

时间:2019-11-01 17:10:22

标签: python c++ opencv

我正在尝试将以下函数转换为C ++:

def base64_2_mask(s):
    z = zlib.decompress(base64.b64decode(s))
    n = np.fromstring(z, np.uint8)
    mask = cv2.imdecode(n, cv2.IMREAD_UNCHANGED)[:, :, 3].astype(bool)
    return mask

它将JSON字符串解码为cv::Mat掩码。

我做得很好,除了那部分

const cv::Mat bitmap( size, CV_8UC1, decompressed.data() );
const auto decoded = cv::imdecode( bitmap, cv::IMREAD_UNCHANGED );

我不知道要在这里传递什么size。 python版本会自动推断出它。这是一个示例运行:

>>> a = base64_2_mask("eJwBhwV4+olQTkcNChoKAAAADUlIRFIAAAEmAAABqwEDAAAAVySonwAAAAZQTFRFAAAA////pdmf3QAAAAF0Uk5TAEDm2GYAAAUvSURBVHic7dtNrts2EABgCVooiwK8Qdgj9Aa8Uk9QEciix8hRnnZZ9gjhLssS6EZBDSv6scgZkUMOJcUvaR8XD0/yZ3tmSFEyZVfV2roq25q+GjmqZqjW1HeOaliqvXGUGBjKSstTkqGU4ajuU75gjR0/55Nsvo4v+SSbcWo8pa97rXxHzoqR46QsSxmW6lkqi2bFGKsNJ8VZWZYyLMWIq2blWHOGBFNVHSeuSTGO2kqtlfglp/R0vGXVHFZrcmoOSwzvM8pOf+T9D4ZSuXIs76hy3VSm9AWqeyjzXCVzx8isfmUpK3Ojf1LNTebmlEnJO0uNH3Oz2KL+mlRy+E9KjH9nVD3OaszMwbNqC1SqYPU0yTUlKhH/rOqHokdiPUXNU9WmepYytBrn2SmjGq5q52GfVX9+yUY/q/E69YmlvrCUb6Z6x1C2Js6VPNVCNb4QQ/GIos7h/ylFHJKHlL1QXRkXT/HqRQx9nmq+ozI/laqfqH5jqd8Z6n1lGUrWjwHXYdVj1dw4qo0rHLsQHCU/RtXuQkC9RNXu6FAjR3XbDoXUcESNcWURaliqdS+OlUFKOCUTSrkdWPW7FLfLlYSaj5yo0rgOrl+x2mXodghSKVBlpOCQaGD9k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1kx1mdqdjk4pzVH7q8MzSl+ngovWmAqvWY+r/joVuZSOKHtUVaGKoFBFr/APqrAObKU4wR9UhqX0CSUY1QpUPCymwnFR61ZHFPGGhxSBsCIXwA4oy1L9dYpeMSxX9EogVOZCpZ+uSPS6KrHw+51U4l4YUInF7VdVhqX6C5W+UNEIqNS9AJ4SnKIWK/t0ZRLKV6I/q8AVfeKleKq7ToHLr0QHgbDshcrQyvdPKsVSlbpFz1OSU6435Zp4uvLjK9WPPFWzlP8QkLzTz1PdD6qUC5+n9IXKsJT9QVVqsHqVGmCvqZ50HnpTb+p/qcZS1Z9U9YWKXKY+oARQhlQSKHtSKaDISRqtT55U0ZswBxV1Lw237jqFv+9gCaV+LiX0aWXc7oGjuvEyVe/SFVEF7jEXqJZUQ7Gqo2p5bg9YF1NLtLdiFb07LJctXarqmOrQ1txURK1bQ7ESoXrMCrcr1Joiut6XodpC1WdUD1NE/a0C5brDnFdujy1VXaDc5nBGWRQ8KH547pArECO4JkDqxlJ3t8MsqepN4S9s6UdYtzULQvUsZR45L30jfHdjZQmFp5Nhfdb9kZiOq9tah+3bPYS6E0ogNYKwYNspzVLrF2x1Ro0+RVL9SykJlV23bEZVHDWs1esD1SEl4ykeUiqe4k4tW5ErFJ6CaPxKFOKIWps5qGI33TWpBpBr+IZlyiiHIqsB20GbXssoUncwzsJCMFW7ReyVJpUFM0GIypTx81hsraxIaa9iCzFiU25wRApRpqr0TySOKh1RymWv6BSL1PyQXFX0A0qJGryyMdW5Sgq6EEVqfsj1+0k1P9TQ5Xqoyqn45zme8g/VdLlKlPVvbmjVL/8qshAF6nEEyi3bsHU+e0EWtUCtYS0dSXyU5inlI26o0cVVAkxFZCHmYMC3TO0pVYMObumVLdYPSplKcn5QylRtnzffACeoxvRkOzXGAAAAAElFTkSuQmCCZ4Wa4g=="
>>> z = zlib.decompress(base64.b64decode(b))
>>> n = np.fromstring(z, np.uint8)
__main__:1: DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead
>>> n
array([137,  80,  78, ...,  66,  96, 130], dtype=uint8)
>>> n.shape
(1415,)
>>> mask = cv2.imdecode(n, cv2.IMREAD_UNCHANGED)[:, :, 3].astype(bool)
>>> mask.shape
(427, 294)
>>> mask2 = cv2.imdecode(n, cv2.IMREAD_UNCHANGED)
>>> mask2.shape
(427, 294, 4)

Here是我的代码。

0 个答案:

没有答案