Advertisement
Guest User

open cv binary and roate

a guest
Jun 6th, 2025
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export async function contourBasedCrop(inputCanvas: HTMLCanvasElement): Promise<void> {
  2.   if (inputCanvas.width === 0 || inputCanvas.height === 0) {
  3.     console.warn("Canvas is empty, skipping processing.");
  4.     return;
  5.   }
  6.   const canvas = inputCanvas;
  7.   const cv = await getOpenCv();
  8.   const src = cv.imread(canvas);
  9.   const gray = new cv.Mat();
  10.   const binary = new cv.Mat();
  11.   const contours = new cv.MatVector();
  12.   const hierarchy = new cv.Mat();
  13.   let largestContour = null;
  14.  
  15.   try {
  16.     // Convert to grayscale
  17.     cv.cvtColor(src, gray, cv.COLOR_RGBA2GRAY);
  18.  
  19.     // Create binary image to detect contours
  20.     cv.adaptiveThreshold(
  21.       gray,
  22.       binary,
  23.       255,
  24.       cv.ADAPTIVE_THRESH_GAUSSIAN_C,
  25.       cv.THRESH_BINARY_INV,
  26.       15,
  27.       2
  28.     );
  29.  
  30.     const kernel = cv.Mat.ones(3, 3, cv.CV_8U);
  31.     cv.erode(binary, binary, kernel);
  32.     cv.dilate(binary, binary, kernel);
  33.     kernel.delete();
  34.  
  35.     // Find contours
  36.     cv.findContours(binary, contours, hierarchy, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE);
  37.     let maxArea = 0;
  38.     for (let i = 0; i < contours.size(); i++) {
  39.       const c = contours.get(i);
  40.       const area = cv.contourArea(c);
  41.       if (area > maxArea && area > 1000) {
  42.         maxArea = area;
  43.         if (largestContour) largestContour.delete();
  44.         largestContour = c.clone();
  45.       }
  46.     }
  47.  
  48.     if (!largestContour) return;
  49.  
  50.     // Get rotation matrix
  51.     const rotatedRect = cv.minAreaRect(largestContour);
  52.     let { angle } = rotatedRect;
  53.     if (rotatedRect.size.width < rotatedRect.size.height) angle += 90;
  54.     const center = new cv.Point(rotatedRect.center.x, rotatedRect.center.y);
  55.     const M = cv.getRotationMatrix2D(center, angle, 1);
  56.  
  57.     const rotated = new cv.Mat();
  58.     cv.warpAffine(
  59.       src,
  60.       rotated,
  61.       M,
  62.       src.size(),
  63.       cv.INTER_LINEAR,
  64.       cv.BORDER_CONSTANT,
  65.       new cv.Scalar()
  66.     );
  67.  
  68.     // Crop the region of interest
  69.     const x = Math.max(0, Math.floor(rotatedRect.center.x - rotatedRect.size.width / 2));
  70.     const y = Math.max(0, Math.floor(rotatedRect.center.y - rotatedRect.size.height / 2));
  71.     const width = Math.floor(rotatedRect.size.width);
  72.     const height = Math.floor(rotatedRect.size.height);
  73.     const safeWidth = Math.min(width, rotated.cols - x);
  74.     const safeHeight = Math.min(height, rotated.rows - y);
  75.  
  76.     if (safeWidth <= 0 || safeHeight <= 0) return;
  77.     const safeRect = new cv.Rect(x, y, safeWidth, safeHeight);
  78.     const cropped = rotated.roi(safeRect);
  79.  
  80.     // Apply Otsu binarization after cropping
  81.     const croppedGray = new cv.Mat();
  82.     const croppedBinary = new cv.Mat();
  83.     cv.cvtColor(cropped, croppedGray, cv.COLOR_RGBA2GRAY);
  84.     cv.threshold(croppedGray, croppedBinary, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU);
  85.     cv.bitwise_not(croppedBinary, croppedBinary);
  86.  
  87.     canvas.width = croppedBinary.cols;
  88.     canvas.height = croppedBinary.rows;
  89.     cv.imshow(canvas, croppedBinary);
  90.  
  91.     cropped.delete();
  92.     croppedGray.delete();
  93.     croppedBinary.delete();
  94.     M.delete();
  95.     rotated.delete();
  96.   } finally {
  97.     gray.delete();
  98.     binary.delete();
  99.     contours.delete();
  100.     hierarchy.delete();
  101.     if (largestContour) largestContour.delete();
  102.   }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement