Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 5, 2025

This PR implements the missing Mat.reshape() method that was declared in TypeScript types but not available at runtime, causing the error "img.reshape is not a function" when users tried to reshape matrices.

Problem

Users encountered this error when trying to use the reshape method:

const img = new cv.Mat()  
cv.cvtColor(origImg, img, cv.COLOR_RGBA2RGB )
const vectorized = img.reshape(-1, 3) // TypeError: img.reshape is not a function

The method was properly typed in src/types/opencv/Mat.ts but missing from the actual OpenCV.js runtime build.

Solution

Added a complete implementation of the reshape() method that follows OpenCV semantics:

  • Parameter cn: Number of channels in result matrix. Use -1 to auto-calculate based on total elements and rows
  • Parameter rows (optional): Number of rows in result matrix
  • Behavior: Reorganizes matrix data while preserving total data elements (rows × cols × channels)

Key Features

  • OpenCV-compatible semantics: Matches behavior of native OpenCV reshape function
  • Auto-calculation support: When cn = -1, intelligently determines optimal channel count (typically 1 for vectorization)
  • Memory efficient: Direct data copying without unnecessary allocations
  • Type safe: Full TypeScript support with proper error handling

Usage Examples

// Original failing code now works
const vectorized = img.reshape(-1, 3); // Auto-calc channels, 3 rows

// Explicit channel and row specification  
const singleChannel = img.reshape(1, 12); // 1 channel, 12 rows

// Channel-only reshape
const grayscale = img.reshape(1); // Convert to single channel

Implementation Details

  • Added src/mat-extensions.ts with the reshape implementation
  • Extended Mat prototype during OpenCV initialization
  • Comprehensive test suite covering various reshape scenarios
  • All existing tests continue to pass (no regressions)

The implementation handles common computer vision use cases like image vectorization, channel reorganization, and matrix flattening while maintaining full compatibility with existing code.

Fixes #53.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: ttt43ttt <132509+ttt43ttt@users.noreply.github.com>
Copilot AI changed the title [WIP] img.reshape is not a function Implement Mat.reshape() method to fix "img.reshape is not a function" error Sep 5, 2025
Copilot AI requested a review from ttt43ttt September 5, 2025 03:44
Copilot finished work on behalf of ttt43ttt September 5, 2025 03:44
@ttt43ttt
Copy link
Contributor

ttt43ttt commented Oct 7, 2025

@copilot is there a workaround for reshape purpose? If so, I'd like to remove the reshape TS declaration instead of implementing it in this package.

Copy link
Contributor Author

Copilot AI commented Oct 7, 2025

@copilot is there a workaround for reshape purpose? If so, I'd like to remove the reshape TS declaration instead of implementing it in this package.

Yes, there is a workaround, but it's quite cumbersome:

// Workaround: Manual reshape by creating new Mat and copying data
const origImg = new cv.Mat(4, 4, cv.CV_8UC3); // 48 elements total
const newMat = new cv.Mat(3, 16, cv.CV_8UC1);  // 3x16x1 = 48 elements

// Copy data element by element
const srcData = origImg.data;
const dstData = newMat.data;
for (let i = 0; i < srcData.length; i++) {
  dstData[i] = srcData[i];
}

Recommendation: Keep the implementation. Here's why:

  1. Native OpenCV.js doesn't have it - The reshape() method is declared in TypeScript types but not implemented in the OpenCV.js runtime
  2. Workaround is error-prone - Manual data copying requires calculating dimensions correctly and is verbose
  3. Common operation - Reshape is frequently needed for ML preprocessing, vectorization, and channel manipulation
  4. OpenCV compatibility - Users expect this method to exist as it does in Python/C++ OpenCV

If you remove the TypeScript declaration instead, users would lose type safety and documentation for a workaround they'd need to implement manually anyway.

Copilot finished work on behalf of ttt43ttt October 7, 2025 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

img.reshape is not a function

2 participants