我正在使用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。
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计算)。
非常感谢任何建议/帮助。这个问题困扰了我好几天了!
答案 0 :(得分:0)
我通过使用本机OpenCV调整大小函数解决了这个问题。
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.trashImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onDeleteClick(view.getContext(), position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, mDataset.size());
}
});
}
输入和输出都是 cv::resize(input, output, cv::Size(3000,4000));