首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何基于有效的OpenCV代码在iPhone应用程序的opencvWrapper文件中创建Python函数findcontour

如何基于有效的OpenCV代码在iPhone应用程序的opencvWrapper文件中创建Python函数findcontour
EN

Stack Overflow用户
提问于 2018-08-11 16:42:36
回答 1查看 580关注 0票数 0

我已经在我的iPhone应用程序中集成了OpenCV,并设法拍摄了一张照片,然后使用以下代码对其进行处理。现在,我想要实现findcontour函数,以便获得轮廓和层次结构,就像我在Python中所做的那样(代码如下)。这段代码有人能帮上忙吗?

代码语言:javascript
复制
// OpenCVWrapper.mm

@implementation OpenCVWrapper : NSObject

+(UIImage *)ConvertImage:(UIImage *)image {
cv::Mat mat;
UIImageToMat(image, mat);


cv::Mat median;
cv::medianBlur(mat, median, 3);

cv::Mat gray;
cv::cvtColor(median, gray, CV_RGB2GRAY);  //or use COLOR_BGR2GRAY

cv::Mat denoise;
cv::fastNlMeansDenoising(gray, denoise, 30.0, 7, 21);


//here I want to enter the converted Python code from below in order to get the largest contour of the image
//cv::findContours(denoise, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);


UIImage *binImg = MatToUIImage(contour);
return binImg;
}
@end

Python代码是:

代码语言:javascript
复制
image, contours, hierarchy = cv2.findContours(denoise,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

contours = [(c, cv2.contourArea(c)) for c in contours]
# sort contours list by area, biggest first
contours = sorted(contours, key=lambda c: c[1], reverse=True)

cnt = contours[0]
img = cv2.drawContours(frame, cnt, 0, (0,255,0), 2)

我实际上不确定是否可以处理UIImage,然后只返回轮廓(透明背景),以便可以将其叠加到视频流中。以下是使用captureOutput的ViewController中的代码:

代码语言:javascript
复制
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!,
                   from connection: AVCaptureConnection!) {
    guard let uiImage = imageFromSampleBuffer(sampleBuffer: sampleBuffer) else { return }
    DispatchQueue.main.async(execute: {

        self.imageView.image = OpenCVWrapper.convert(uiImage) ///I am really not sure whether this is the right code to show the uiImage on top of the live stream. Have not really gotten to that issue as yet, but any assistance is welcome
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-11 18:55:16

实际上,我想通了。以下是可行的解决方案:

代码语言:javascript
复制
//Find the contours. Use the contourOutput Mat so the original image doesn't get overwritten
std::vector<std::vector<cv::Point> > contours;
cv::Mat contourOutput = canny.clone();
cv::findContours( contourOutput, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE );

int largest_area=0;
int largest_contour_index=0;
// iterate through each contour.
for( int i = 0; i< contours.size(); i++ )
{
    //  Find the area of contour
    double a=contourArea( contours[i],false);
    if(a>largest_area){
        largest_area=a;std::cout<<i<<" area  "<<a<<std::endl;
        // Store the index of largest contour
        largest_contour_index=i;

    }
}

cv::Mat contourImage(canny.size(), CV_8UC3, cv::Scalar(0,0,0));
cv::Scalar color;
color = cv::Scalar(255, 0, 0);

cv::drawContours(contourImage, contours, largest_contour_index, color);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51798006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档