作者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/m.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