看板 C_and_CPP 關於我們 聯絡資訊
開發平台(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://www.ptt.cc/bbs/C_and_CPP/M.1632904564.A.A8E.html
nh60211as: Mat rawImage = Mat(...) 才對09/29 16:45
hope2801: 收到09/29 16:49
hope2801: 非常謝謝您的幫忙,幫忙我解決了一部份的問題09/29 17:18
hope2801: 但是我現在遇到了我沒看過的問題09/29 17:18
hope2801: 0x00007FFCEAA14F99 中 (於 showphoto.exe) 發生未處理09/29 17:19
hope2801: 的例外狀況: Microsoft C++ 發生例外狀況: std::bad_a09/29 17:19
hope2801: rray_new_length (記憶體位置 0x000000982CF5F7F0)。09/29 17:19
hope2801: 這是在throw_bad_alloc.cpp檔裡09/29 17:20
hope2801: #else // ^^^ Managed ^^^ // vvv Native vvv //09/29 17:20
hope2801: __declspec(noreturn) void __CRTDECL __scrt_thr09/29 17:20
hope2801: ow_std_bad_alloc()09/29 17:20
hope2801: {09/29 17:20
hope2801: throw std::bad_alloc{};09/29 17:20
hope2801: }09/29 17:20
hope2801: __declspec(noreturn) void __CRTDECL __scrt_thr09/29 17:20
hope2801: ow_std_bad_array_new_length()09/29 17:20
hope2801: {09/29 17:20
hope2801: throw std::bad_array_new_length{};09/29 17:20
hope2801: }09/29 17:20
hope2801: #endif09/29 17:20
hope2801: https://i.imgur.com/bFNOkC0.jpg09/29 17:21
hope2801: 請問一下這個bug是什麼問題?09/29 17:22
hope2801: char* buffer = new char[length];09/29 17:36
hope2801: 我是此行程式有問題,可是我看不出來我錯在哪裡09/29 17:36
nh60211as: 你tellg()回傳值是啥?感覺你這程式碼還會有很多問題09/29 17:57
hope2801: 我的tellg()回傳25970-1給length09/29 18:08
hope2801: 我的raw圖是一個大小為256x256的深度為8的灰階圖09/29 18:08
hope2801: https://i.imgur.com/itcDqee.jpg09/29 18:10
nh60211as: 這什麼數字啊,我看不太懂09/29 18:29
F04E: 只不過是show個raw檔需要動用到OpenCV??這年頭作業真好寫09/29 20:18
nh60211as: 我知道25970是什麼了,那是'er'多字元字元的數值09/29 20:24
nh60211as: tellg()回傳的是-1,代表你讀檔根本就失敗了09/29 20:28
※ 編輯: hope2801 (27.246.37.129 臺灣), 10/04/2021 21:41:28