【问题标题】:How to get screen dimensions for iPhone using openCV?如何使用 openCV 获取 iPhone 的屏幕尺寸?
【发布时间】:2019-12-11 02:51:34
【问题描述】:

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

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

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

如何获取应用框架的screenWidthscreenHeight

提前致谢,

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

#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

【问题讨论】:

  • 你用 iPhone 5s 得到了什么?
  • 我有 iPhone 6s
  • 你在 iPhone 6s 上买了什么尺寸的?
  • @Gralex 更新了代码。我在 swift 项目中使用了 Objective-C++ 包装器。
  • 对于 iPhone 6s - (x = 0, y = 0, width = 750, height = 2668) 代码是 cv::Rect upperRect = cv::Rect(0, 0, UIScreen. mainScreen.bounds.size.width*2, UIScreen.mainScreen.bounds.size.height*2);

标签: ios xcode opencv computer-vision objective-c++


【解决方案1】:

您以磅为单位获得大小。要获得像素,您需要乘以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);

【讨论】:

  • 我试过了,但还是有问题。更新了相关截图。
  • @KrishnaChaitanyaBandaru 在获取值的代码中没有解决这个问题。您能否提供代码如何在屏幕上显示此图像?
猜你喜欢
  • 2011-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-31
  • 1970-01-01
相关资源
最近更新 更多