为 OpenCV fisherface 识别器调整图像大小
Resizing Images for OpenCV fisherface recognizer
我正在使用 OpenCV Contrib 框架来执行面部识别。我的问题是,当隔离面孔(使用 for 循环)时,OpenCV 会裁剪测试图像以仅显示面孔(如面孔周围的 40x40 框)。我需要将此图像调整为 3000x4000(我的训练图像就是这个尺寸)。 我的问题是如何将图像(大部分时间为 40x40)调整为 3000x4000。
以下是我尝试过的几个调整大小的函数及其相应的错误信息。
1.
(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
错误信息...
OpenCV(3.4.0-dev) Error: Bad argument (Wrong shapes for given matrices. Was size(src) = (1,48000000), size(W) = (12000000,1).) in subspaceProject, file /Users/mustafa/Desktop/OpenCVBuild/opencv/modules/core/src/lda.cpp, line 183
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: OpenCV(3.4.0-dev) /Users/mustafa/Desktop/OpenCVBuild/opencv/modules/core/src/lda.cpp:183: error: (-5) Wrong shapes for given matrices. Was size(src) = (1,48000000), size(W) = (12000000,1). in function subspaceProject
(lldb)
我在 predict
函数(发生崩溃的地方)之前设置了一个断点,我传递给函数的图像似乎是 3000x4000。
screenshot
2.
(UIImage *)squareImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
double ratio;
double delta;
CGPoint offset;
//make a new square size, that is the resized imaged width
CGSize sz = CGSizeMake(newSize.width, newSize.width);
//figure out if the picture is landscape or portrait, then
//calculate scale factor and offset
if (image.size.width > image.size.height) {
ratio = newSize.width / image.size.width;
delta = (ratio*image.size.width - ratio*image.size.height);
offset = CGPointMake(delta/2, 0);
} else {
ratio = newSize.width / image.size.height;
delta = (ratio*image.size.height - ratio*image.size.width);
offset = CGPointMake(0, delta/2);
}
//make the final clipping rect based on the calculated values
CGRect clipRect = CGRectMake(-offset.x, -offset.y,
(ratio * image.size.width) + delta,
(ratio * image.size.height) + delta);
//start a new context, with scale factor 0.0 so retina displays get
//high quality image
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
//0.0
UIGraphicsBeginImageContextWithOptions(sz, YES, 1.0);
} else {
UIGraphicsBeginImageContext(sz);
}
UIRectClip(clipRect);
[image drawInRect:clipRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
错误信息...
OpenCV(3.4.0-dev) Error: Bad argument (Wrong input image size. Reason: Training and Test images must be of equal size! Expected an image with 12000000 elements, but got 9000000.) in predict, file /Users/mustafa/Desktop/OpenCVBuild/opencv_contrib/modules/face/src/fisher_faces.cpp, line 140
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: OpenCV(3.4.0-dev) /Users/mustafa/Desktop/OpenCVBuild/opencv_contrib/modules/face/src/fisher_faces.cpp:140: error: (-5) Wrong input image size. Reason: Training and Test images must be of equal size! Expected an image with 12000000 elements, but got 9000000. in function predict
(lldb)
这给了我一张 3000x3000 的图片。
3.
- (UIImage *)scaleImageToSize:(CGSize)newSize image:(UIImage *)image {
CGRect scaledImageRect = CGRectZero;
CGFloat aspectWidth = newSize.width / image.size.width;
CGFloat aspectHeight = newSize.height / image.size.height;
CGFloat aspectRatio = MIN ( aspectWidth, aspectHeight );
scaledImageRect.size.width = image.size.width * aspectRatio;
scaledImageRect.size.height = image.size.height * aspectRatio;
scaledImageRect.origin.x = (newSize.width - scaledImageRect.size.width) / 2.0f;
scaledImageRect.origin.y = (newSize.height - scaledImageRect.size.height) / 2.0f;
UIGraphicsBeginImageContextWithOptions( newSize, NO, 0 );
[image drawInRect:scaledImageRect];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
错误信息...
Error: Bad argument (Wrong input image size. Reason: Training and Test images must be of equal size! Expected an image with 12000000 elements, but got 108000000.) in predict, file /Users/mustafa/Desktop/OpenCVBuild/opencv_contrib/modules/face/src/fisher_faces.cpp, line 140
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: OpenCV(3.4.0-dev) /Users/mustafa/Desktop/OpenCVBuild/opencv_contrib/modules/face/src/fisher_faces.cpp:140: error: (-5) Wrong input image size. Reason: Training and Test images must be of equal size! Expected an image with 12000000 elements, but got 108000000. in function predict
(lldb)
这给了我一张 9000x12000 的图片(销量增加了 3)。
非常感谢 advice/help。这个问题困扰我好几天了!
我使用本机 OpenCV 调整大小功能解决了这个问题。
cv::resize(input, output, cv::Size(3000,4000));
输入输出都是cv::Mat
类型
我正在使用 OpenCV Contrib 框架来执行面部识别。我的问题是,当隔离面孔(使用 for 循环)时,OpenCV 会裁剪测试图像以仅显示面孔(如面孔周围的 40x40 框)。我需要将此图像调整为 3000x4000(我的训练图像就是这个尺寸)。 我的问题是如何将图像(大部分时间为 40x40)调整为 3000x4000。
以下是我尝试过的几个调整大小的函数及其相应的错误信息。
1.
(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
错误信息...
OpenCV(3.4.0-dev) Error: Bad argument (Wrong shapes for given matrices. Was size(src) = (1,48000000), size(W) = (12000000,1).) in subspaceProject, file /Users/mustafa/Desktop/OpenCVBuild/opencv/modules/core/src/lda.cpp, line 183
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: OpenCV(3.4.0-dev) /Users/mustafa/Desktop/OpenCVBuild/opencv/modules/core/src/lda.cpp:183: error: (-5) Wrong shapes for given matrices. Was size(src) = (1,48000000), size(W) = (12000000,1). in function subspaceProject
(lldb)
我在 predict
函数(发生崩溃的地方)之前设置了一个断点,我传递给函数的图像似乎是 3000x4000。
screenshot
2.
(UIImage *)squareImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
double ratio;
double delta;
CGPoint offset;
//make a new square size, that is the resized imaged width
CGSize sz = CGSizeMake(newSize.width, newSize.width);
//figure out if the picture is landscape or portrait, then
//calculate scale factor and offset
if (image.size.width > image.size.height) {
ratio = newSize.width / image.size.width;
delta = (ratio*image.size.width - ratio*image.size.height);
offset = CGPointMake(delta/2, 0);
} else {
ratio = newSize.width / image.size.height;
delta = (ratio*image.size.height - ratio*image.size.width);
offset = CGPointMake(0, delta/2);
}
//make the final clipping rect based on the calculated values
CGRect clipRect = CGRectMake(-offset.x, -offset.y,
(ratio * image.size.width) + delta,
(ratio * image.size.height) + delta);
//start a new context, with scale factor 0.0 so retina displays get
//high quality image
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
//0.0
UIGraphicsBeginImageContextWithOptions(sz, YES, 1.0);
} else {
UIGraphicsBeginImageContext(sz);
}
UIRectClip(clipRect);
[image drawInRect:clipRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
错误信息...
OpenCV(3.4.0-dev) Error: Bad argument (Wrong input image size. Reason: Training and Test images must be of equal size! Expected an image with 12000000 elements, but got 9000000.) in predict, file /Users/mustafa/Desktop/OpenCVBuild/opencv_contrib/modules/face/src/fisher_faces.cpp, line 140
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: OpenCV(3.4.0-dev) /Users/mustafa/Desktop/OpenCVBuild/opencv_contrib/modules/face/src/fisher_faces.cpp:140: error: (-5) Wrong input image size. Reason: Training and Test images must be of equal size! Expected an image with 12000000 elements, but got 9000000. in function predict
(lldb)
这给了我一张 3000x3000 的图片。
3.
- (UIImage *)scaleImageToSize:(CGSize)newSize image:(UIImage *)image {
CGRect scaledImageRect = CGRectZero;
CGFloat aspectWidth = newSize.width / image.size.width;
CGFloat aspectHeight = newSize.height / image.size.height;
CGFloat aspectRatio = MIN ( aspectWidth, aspectHeight );
scaledImageRect.size.width = image.size.width * aspectRatio;
scaledImageRect.size.height = image.size.height * aspectRatio;
scaledImageRect.origin.x = (newSize.width - scaledImageRect.size.width) / 2.0f;
scaledImageRect.origin.y = (newSize.height - scaledImageRect.size.height) / 2.0f;
UIGraphicsBeginImageContextWithOptions( newSize, NO, 0 );
[image drawInRect:scaledImageRect];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
错误信息...
Error: Bad argument (Wrong input image size. Reason: Training and Test images must be of equal size! Expected an image with 12000000 elements, but got 108000000.) in predict, file /Users/mustafa/Desktop/OpenCVBuild/opencv_contrib/modules/face/src/fisher_faces.cpp, line 140
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: OpenCV(3.4.0-dev) /Users/mustafa/Desktop/OpenCVBuild/opencv_contrib/modules/face/src/fisher_faces.cpp:140: error: (-5) Wrong input image size. Reason: Training and Test images must be of equal size! Expected an image with 12000000 elements, but got 108000000. in function predict
(lldb)
这给了我一张 9000x12000 的图片(销量增加了 3)。
非常感谢 advice/help。这个问题困扰我好几天了!
我使用本机 OpenCV 调整大小功能解决了这个问题。
cv::resize(input, output, cv::Size(3000,4000));
输入输出都是cv::Mat