作者hope2801 (zzzzz)
看板C_and_CPP
标题[问题]
时间Wed Sep 29 16:36:02 2021
开发平台(Platform) Win10
编译器 VC++ 2019+目标环境(跟开发平台不同的话需列出)
额外使用到的函数库(Library Used): (Ex: OpenCV 4.5.3 )
问题(Question)
没有合适的建构函式存在,无法从"char"转换为”cv::Mat”
喂入的资料(Input):
预期的正确结果:
show出 raw档的图
错误结果(Wrong Output):
没有合适的建构函式存在,无法从"char"转换为”cv::Mat”
程式码(Code):
#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <fstream>
using namespace cv;
using std::ofstream;
using std::ifstream;
using std::fstream;
using std::ios;
int main() {
fstream rawlena;
rawlena.open("lena_256.raw", ios::binary);
rawlena.seekg(0, ios::end);
int length = rawlena.tellg();
rawlena.seekg(0, ios::beg);
char* buffer = new char[length];
Mat rawlenaimage = (256,256, CV_8UC1, buffer);
imshow("photo", rawlenaimage);
waitKey(5000);
}
补充说明(Supplement):
我上网爬了许多文看到大家都用类似的方法,可是我找了一整天还是找不到我的问题在
哪里,只好拜托大家帮忙了
----
Sent from
BePTT on my Xiaomi M2102J20SG
--
int main()
{
FILE* input_file;
unsigned char* img_lena = new unsigned char[256*256];
fread(img_lena, 1, 256*256, input_file);
Mat mat_lena(256, 256, CV_8UC1, img_lena);
imshow("lena in opencv", mat_lena);
}
我後来改用这样写,然後成功了。
还有其实是不能用opencv的,只是因为我真的不知道要怎麽不用opencv才能把照片show出来,我网路上怎麽找也找不到相关的写法,抱歉我有点烂...
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 27.240.170.9 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1632904564.A.A8E.html
1F:推 nh60211as: Mat rawImage = Mat(...) 才对09/29 16:45
2F:→ hope2801: 收到09/29 16:49
3F:→ hope2801: 非常谢谢您的帮忙,帮忙我解决了一部份的问题09/29 17:18
4F:→ hope2801: 但是我现在遇到了我没看过的问题09/29 17:18
5F:→ hope2801: 0x00007FFCEAA14F99 中 (於 showphoto.exe) 发生未处理09/29 17:19
6F:→ hope2801: 的例外状况: Microsoft C++ 发生例外状况: std::bad_a09/29 17:19
7F:→ hope2801: rray_new_length (记忆体位置 0x000000982CF5F7F0)。09/29 17:19
8F:→ hope2801: 这是在throw_bad_alloc.cpp档里09/29 17:20
9F:→ hope2801: #else // ^^^ Managed ^^^ // vvv Native vvv //09/29 17:20
10F:→ hope2801: __declspec(noreturn) void __CRTDECL __scrt_thr09/29 17:20
11F:→ hope2801: ow_std_bad_alloc()09/29 17:20
12F:→ hope2801: {09/29 17:20
13F:→ hope2801: throw std::bad_alloc{};09/29 17:20
14F:→ hope2801: }09/29 17:20
15F:→ hope2801: __declspec(noreturn) void __CRTDECL __scrt_thr09/29 17:20
16F:→ hope2801: ow_std_bad_array_new_length()09/29 17:20
17F:→ hope2801: {09/29 17:20
18F:→ hope2801: throw std::bad_array_new_length{};09/29 17:20
19F:→ hope2801: }09/29 17:20
20F:→ hope2801: #endif09/29 17:20
22F:→ hope2801: 请问一下这个bug是什麽问题?09/29 17:22
23F:→ hope2801: char* buffer = new char[length];09/29 17:36
24F:→ hope2801: 我是此行程式有问题,可是我看不出来我错在哪里09/29 17:36
25F:→ nh60211as: 你tellg()回传值是啥?感觉你这程式码还会有很多问题09/29 17:57
26F:→ hope2801: 我的tellg()回传25970-1给length09/29 18:08
27F:→ hope2801: 我的raw图是一个大小为256x256的深度为8的灰阶图09/29 18:08
29F:→ nh60211as: 这什麽数字啊,我看不太懂09/29 18:29
30F:→ F04E: 只不过是show个raw档需要动用到OpenCV??这年头作业真好写09/29 20:18
31F:→ nh60211as: 我知道25970是什麽了,那是'er'多字元字元的数值09/29 20:24
32F:→ nh60211as: tellg()回传的是-1,代表你读档根本就失败了09/29 20:28
※ 编辑: hope2801 (27.246.37.129 台湾), 10/04/2021 21:41:28