こんにちは。
どうしても解決できず、ずっと悩んでいるのでご助力いただけないかと思い投稿しました。
###前提・実現したいこと
XcodeでC++、opencvを用いて行列操作をしています。
ある関数内で、メイン関数から受け取ったMat型in_img(CV_64F)を、Mat型tmp(CV_8UC1)に正規化したいです。
Mat型in_imgには例えば[700,400,800,550]のような行列で、0〜255の範囲外の値があります。
これを0〜255の範囲に直し、Mat型tmpに出力しようとしています。
が、うまくいきません…。
どのように直せば正規化が行えるでしょうか?
###発生している問題・エラーメッセージ
下記のソースコードで動かすと、cvConvertScaleの部分でsingl SIGABRTになり止まってしまいます。
デバッグ画面には
OpenCV Error: Bad argument (Unknown array type) in cvarrToMat
が出力されます。
###ソースコード
C++
1void convert(const int a, Mat& in_img, Mat& out_img, int mode){ 2 cout << "isContinuous:" << (in_img.isContinuous()?"true":"false") << endl; 3 cout << "channels: " << in_img.channels() << endl; // チャンネル数を表示 4 cout << "depth (ID):" << in_img.depth() << "(=" << CV_64F << ")" << endl; 5 cout << in_img.rows << " " << in_img.cols << endl; 6 double minVal, maxVal; 7 Point minLoc, maxLoc; 8 minMaxLoc(in_img, &minVal, &maxVal, &minLoc, &maxLoc); 9Mat tmp; 10 cvConvertScale(&in_img, &tmp, 255.0/(maxVal-minVal), (255.0/(maxVal-minVal))*(-minVal)); 11 cout << "tmp: " << tmp; 12}
###補足情報(言語/FW/ツール等のバージョンなど)
デバッグ画面出力内容
isContinuous:true
channels: 1
depth (ID):6(=6)
1 3264
OpenCV Error: Bad argument (Unknown array type) in cvarrToMat
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答2件
0
ベストアンサー
まだ見てるのかな、いいや、暇つぶしに調べただけなので。
cv::Mat::convertToでいい気がするのですが・・・。
ex.)http://docs.opencv.org/3.1.0/de/dc7/fitellipse_8cpp-example.html#a14&gsc.tab=0
cpp
1#include <opencv2/core/core.hpp> 2#include <iostream> 3#include <vector> 4cv::Mat rescale_to_8bit(const cv::Mat& in) { 5 double minVal, maxVal; 6 cv::minMaxLoc(in, &minVal, &maxVal); 7 cv::Mat re; 8 in.convertTo(re, CV_8U, 255.0 / (maxVal - minVal), (255.0 / (maxVal - minVal))*(-minVal)); 9 return re; 10} 11int main(int argc, const char* argv[]) 12{ 13 std::vector<int> src_v = { 700,400,800,550 }; 14 using std::endl; 15 auto src_m = cv::Mat(src_v); 16 std::cout << src_m << std::endl; 17 std::cout << rescale_to_8bit(src_m) << std::endl; 18 return 0; 19} 20
[700; 400; 800; 550] [191; 0; 255; 96]
検証環境はVisual Studio 2015 Update 1 OpenCV 3.1 自前ビルド
General configuration for OpenCV 3.1.0 ===================================== Version control: unknown Platform: Host: Windows 6.1.7601 AMD64 CMake: 3.4.1 CMake generator: Visual Studio 14 2015 CMake build tool: C:/Program Files (x86)/MSBuild/14.0/bin/MSBuild .exe MSVC: 1900 C/C++: Built as dynamic libs?: YES C++ Compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe (ver 19.0.23506.0) C++ flags (Release): /DWIN32 /D_WINDOWS /W4 /GR /EHa /D _CRT_SECURE _NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigob j /arch:SSE2 /Oi /fp:fast /wd4251 /wd4324 /wd4275 /wd4589 /MP4 /MD /O2 /Ob2 /D NDEBUG /Zi C++ flags (Debug): /DWIN32 /D_WINDOWS /W4 /GR /EHa /D _CRT_SECURE _NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigob j /arch:SSE2 /Oi /fp:fast /wd4251 /wd4324 /wd4275 /wd4589 /MP4 /D_DEBUG /MDd / Zi /Ob0 /Od /RTC1 C Compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe C flags (Release): /DWIN32 /D_WINDOWS /W3 /D _CRT_SECURE_NO_DEPRE CATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /arch:S SE2 /Oi /fp:fast /MP4 /MD /O2 /Ob2 /D NDEBUG /Zi C flags (Debug): /DWIN32 /D_WINDOWS /W3 /D _CRT_SECURE_NO_DEPRE CATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /arch:S SE2 /Oi /fp:fast /MP4 /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1 Linker flags (Release): /machine:X86 /INCREMENTAL:NO /debug Linker flags (Debug): /machine:X86 /debug /INCREMENTAL Precompiled headers: YES Extra dependencies: comctl32 gdi32 ole32 setupapi ws2_32 vfw32 3rdparty dependencies: zlib libjpeg libwebp libpng libtiff libjasper I lmImf OpenCV modules: To be built: core flann imgproc ml photo video imgcodecs sha pe videoio highgui objdetect superres ts features2d calib3d stitching videostab python3 Disabled: python2 world Disabled by dependency: - Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarp ing cudev java viz Windows RT support: NO GUI: QT: NO Win32 UI: YES OpenGL support: NO VTK support: NO Media I/O: ZLib: build (ver 1.2.8) JPEG: build (ver 90) WEBP: build (ver 0.3.1) PNG: build (ver 1.6.19) TIFF: build (ver 42 - 4.0.2) JPEG 2000: build (ver 1.900.1) OpenEXR: build (ver 1.7.1) GDAL: NO Video I/O: Video for Windows: YES DC1394 1.x: NO DC1394 2.x: NO FFMPEG: YES (prebuilt binaries) codec: YES (ver 56.41.100) format: YES (ver 56.36.101) util: YES (ver 54.27.100) swscale: YES (ver 3.1.101) resample: NO gentoo-style: YES GStreamer: NO OpenNI: NO OpenNI PrimeSensor Modules: NO OpenNI2: NO PvAPI: NO GigEVisionSDK: NO DirectShow: YES Media Foundation: NO XIMEA: NO Intel PerC: NO Parallel framework: TBB (ver 4.4 interface 9002) Other third-party libraries: Use IPP: 9.0.1 [9.0.1] at: C:/lib/opencv-3.1.0/3rdparty/ippicv/unpack/ippi cv_win Use IPP Async: NO Use Eigen: YES (ver 3.2.7) Use Cuda: NO Use OpenCL: YES Use custom HAL: NO OpenCL: Version: dynamic Include path: C:/lib/opencv-3.1.0/3rdparty/include/opencl/1.2 Use AMDFFT: NO Use AMDBLAS: NO Python 2: Interpreter: C:/Python34/python.exe (ver 3.4.4) Python 3: Interpreter: C:/Python34/python.exe (ver 3.4.4) Libraries: C:/Python34/libs/python34.lib (ver 3.4.4) numpy: C:/Python34/lib/site-packages/numpy/core/includ e (ver 1.10.2) packages path: C:/Python34/Lib/site-packages Python (for build): C:/Python34/python.exe Java: ant: NO JNI: NO Java wrappers: NO Java tests: NO Matlab: Matlab not found or implicitly disabled Documentation: Doxygen: D:/Program1/doxygen-1.8.10.windows.x64.bin/doxy gen.exe (ver 1.8.10) PlantUML: NO Tests and samples: Tests: YES Performance tests: YES C/C++ Examples: NO Install path: C:/lib/opencv-3.1.0/build/install cvconfig.h is in: C:/lib/opencv-3.1.0/build -----------------------------------------------------------------
ちなみにcvConvertScaleがうまく行かなかったのは、3.xで廃止されたC-stype APIの関数だからcv::Matへのポインタを渡されることが想定外だったのでしょう。
投稿2016/01/21 16:41
総合スコア5850
0
こんにちは。
cvConvertScale()の第一引数は行列へのポインタです。
ご提示されたソースでは「行列のポインタへのポインタ」になっているようです。
【ところで】
OpenCV 2.4.9で確認したのですが、cvConvertScale()の第一引数はconst CvArr* src
です。
そして、typedef void CvArr;
と定義してあります。つまり、ポインタなら何を書いてもコンパイル・エラーにならないようです。
投稿2016/01/05 06:29
編集2016/01/05 06:45総合スコア23272
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/05 07:24
2016/01/05 08:03
2016/01/05 08:45
2016/01/07 08:36
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/23 10:35