Reputation: 47
does anyone know if it is possible to pass an array of CGPoint from objective c++ to swift?
I am able to pass CGPoint back to swift. However, I was not able to figure out how to pass the value back as an array.
Here is the code I used to pass it back to swift.
OpenCVWrapper.mm
+(CGPoint) detectCircle:(UIImage *)image
{
CGPoint pointArray;
cv::Mat img;
UIImageToMat(image, img);
cv::Mat grayMat;
cvtColor(img, grayMat, CV_BGR2GRAY);
cv::Mat dilateMat;
cv::Mat kernel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(7,7));
cv::morphologyEx(grayMat, dilateMat, cv::MORPH_OPEN, kernel);
cv::Mat cannyMat;
Canny(dilateMat, cannyMat, 10, 100);
cv::Mat guassianMat;
cv::GaussianBlur(cannyMat, guassianMat, cv::Size(11,13), 0);
std::vector<cv::Vec3f> circleMat;
cv::HoughCircles(guassianMat, circleMat, CV_HOUGH_GRADIENT, 1, cannyMat.rows / 15,120, 10, 0, 30);
cv::Mat houghCircles;
houghCircles.create(guassianMat.rows, guassianMat.cols, CV_8UC1);
for( size_t i = 0; i < circleMat.size(); i++ )
{
cv::Vec3i parameters = circleMat[i];
double x, y;
int r;
x = parameters[0];
y = parameters[1];
r = int(parameters[2]);
pointArray.x = x;
pointArray.y = y;
}
return pointArray;
}
OpenCVWrapper.h
+(CGPoint) detectCircle: (UIImage *)image;
viewController.Swift
@IBAction func detectBtn(_ sender: Any) {
var points : CGPoint = OpenCVWrapper.detectCircle(adjustBtightnessImage)
print(points)
}
Upvotes: 1
Views: 957
Reputation: 2344
The best way to do this is to use NSValue
to wrap it as @OOPer said.
The main reason is that CGPoint
is not an object type in Objective-C. You can't put it directly in an NSArray
. You need to wrap it in NSValue
.
// Objective-C
+ (NSArray<NSValue *> *)detectCircle:(UIImage *)image {
return @[
[NSValue valueWithCGPoint:CGPointMake(10, 10)],
[NSValue valueWithCGPoint:CGPointMake(10, 20)]
];
}
// swift
let points = Circle.detect(UIImage()).map { $0.cgPointValue }
Upvotes: 4