我绝对是c ++和opencv库的新手.我正在浏览stackoverflow并搜索网页,但找不到我的问题的答案.
如何在c ++中将浮点数组保存到图像中?目前(作为练习)我只是手动将图像转换为灰度(虽然我读到这可以用opencv完成).
后来我打算做一些过滤操作和其他像素操作.这就是我想使用浮点数组(大小为512*512)的原因.
这是代码.我所知道的是,float数组必须转换为int或char或uint8或cv :: Mat,以便它可以保存为.png,但我不知道如何.
任何提示或链接都非常感谢.
#include <stdio.h>
#include <opencv2/highgui/highgui.hpp>
int main(void)
{
// load in image using opencv and convert to char
cv::Mat myImg = cv::imread("~/Lenna.png", CV_LOAD_IMAGE_COLOR);
unsigned char *img = (unsigned char*)(myImg.data); //somehow only works for unsigned char and not for float (SegFault)
float *r = new float[512*512];
float *g = new float[512*512];
float *b = new float[512*512];
float *gray = new float[512*512];
// 3*iCol, bc every pixel takes 3 bytes (one for R channel/B …Run Code Online (Sandbox Code Playgroud) 我想将浮点数保存为图像文件中的像素。我目前正在使用 OpenCV-python,但我也尝试过使用 Pillow (PIL)。这两个包在将float像素数据写入文件之前都将其转换为整数。
我想保存像素值,例如:
(245.7865, 123.18788, 98.9866)
Run Code Online (Sandbox Code Playgroud)
但是当我读回图像文件时,我得到:
(246, 123, 99)
Run Code Online (Sandbox Code Playgroud)
不知何故,我的浮点数被四舍五入并转换为整数。如何阻止 PIL 或 OpenCV 将它们转换为整数?