OpenCV:在iOS中覆盖具有透明度的图像时的随机alpha通道工件

时间:2016-07-19 08:56:48

标签: c++ ios opencv

在我的iOS项目中,我正在添加小的PNG图像,包括Alpha通道作为JPEG图片上的叠加。我的设备在DEBUG模式下的结果与预期的一样,正确绘制了眼泪。

iOS Result DEBUG mode

当我在Simulator上运行相同的代码或当我在RELEASE模式下存档并导出应用程序时,我会在alpha通道中获得随机工件。

iOS Result on Simulator and RELEASE mode

底层的cv :: Mat都包含标题信息和有效的数据部分。即使在绿色背景下,错误也是可重现的。

enter image description here

这种行为似乎是完全随机的,因为有时不会产生伪影(图3:右眼撕裂,图像4:左眼撕裂)。 enter image description here

想法,任何人?

1 个答案:

答案 0 :(得分:0)

    @IBAction func LocationChooserPressed(_ sender: AnyObject) {

    let center = CLLocationCoordinate2DMake((locationManager.location?.coordinate.latitude)!, (locationManager.location?.coordinate.longitude)!)
    let northEast = CLLocationCoordinate2DMake(center.latitude + 0.001, center.longitude + 0.001)
    let southWest = CLLocationCoordinate2DMake(center.latitude - 0.001, center.longitude - 0.001)
    let viewport = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest)
    let config = GMSPlacePickerConfig(viewport: viewport)
    placePicker = GMSPlacePicker(config: config)


    placePicker?.pickPlace( callback: { (place: GMSPlace?, error: Error?) -> Void in
        if let error = error {
            print("Pick Place error: \(error.localizedDescription)")
            return
        }

        if let place = place {
            self.nameLabel.text = place.name
            self.addressLabel.text = place.formattedAddress!.components(separatedBy: ", ").joined(separator: "\n")
            self.chosenLong = String(place.coordinate.longitude)
            self.chosenLat = String(place.coordinate.latitude)

        } else {
            //self.nameLabel.text = "No place selected"
            self.addressLabel.text = ""
        }
    })
}

和叠加功能如下所示

覆盖功能引自:http://answers.opencv.org/question/73016/how-to-overlay-an-png-image-with-alpha-channel-to-another-png/

const char *cpath1 = [@"" cStringUsingEncoding:NSUTF8StringEncoding];//overlay image path , within @"" pass your image path which is in NSString

const char *cpath  = [@"" cStringUsingEncoding:NSUTF8StringEncoding];//underlay imagepath

    cv::Mat overlay = cv::imread(cpath1,-1);//-1 is for read .png images
    cv::Mat underlay = cv::imread(cpath,-1);

    //convert mat image in to RGB channel
    cv::Mat overlayAlpha;
    std::vector<Mat> channels1;
    split(overlay, channels1);
    channels1[3].copyTo(overlayAlpha);

    cv::Mat underlayAlpha;
    std::vector<Mat> channels2;
    split(underlay, channels2);
    channels2[3].copyTo(underlayAlpha);

    overlayImage( &underlay, &overlay,cv::Point(10,10);

    convert final image to RGB channel
    cv::split(underlay,channels1);

    std::swap(channels1[0],channels1[2]);// swap B and R channels.
    cv::merge(channels1,underlay);//merge channels

    MatToUIImage(background); //display your final image, it returns cv::Mat image