gpt4 book ai didi

ios - 如何使用 openCV 获取 iPhone 的屏幕尺寸?

转载 作者:行者123 更新时间:2023-11-29 05:33:16 25 4
gpt4 key购买 nike

我试图使用 OpenCV 获取 iPhone 的屏幕宽度和高度。但无法得到准确的结果。

这是我使用的,但给出了错误的结果。我是 OpenCV 新手。所以我不确定以正确的方式使用它。

cv::Rect screenRect = cv::Rect(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height);

如何获取应用程序框架的screenWidthscreenHeight

提前致谢。

@Gralex 使用您提供的解决方案后。我得到的结果是这样的...

enter image description here

#import "OpenCVWrapper.h"
#import <opencv2/videoio/cap_ios.h>
#include <opencv2/imgproc/imgproc.hpp>
#import <opencv2/imgcodecs/ios.h>
#include <opencv2/core/types.hpp>

using namespace cv;
using namespace std;

@interface OpenCVWrapper() <CvVideoCameraDelegate>
@property (strong, nonatomic) CvVideoCamera *videoCamera;
@end
@implementation OpenCVWrapper

#pragma mark - initializers
- (instancetype)initWithParentView:(UIImageView *)parentView {
if (self = [super init]) {
parentView.contentMode = UIViewContentModeScaleAspectFill;

self.videoCamera = [[CvVideoCamera alloc] initWithParentView:parentView];
self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack;
self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPresetHigh;
self.videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;
self.videoCamera.defaultFPS = 30;
self.videoCamera.grayscaleMode = false;
self.videoCamera.delegate = self;
[self.videoCamera start];

}

return self;
}

#pragma mark - Private Methods
- (Mat)applyCannyAlgorithm:(int)threshold input:(Mat)input {
Mat cannyCVResult;
Canny(input, cannyCVResult, threshold, threshold*2);
return cannyCVResult;
}

- (Mat)changeGrayAndApplyBlur:(Mat)input {
Mat greyCVImage;
cvtColor(input, greyCVImage, CV_BGR2GRAY);
GaussianBlur(greyCVImage, greyCVImage, cv::Size(5, 5), 0);
return greyCVImage;
}

- (void)detectAndDrawBoundaries:(Mat)cvMat {
Mat cvImage = cvMat;
Mat blurImage = [self changeGrayAndApplyBlur:cvImage];
Mat cannyResult = [self applyCannyAlgorithm:80 input:blurImage];

vector<vector<cv::Point> > contours;
findContours(cannyResult, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

for( size_t i = 0; i< contours.size(); i++ ) {
cv::Rect rect = boundingRect(Mat(contours[i]));
rectangle(cvImage, rect.tl(), rect.br(), Scalar(255,0,0), 4, 8, 0);
}

cv::Rect upperRect = cv::Rect(0, 0, UIScreen.mainScreen.bounds.size.width*2, UIScreen.mainScreen.bounds.size.height);
cv::Rect lowerRect = cv::Rect(0, UIScreen.mainScreen.bounds.size.height, UIScreen.mainScreen.bounds.size.width*2, UIScreen.mainScreen.nativeBounds.size.height);

rectangle(cvImage, upperRect.tl(), upperRect.br(), Scalar(0,255,0), 8, 8, 0);
rectangle(cvImage, lowerRect.tl(), lowerRect.br(), Scalar(0,0,255), 8, 8, 0);
}

#pragma mark - CvVideoCamera Delegate Methods
- (void)processImage:(Mat&)image {
[self detectAndDrawBoundaries:image];
}

@end

最佳答案

您可以得到以点为单位的大小。要获得像素,您需要乘以 UIScreen.mainScreen.scale

cv::Rect screenRect = cv::Rect(0, 0, 
UIScreen.mainScreen.bounds.size.width * UIScreen.mainScreen.scale,
UIScreen.mainScreen.bounds.size.height * UIScreen.mainScreen.scale);

关于ios - 如何使用 openCV 获取 iPhone 的屏幕尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57327115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com