The Question Title May sound basic but for me it's not :D
I want to crop out the image from the dashed line to the next black border at bottom from the 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.
The text isn't extractable sometimes so can't verify if it's there or not.
How can i trim the image to border while ignoring the Text at Top and Bottom ?
Can't use OpenCV multiple times (to detect the borderBox) as the function will be deployed at firebase functions and the cropping needs to be done under 5 seconds. the detection of the dashed line itself is taking 8 seconds
Code
/**
* 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");
})