OpenCV 错误:图像步长错误(矩阵不连续)
Posted
技术标签:
【中文标题】OpenCV 错误:图像步长错误(矩阵不连续)【英文标题】:OpenCV error: Image step is wrong (The matrix is not continuous) 【发布时间】:2014-02-21 14:55:04 【问题描述】:通过命令行启动我的程序时,遇到这样的问题: OpenCV 错误:图像步骤错误(矩阵不连续,因此无法更改其行数) un cv::Mat::reshape,文件 C:\builds\2_4_PackSlave-win64-vc12-shared\opencv\modules \core\src\matrix.cpp,第 802 行。
程序代码:
#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace cv;
using namespace std;
static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';')
std::ifstream file(filename.c_str(), ifstream::in);
if (!file)
string error_message = "No valid input file was given, please check the given filename.";
CV_Error(CV_StsBadArg, error_message);
string line, path, classlabel;
while (getline(file, line))
stringstream liness(line);
getline(liness, path, separator);
getline(liness, classlabel);
if (!path.empty() && !classlabel.empty())
images.push_back(imread(path, 0));
labels.push_back(atoi(classlabel.c_str()));
int main(int argc, const char *argv[])
// Check for valid command line arguments, print usage
// if no arguments were given.
if (argc != 4)
cout << "usage: " << argv[0] << " </path/to/haar_cascade> </path/to/csv.ext> </path/to/device id>" << endl;
cout << "\t </path/to/haar_cascade> -- Path to the Haar Cascade for face detection." << endl;
cout << "\t </path/to/csv.ext> -- Path to the CSV file with the face database." << endl;
cout << "\t <device id> -- The webcam device id to grab frames from." << endl;
exit(1);
// Get the path to your CSV:
string fn_haar = string(argv[1]);
string fn_csv = string(argv[2]);
int deviceId = atoi(argv[3]);
// These vectors hold the images and corresponding labels:
vector<Mat> images;
vector<int> labels;
// Read in the data (fails if no valid input filename is given, but you'll get an error message):
try
read_csv(fn_csv, images, labels);
catch (cv::Exception& e)
cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
// nothing more we can do
exit(1);
// Get the height from the first image. We'll need this
// later in code to reshape the images to their original
// size AND we need to reshape incoming faces to this size:
int im_width = images[0].cols;
int im_height = images[0].rows;
// Create a FaceRecognizer and train it on the given images:
Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
model->train(images, labels);
// That's it for learning the Face Recognition model. You now
// need to create the classifier for the task of Face Detection.
// We are going to use the haar cascade you have specified in the
// command line arguments:
//
CascadeClassifier haar_cascade;
haar_cascade.load(fn_haar);
// Get a handle to the Video device:
VideoCapture cap(deviceId);
// Check if we can use this device at all:
if (!cap.isOpened())
cerr << "Capture Device ID " << deviceId << "cannot be opened." << endl;
return -1;
// Holds the current frame from the Video device:
Mat frame;
for (;;)
cap >> frame;
// Clone the current frame:
Mat original = frame.clone();
// Convert the current frame to grayscale:
Mat gray;
cvtColor(original, gray, CV_BGR2GRAY);
// Find the faces in the frame:
vector< Rect_<int> > faces;
haar_cascade.detectMultiScale(gray, faces);
// At this point you have the position of the faces in
// faces. Now we'll get the faces, make a prediction and
// annotate it in the video. Cool or what?
for (int i = 0; i < faces.size(); i++)
// Process face by face:
Rect face_i = faces[i];
// Crop the face from the image. So simple with OpenCV C++:
Mat face = gray(face_i);
// Resizing the face is necessary for Eigenfaces and Fisherfaces. You can easily
// verify this, by reading through the face recognition tutorial coming with OpenCV.
// Resizing IS NOT NEEDED for Local Binary Patterns Histograms, so preparing the
// input data really depends on the algorithm used.
//
// I strongly encourage you to play around with the algorithms. See which work best
// in your scenario, LBPH should always be a contender for robust face recognition.
//
// Since I am showing the Fisherfaces algorithm here, I also show how to resize the
// face you have just found:
Mat face_resized;
cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);
// Now perform the prediction, see how easy that is:
int prediction = model->predict(face_resized);
// And finally write all we've found out to the original image!
// First of all draw a green rectangle around the detected face:
rectangle(original, face_i, CV_RGB(0, 255, 0), 1);
// Create the text we will annotate the box with:
string box_text = format("Prediction = %d", prediction);
// Calculate the position for annotated text (make sure we don't
// put illegal values in there):
int pos_x = std::max(face_i.tl().x - 10, 0);
int pos_y = std::max(face_i.tl().y - 10, 0);
// And now put it into the image:
putText(original, box_text, Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0, 255, 0), 2.0);
// Show the result:
imshow("face_recognizer", original);
// And display it:
char key = (char)waitKey(20);
// Exit this loop on escape:
if (key == 27)
break;
return 0;
我必须做什么?
【问题讨论】:
当您使用cv::Mat::colRange()
或cv::Mat::RowRange()
然后调用使用缓冲区对图像元素进行迭代的函数时,通常会发生此错误。你知道你的代码哪里出错了吗?
我想 - 在这里? int im_width = 图像[0].cols; int im_height = images[0].rows;
我不这么认为,尝试使用逐步调试器找出来。
这样的错误,调试时:opencv_flann248.dll opencv_features2d248.dll opencv_calib3d248.dll opencv_ml248.dll opencv_video248.dll opencv_core248.dll opencv_imgproc248.dll opencv_highgui248.dll opencv_objdetect248.dll opencv_contrib248.dll : 不能PDB 文件。
【参考方案1】:
FisherFaceRecognizer(也是 Eigen)尝试将图像“扁平化”为单行 (reshape()) 以进行训练和测试。
如果 Mat 是不连续的(因为它要么是填充的,要么只是一个 submat/roi),这不起作用。
(再一次,'fileNotFound' 也算作'非连续';])
如果您的图片是.bmp ,很有可能某些图像编辑器会填充您的图像,因此行大小是 4 的因子。
也许您可以在外部将您的 imgs 批量转换为 .png 或 .pgm
在加载火车图像后调整它们的大小会有所帮助(任何复制它的东西)
或者,在加载代码中更改这一行:
images.push_back(imread(path, 0));
到:
Mat m = imread(path, 1);
Mat m2;
cvtColor(m,m2,CV_BGR_GRAY);
images.push_back(m2);
【讨论】:
( then again, 'fileNotFound' counts as 'non-continuous', too ;] )
帮助我找到了我的问题。
我也遇到了与@SilverSurfer 相同的问题。未找到文件,FisherFaceRecognizer 抛出此异常。【参考方案2】:
这是您的 csv 文件中的斜线/反斜线问题。 比如我的是这样的:
sujets\s1/1.pgm;0
sujets\s1/10.pgm;0
...
sujets\s1/9.pgm;0
sujets\s2/1.pgm;1
sujets\s2/10.pgm;1
...
sujets\s2/9.pgm;1
sujets\s3/1.pgm;2
sujets\s3/10.pgm;2
...
sujets\s3/9.pgm;2
sujets\s4/1.pgm;3
sujets\s4/10.pgm;3
...
sujets\s4/9.pgm;3
为此改变:
sujets/s1/1.pgm;0
sujets/s1/10.pgm;0
...
sujets/s1/9.pgm;0
sujets/s2/1.pgm;1
sujets/s2/10.pgm;1
...
sujets/s2/9.pgm;1
sujets/s3/1.pgm;2
sujets/s3/10.pgm;2
...
sujets/s3/9.pgm;2
sujets/s4/1.pgm;3
sujets/s4/10.pgm;3
...
sujets/s4/9.pgm;3
做了什么
【讨论】:
【参考方案3】:我认为问题可能出在Mat face = gray(face_i)
。
请阅读 cmets 进行说明。
Rect face_i = faces[i];
// This operation makes a new header for the specified sub-array of
// *this, thus it is a 0(1) operation, that is, no matrix data is
// copied. So matrix elements are no longer stored continuously without
// gaps at the end of each row.
Mat face = gray(face_i);
...
Mat face_resized;
// Here new memory for face_resized should be allocated, but I'm not sure.
// If not then it is the reason of the error, because in this case
// face_resized will contain not continuous data (=face).
cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);
...
// here reshape(face_resized) will be called and will throw the error if
// matrix is not continuous
int prediction = model->predict(face_resized);
【讨论】:
以上是关于OpenCV 错误:图像步长错误(矩阵不连续)的主要内容,如果未能解决你的问题,请参考以下文章
youcans 的 OpenCV 例程200篇149. 图像分割之边缘模型
youcans 的 OpenCV 例程200篇149. 图像分割之边缘模型