OpenCVMatデータ型ポインターptrの使用



Use Opencv Mat Data Type Pointer Ptr



順番: https://blog.csdn.net/github_35160620/article/details/51708659

OpenCVMatデータ型ポインターptrの使用



Cv::Mat image = cv::Mat(400, 600, CV_8UC1) //width 400, length 600 uchar * data00 = image.ptr(0) uchar * data10 = image.ptr(1) uchar * data01 = image.ptr(0)[1]

説明:

マット変数イメージが定義されています。
data00は、画像の最初の行の最初の要素へのポインタです。
data10は、画像の2行目の最初の要素へのポインタです。
data01は、画像の最初の行の2番目の要素へのポインタです。



Note: If your program uses the image.ptr pointer, and the following error occurs: (assuming you are using Visual Studio 201x) 1 There is an unhandled exception at 0x75065b68 in the .exe: Microsoft C++ exception cv::Exception at memory location 0x85e790. This may be because you don't understand the image.ptr pointer and made the mistake: image.ptr(1) refers not to the second pixel in the image, but to the second pixel of the first row. Use the code above for an example: image has 400 lines and has 400*600 pixels. Suppose now that you want to get the 42nd pixel of the 3rd line, if you write: uchar * data = image.ptr(3*image.cols + 41) This is wrong to write, the above error will occur. What you get is not the 42nd pixel of the 3rd line, but the 0th pixel of the (3×image.cols + 41) line, because there is no (3×image.cols + 41) line, so there is no such Pointer, so wrong. Correct way of writing: uchar * data = image.ptr(3)[41] So pay attention to this: if the program can compile normally, but the runtime error, it is very likely that when you assign a value to the pointer, the index value overflows the specified range, the pointer is pointed, causing the program to run off, so it can only be found at runtime. error. Cv::Mat image = cv::Mat(400, 600, CV_8UC3) //width 400, length 600, 3 channel color picture uchar * data000 = image.ptr(0) uchar * data100 = image.ptr(1) uchar * data001 = image.ptr(0)[1] uchar * data Cv::Mat image = cv::Mat(400, 600, CV_8UC3) //width 400, length 600, 3 channel color picture cv::Vec3b * data000 = image.ptr(0) cv::Vec3b * data100 = image.ptr(1) cv::Vec3b * data001 = image.ptr(0)[1] cv::Vec3b * data