相关疑难解决方法(0)

UIImage:调整大小,然后裁剪

我一直在this this for for for for for for and and and while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while

我认为,在我的设计的概念阶段提前,从iPhone的相机或库中获取图像,将其缩小到指定高度,使用与Aspect Fill选项相当的功能将是一件小事. UIImageView(完全在代码中),然后裁掉任何不适合传递给CGRect的东西.

从相机或图书馆获取原始图像非常简单.我很惊讶其他两个步骤的难度.

附图显示了我想要实现的目标.有人请你好好握住我的手吗?到目前为止我发现的每个代码示例似乎都会破坏图像,颠倒过来,看起来像废话,画出界限,或者其他方法都不能正常工作.

iphone core-graphics image-processing ios

187
推荐指数
6
解决办法
12万
查看次数

在iOS中绘制部分图像的最有效方法

给定a UIImage和a CGRect,绘制与CGRect(不缩放)对应的图像部分的最有效方式(在内存和时间上)是什么?

作为参考,这是我目前的做法:

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect frameRect = CGRectMake(frameOrigin.x + rect.origin.x, frameOrigin.y + rect.origin.y, rect.size.width, rect.size.height);    
    CGImageRef imageRef = CGImageCreateWithImageInRect(image_.CGImage, frameRect);
    CGContextTranslateCTM(context, 0, rect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, rect, imageRef);
    CGImageRelease(imageRef);
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,对于中等大小的图像和高setNeedsDisplay频率,这似乎非常慢.使用UIImageView框架并clipToBounds产生更好的结果(灵活性较低).

iphone uiimage drawrect quartz-2d ios

36
推荐指数
2
解决办法
2万
查看次数

使用OpenCV人脸检测在iOS中裁剪图像

我使用以下代码在人脸检测代码中从图像裁剪出人脸。但是我没有得到正确的人脸图像,而是得到了一部分图像而不是脸部图像。我的代码有什么问题?

_faceCascade.detectMultiScale(mat, faces, 1.1, 2, kHaarOptions, cv::Size(40, 40));
Run Code Online (Sandbox Code Playgroud)

并且在displayfaces函数内部使用以下代码进行裁剪:

CGRect cropRect = CGRectMake(faces[i].x, faces[i].y, faces[i].width, faces[i].width);
CGImageRef cropped_img = CGImageCreateWithImageInRect(self.storeImage.CGImage, cropRect);
UIImage *img = [UIImage imageWithCGImage:cropped_img];
UIImageWriteToSavedPhotosAlbum( img, self,  nil,nil);
Run Code Online (Sandbox Code Playgroud)

正在获取的正确坐标faces[i]。但是问题仅在于裁剪和设置ROI。有人可以帮我解决吗?

我也尝试使用下面的代码,再次获得相同的图像。(即没有得到实际的人脸图像)

cv :: Mat image_roi;
cv::Rect roi(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
cv::Mat(testMat, roi).copyTo(image_roi);
UIImage *img = [CaptureViewController imageWithCVMat:image_roi ];
UIImageWriteToSavedPhotosAlbum( img, self,  nil,nil);
Run Code Online (Sandbox Code Playgroud)

注意:我正在使用opencv facedetect在实时视频流中检测人脸。我可以得到脸周围的绿色矩形。但是我无法使用人脸参数裁剪人脸。我也尝试设置faceroi来检测眼睛,即使失败了。为了缩小问题的范围,问题可能出在为图像设置ROI吗?

13年11月2日更新:

我的裁剪如下,但是ROI设置不正确,图像裁剪不佳:

我在上面的帖子中找到了问题(感谢@Jameo指出这是由于旋转问题。)我已经如下旋转了图像。

UIImage *rotateImage = [[UIImage alloc] initWithCGImage: image.CGImage
                               scale: 1.0
                         orientation: UIImageOrientationRight];
Run Code Online (Sandbox Code Playgroud)

然后使用以下代码裁剪图像:

// get sub image
+ (UIImage*) getSubImageFrom: …
Run Code Online (Sandbox Code Playgroud)

iphone opencv image-processing objective-c ios

5
推荐指数
1
解决办法
3388
查看次数