我是openCV的新手。
我剛剛從Github下載了openCV的源代碼,並將它安裝在桌面上(Mac OS)。
如何在MacOS上使用OpenCV連接C++程序
現在我需要將二進制文件轉換爲圖像文件。下面是代碼:
// The program is first to convert the image to the binary file then convert the binary file back to the image
#include <iostream>
#include </Users/Me/Desktop/opencv-master/include/opencv2/opencv.hpp>
#include </Users/Me/Desktop/opencv-master/modules/highgui/include/opencv2/highgui.hpp>
const std::string filename = "test.dat";
const std::string picname = "pano_b.jpg";
int main(int argc, char **argv)
{
std::ofstream outfile;
outfile.open(filename.c_str(), std::ios::binary);
if (!outfile)
{
std::cerr << "failed to open the file : " << filename << std::endl;
return -1;
}
cv::Mat srcImg = cv::imread(picname);
if (srcImg.empty())
{
std::cerr << "failed to open the file : " << picname << std::endl;
return -1;
}
for (int r = 0; r < srcImg.rows; r++)
outfile.write(reinterpret_cast<const char*>(srcImg.ptr(r)), srcImg.cols*srcImg.elemSize());
std::cout << "write to file ok!" << std::endl;
///// Here the file test.dat is generated. It looks like this:
///// 0069 a800 6caa 0074 af0c 80b9 2096 ca2a
///// 9fd2 1c8d bf03 71a1 0773 a300 6295 0064
///// 9907 73a9 0070 a900 6da8 0480 bc18 95d2
///// ...
//////// Now I want to convert test.dat into an image file
cv::Mat img = cv::imread(filename.c_str(), cv::IMREAD_ANYCOLOR);
cv::imshow("img", img);
cv::waitKey();
return 0;
}
不過,我得到了一個錯誤如下:
Undefined symbols for architecture x86_64: "cv::imshow(cv::String const&, cv::_InputArray const&)", referenced from: _main in bbb-bb77f0.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
正如我所說的,我在桌面上安裝OpenCV的,這是我如何編譯:
g++ test.cpp /Users/Me/Desktop/opencv-master/build/lib/libopencv_core.dylib /Users/Me/Desktop/opencv-master/build/lib/libopencv_imgproc.dylib /Users/Me/Desktop/opencv-master/build/lib/libopencv_imgcodecs.dylib
我應該怎麼做才能解決這個鏈接錯誤?
要使用imread,您需要文件中的圖像標題。如果您想手動讀取二進制文件並從中構建圖像,則必須知道原始數據的寬度,高度和像素類型。 – Micka
@Micka你能舉個例子嗎? – Yves