Finding the Cut From HERE Part with OpenCV

Hello, Alejandro. I’ve find out that the width of the template image is bigger then the base image. so in that case it’s making the mess. I tried to make the width of template image same as width of the base image while creating the canvas. But i didn’t find success there. I can’t reduce the width of the template image as if the base image’s width is bigger it’s going to create the mess again.

Overall, How to keep the template images width same as the width of base image ?
I tried this:

   let img1 = await loadImage(buffer); 
   let img2 = await loadImage("./find.jpeg");
   img1 = cv.imread(img1); 
   img2 = cv.imread(img2);
   let canIn = createCanvas();
   cv.imshow(canIn, img1);
   let { width, height } = await trimImage.metadata();
   let canFi = createCanvas(width, height); 
   cv.imshow(canFi, img2)

Update:

I Managed to handle the width of template by resizing it but i don’t think so that it’s efficient in any way. The Current code looks like this:

sharp(imagePath).trim().toBuffer()
        .then(async (buffer) => {
            /**
             * Removed White Space
             * Now find the location of dashed line part with openCV
             */
            let trimImage = sharp(buffer);

            let { width, height } = await trimImage.metadata();

            let findBuffer = await sharp("./find.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);

            const src = cv.imread(canIn);
            const 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 dashed line was found
            let cvTop = maxPoint.y + 8;


            trimImage
                .extract({ top: cvTop, left: 0, width: width, height: (height - cvTop) })
                .toFile("./croppedFinal.jpeg");
        }) 

Other thing is it won’t work if the background color is not white. Sometimes the background color is bit greenish so how can i handle that.