Sunday, May 12, 2024
HomeiOS Developmentios - OpenCV: Nostril Resizing Alters Cropped Space As a substitute of...

ios – OpenCV: Nostril Resizing Alters Cropped Space As a substitute of Direct Face Modification


I’m engaged on resizing the nostril on a facial picture utilizing OpenCV in Goal-C for iOS. The objective is to elongate or shorten the nostril, however my present implementation solely modifies a cropped space, which is then overlaid again onto the unique picture relatively than adjusting the nostril measurement immediately on the face. Right here is the snippet from my OpenCVWrapper.mm:

+ (UIImage *)makeNoseLongerInImage:(UIImage *)picture withFactor:(int)scaleFactor {
    cv::Mat mat;
    [image convertToMat:&mat :false];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"haarcascade_frontalface_default" ofType:@"xml"];
    cv::CascadeClassifier face_cascade;
    if (!face_cascade.load([path UTF8String])) {
        NSLog(@"Didn't load face cascade from path: %@", path);
        return picture;
    }

    std::vector<cv::Rect> faces;
    face_cascade.detectMultiScale(mat, faces, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, cv::Dimension(30, 30));

    for (const cv::Rect& face : faces) {
        cv::Rect noseArea(
            std::max(0, face.x + static_cast<int>(face.width * 0.25)),
            std::max(0, face.y + static_cast<int>(face.top * 0.60)),
            std::min(mat.cols - face.x, static_cast<int>(face.width * 0.50)),
            std::min(mat.rows - face.y, static_cast<int>(face.top * 0.12))
        );

        if (noseArea.width == 0 || noseArea.top == 0) {
            NSLog(@"nostril space computed with zero width or top.");
            proceed;
        }

        cv::Mat noseRegion = mat(noseArea);
        int newHeight = std::max(1, static_cast<int>(noseRegion.rows * scaleFactor / 100.0));
        cv::Mat resizednoseRegion;
        cv::resize(noseRegion, resizednoseRegion, cv::Dimension(noseRegion.cols, newHeight), 0, 0, cv::INTER_LINEAR);

        NSLog(@"Unique nostril area: (%d, %d), Resized: (%d, %d)", noseRegion.cols, noseRegion.rows, resizednoseRegion.cols, resizednoseRegion.rows);

        // Place the resized nostril area again into the unique mat
        if (!resizednoseRegion.empty() && resizednoseRegion.cols == noseRegion.cols && resizednoseRegion.rows == newHeight) {
            // Replace the noseArea to match the resized dimensions
            cv::Rect newnoseArea = noseArea;
            newnoseArea.top = newHeight; // Replace top to match resized area
            resizednoseRegion.copyTo(mat(newnoseArea));
        } else {
            NSLog(@"Mismatch in dimensions after resize or empty resized area.");
        }
    }

    return MatToUIImage(mat);
}

output image

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular