首页 > 解决方案 > 将图像修剪到内容框忽略顶部/底部的文本?

问题描述

问题标题可能听起来很基本,但对我来说不是:D

I want to crop out the image from the dashed line to the next black border at bottom from the image.

Demo Image

The Content Box (The Outer Box) is not at a fixed position. It varies in height width and top. The text at top and bottom are not always there sometimes they exist some times they don't.

So to detect the dashed line i started using openCV match template.

To Escape from finding the bottom line (the black line at bottom next to the dashed line) work I thought of trimming the image to content (I just needed to crop the image from top so I don't have to find the height of image from dashed line to bottom border) and for that I used Sharp Library.

But the problem is if the text exists the trimming is done just to the text but I want the trimming to be done to the black bordered box.

文本有时不可提取,因此无法验证它是否存在。

如何在忽略顶部和底部的文本的同时将图像修剪到边框?

不能多次使用 OpenCV(检测边界框),因为该函数将部署在 firebase 函数中,并且裁剪需要在 5 秒内完成。虚线本身的检测需要 8 秒

代码

/**
 * The Idea is to crop the image to content/Border Box 
 * Then Find the Dashed Line with OpenCV
 * OpenCV will give the X (Position from Top) Value of the Dashed Line.
 * Using that X value with Sharp Library We'll Crop the Image from TOP.
 * 
 * But The Problem is The Trim to Content won't work if there is other
 * Content (Like Text in my Case) in the path from corners to the Content Box.
 */
 
// Trimming to Content / Bordered Box
sharp(filePath).trim(50).toBuffer()
    .then(async (buffer) => {
 
        /**
         * The Image Here is Trimmed to The Content / Border Box 
         * Only if there is no text on the corners or at the
         * Top and Bottom of the image.
         * 
         * How to Ignore them so the Trimming to Content Can 
         * Be done right ?
         */
        
        
        // Stored Trimmed Image as sharp Instance for later Use
        let trimmedImage = sharp(buffer);
        
        let { width, height } = await trimmedImage.metadata();        
        
        
        // Template Matching 
        let findBuffer = await sharp("./dashedTemplate.jpeg").resize(width).toBuffer();
        
        let img1 = await loadImage(buffer);
        let img2 = await loadImage(findBuffer);
        
        img1 = cv.imread(img1);
        img2 = cv.imread(img2);
        
        let canIn = createCanvas();
        cv.imshow(canIn, img1);
        
        let canFi = createCanvas();
        cv.imshow(canFi, img2);
        
        let src = cv.imread(canIn);
        let temp = cv.imread(canFi);
        
        let dest = new cv.Mat();
        let mask = new cv.Mat();
        
        cv.matchTemplate(src, temp, dest, cv.TM_CCOEFF_NORMED, mask);
        
        let result = cv.minMaxLoc(dest, mask);                
        
        let maxPoint = result.maxLoc;
        src.delete(); dest.delete(); mask.delete();
        
        // Position from top where the ExepectImaged was found
        let cvTop;
 
        cvTop = maxPoint.y + 8;
        
        // Save the final Image.
        trimmedImage
        .extract({ top: cvTop, left: 0, width: width, height: (height - cvTop) })
        .toFile("./output/" + path.basename(filePath, ".jpeg") + "_cropped.jpeg");
    }) 

标签: javascriptnode.jsimage-processingsharp

解决方案


推荐阅读