0

我正在开发一个 iPhone 应用程序,它必须从预览中检测蓝色对象并使用 drawcontour api 将其更改为黄色。我尝试了很多,但轮廓是用白色绘制的。我搜索了很多,但我无法解决我的问题。我的代码如下:

- (void)viewDidLoad {
   [super viewDidLoad];    
   self->videoCamera = [[CvVideoCamera alloc] initWithParentView:imageView];
   self->videoCamera.delegate = self;
   self->videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack;
   self->videoCamera.defaultAVCaptureSessionPreset =
   AVCaptureSessionPreset352x288;
   self->videoCamera.defaultAVCaptureVideoOrientation =
   AVCaptureVideoOrientationPortrait;
   self->videoCamera.grayscaleMode = NO;
   self->videoCamera.defaultFPS = 30;    
   [videoCamera start];
}

委托获取帧:

-(void)processImage:(cv::Mat &)image
{
    cv::Mat image_copy = [self convertToYellow:image];       
}

转换方法

- (cv::Mat)convertToYellow:(cv::Mat)image
{    
cv::Mat cameraFeed = image;
cv::Mat HSV, threshold;
cv::cvtColor(cameraFeed, HSV, CV_BGR2HSV);    
cv::inRange(HSV, cvScalar(90,50,50), cvScalar(130,255,255), threshold);

cv::Mat erodeElement = getStructuringElement( cv::MORPH_RECT,    cvSize(3,3));
//dilate with larger element so make sure object is nicely visible
cv::Mat dilateElement = getStructuringElement(  cv::MORPH_RECT,cvSize(3,3));

erode(threshold,threshold,erodeElement);
erode(threshold,threshold,erodeElement);

dilate(threshold,threshold,dilateElement);
dilate(threshold,threshold,dilateElement);   

cv::Mat temp;
threshold.copyTo(temp);
cv::vector< cv::vector<cv::Point> > contours;
cv::vector<cv::Vec4i> hierarchy;

findContours( temp, contours, hierarchy, CV_RETR_EXTERNAL,  CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );

bool objectFound = false;
if (hierarchy.size() > 0) {

    for (int index = 0; index >= 0; index = hierarchy[index][0]) {

        cv::Moments moment = moments((cv::Mat)contours[index]);
        double area = moment.m00;


        if(area > 500){

            objectFound = true;

        }else objectFound = false;
    }

    //let user know you found an object
    if(objectFound ==true){

        for(int i=0; i < contours.size() ; i++)
        {      
                    cv::drawContours(cameraFeed,contours,i,cvScalar(0,255,255),CV_FILLED);

        }

    }

}

return cameraFeed;

}

实物

实物

等高线画好后

绘制轮廓后预览

有人请帮忙。提前致谢

4

0 回答 0