Skip to content

Mat conversions for macOS/AppKit#18547

Merged
alalek merged 12 commits intoopencv:masterfrom
mtfrctl:objc-conversions-macosx
Oct 14, 2020
Merged

Mat conversions for macOS/AppKit#18547
alalek merged 12 commits intoopencv:masterfrom
mtfrctl:objc-conversions-macosx

Conversation

@mtfrctl
Copy link
Copy Markdown
Contributor

@mtfrctl mtfrctl commented Oct 9, 2020

This PR solves #18546.
It consists

  • Extract CoreGraphics conversion logics from ios_conversions.mm to apple_conversions.h, apple_conversions.mm (MatToCGImage(), CGImageToMat())
  • Simplify ios_conversions.mm
  • Add macosx_conversions.mm for macOS/AppKit (MatToNSImage(), NSImageToMat())
  • Add Mat+Conversions.h and Mat+Conversions.mm for macOS/AppKit

Usage example in Swift:
※ Don't forget to set the -all_load flag to "Other Linker Flags" in "Build Settings" when using! #17532

// CGImage to Mat
let sourceMat = Mat(cgImage: cgImage, alphaExist: true)

// NSImage to Mat
let sourceMat = Mat(nsImage: nsImage, alphaExist: true)

// Something to process
let grayMat = Mat()
Imgproc.cvtColor(src: sourceMat, dst: grayMat, code: .COLOR_RGBA2GRAY)

// Mat to CGImage
let cgImage = grayMat.toCGImage().takeRetainedValue() // or .takeUnetainedValue()

// Mat to NSImage
let nsImage = grayMat.toNSImage()

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to original bug report and related work
force_builders=Custom Mac
build_image:Custom Mac=osx_framework

buildworker:iOS=macosx-1

@mtfrctl mtfrctl changed the title Mat conversions for macOS(AppKit) Mat conversions for macOS/AppKit Oct 9, 2020
Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contribution!

Please take a look on comments below.

//
//
// License Agreement
// For Open Source Computer Vision Library
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use short license header: https://github.com/opencv/opencv/wiki/Coding_Style_Guide#file-structure

// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.

Copy link
Copy Markdown
Contributor Author

@mtfrctl mtfrctl Oct 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙆‍♂️
42a685c

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for updates!
Please make similar update of license headers of new files in this PR.
Please remove empty first line in this file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#import <AVFoundation/AVFoundation.h>
#import <ImageIO/ImageIO.h>
#include "opencv2/core.hpp"
#include "precomp.hpp"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

precomp.hpp

Please don't use this from header files. Move into .mm files instead

Copy link
Copy Markdown
Contributor Author

@mtfrctl mtfrctl Oct 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I moved it.
edfe785


CGColorSpaceRef colorSpace;

if (image.elemSize() == 1) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.elemSize() => .channels()

+ CV_CheckDepth(image.depth(), CV_8U, ""); check on the first line.


OK, I see this code is just moved.

@mtfrctl mtfrctl requested a review from alalek October 9, 2020 11:50
@mtfrctl mtfrctl mentioned this pull request Oct 9, 2020
5 tasks
@komakai
Copy link
Copy Markdown
Contributor

komakai commented Oct 10, 2020

@mtfrctl thanks for this - this is a great feature.
I checked the code and everything looks fine.
I have tried it quickly - after I remembered to set -all_load in the Other Linker Flags of my project everything worked well.
👍

@mtfrctl
Copy link
Copy Markdown
Contributor Author

mtfrctl commented Oct 10, 2020

@komakai Thank you! Regarding the flag, people who come to check here will also check these @komakai and @treastrain conversations (#17532 #18023), but it would be nice in the future so that it can be used immediately without being aware of these things ⭐

@mtfrctl
Copy link
Copy Markdown
Contributor Author

mtfrctl commented Oct 11, 2020

@alalek By the way, I would like to add the following method to modules/core/misc/objc/common/Mat.mm (and .h) for more flexibility, is it okay to include it in this PR, or is it better to separate it into another PR?

- (unsigned char*)dataPtr {
    return _nativePtr->data;
}

@alalek
Copy link
Copy Markdown
Member

alalek commented Oct 11, 2020

If there is no strong dependency between patches, then separate PR is better.

@mtfrctl
Copy link
Copy Markdown
Contributor Author

mtfrctl commented Oct 11, 2020

@alalek I created a PR separately here #18566. I'm sorry to bother you, but I'd be happy if you could check it. 🙏

Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mtfrctl Thank you for contribution 👍

@komakai Thank you for review!

@alalek alalek merged commit 7de1891 into opencv:master Oct 14, 2020
@mtfrctl mtfrctl deleted the objc-conversions-macosx branch October 15, 2020 06:13
@komakai
Copy link
Copy Markdown
Contributor

komakai commented Oct 15, 2020

@mtfrctl keep the contributions coming!

a-sajjad72 pushed a commit to a-sajjad72/opencv that referenced this pull request Mar 30, 2023
Mat conversions for macOS/AppKit

* Extract CoreGraphics conversion logics from ios_conversions.mm to apple_conversions.h, apple_conversions. Add macosx_conversions.mm

* Add macosx.h

* Add Mat+Conversions.h and Mat+Conversions.mm

* Delete duplicated declaration from apple_conversion.mm

* Use short license header

* Add compile guard

* Delete unused imports

* Move precomp.hpp import from header to implementation

* Add macosx.h to skip headers

* Fix compile guard condition

* Use short license header

* Remove commented out unused code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants