matlab/matlab-agentic-toolkit / skills-catalog/image-processing-and-computer-vision/matlab-integrate-pytorch-vision
matlab-integrate-pytorch-vision
VerifiedCreates MATLAB interfaces to Python image processing and computer vision models from GitHub repositories or pip-installable packages using MPyReq. Use when asked to interface MATLAB with a Python CV/image model (segmentation, depth estimation, object detection, image generation, super-resolution, etc.), given a GitHub repo URL for an image/vision model, or asked to create an MPyReq demo for a deep-learning vision pipeline. Do NOT use for general-purpose Python-MATLAB interfacing, non-vision models (NLP, tabular, audio), model deployment/serving, or MATLAB-only image processing workflows.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 14.7 KB |
| manifest.yaml | config | 535 B |
| references/common_gotchas.md | markdown | 1.8 KB |
| references/demo-birefnet.md | markdown | 2.0 KB |
| references/demo-cellpose.md | markdown | 839 B |
| references/demo-depthpro.md | markdown | 1.8 KB |
| references/demo-sam2.md | markdown | 2.2 KB |
| references/demo-stardist.md | markdown | 2.8 KB |
| references/demo-vggt.md | markdown | 2.2 KB |
| references/image_conversion_patterns.md | markdown | 3.1 KB |
| references/python_matlab_conversions.md | markdown | 1.5 KB |
| references/readme.md | markdown | 2.3 KB |
references/image_conversion_patterns.md
View on GitHub# Image Data Conversion Patterns
## MATLAB image to Python tensor (basic)
```matlab
inp = im2single(im);
% Ensure 3-channel RGB
if ismatrix(inp)
inp = repmat(inp, [1 1 3]);
end
% Resize if needed
inp = imresize(inp, [H W]);
% Reshape to NCHW format for PyTorch: [batch, channels, height, width]
inp = reshape(inp, [1 size(inp)]);
inp = permute(inp, [1 4 2 3]);
inp = py.torch.tensor(inp).to("cuda");
```
## MATLAB image to Python tensor (with ImageNet normalization + center crop)
Many vision models (DINOv2, ResNet, ViT, etc.) expect ImageNet-normalized input:
```matlab
inp = im2single(im);
if ismatrix(inp)
inp = repmat(inp, [1 1 3]);
end
% Resize shortest side to 256, then center crop to 224x224
inp = imresize(inp, [256 256]);
offset = floor((256 - 224) / 2);
inp = inp(offset+1:offset+224, offset+1:offset+224, :);
% ImageNet normalization
mean_val = reshape(single([0.485, 0.456, 0.406]), [1 1 3]);
std_val = reshape(single([0.229, 0.224, 0.225]), [1 1 3]);
inp = (inp - mean_val) ./ std_val;
% Reshape to NCHW
inp = reshape(inp, [1 size(inp)]);
inp = permute(inp, [1 4 2 3]);
inp = py.torch.tensor(inp);
```
## Python tensor to MATLAB array (dimension reordering)
PyTorch uses NCHW (batch, channels, height, width). MATLAB expects H x W x C x B (batch last, channels second-to-last). Always permute outputs back to MATLAB ordering:
```matlab
% General pattern: cast to MATLAB, then permute NCHW to H x W x C x B
result_ml = double(result.cpu().detach().numpy()); % still NCHW
result_ml = permute(result_ml, [3 4 2 1]); % to H x W x C x B
% For single-image output (N=1), squeeze the batch dim after permuting
result_ml = squeeze(result_ml); % to H x W x C
% For feature vectors [N, D] to [D, N] (feature dim first, batch last)
featureVec = double(result.cpu().detach().numpy()); % [N, D]
featureVec = permute(featureVec, [2 1]); % to [D, N]
% For masks/segmentation [N, 1, H, W] to H x W x N
mask = double(result.cpu().detach().numpy());
mask = permute(mask, [3 4 2 1]); % to H x W x 1 x N
mask = squeeze(mask); % to H x W or H x W x N
mask = imresize(mask, size(im, [1 2]));
% For labeled output
labels = uint16(outputs{1});
% For depth maps [N, 1, H, W] to H x W
depth = double(result.cpu().detach().numpy());
depth = permute(depth, [3 4 2 1]);
depth = squeeze(depth);
% For bounding boxes -- Python typically returns [x1, y1, x2, y2] (corners)
% MATLAB expects M x 4 as [startX, startY, width, height] (xywh)
boxes = double(result.cpu().detach().numpy()); % [M, 4] as [x1, y1, x2, y2]
boxes = [boxes(:,1), boxes(:,2), ...
boxes(:,3)-boxes(:,1), boxes(:,4)-boxes(:,2)]; % to [startX, startY, w, h]
% For point clouds
wp = double(predictions{'world_points'}.cpu().detach().numpy());
```
## Display patterns
```matlab
% Overlay segmentation
imageshow(im, OverlayData=labels)
% Side-by-side comparison
imshowpair(im, mask, "montage");
% Depth visualization
imageshow(rescale(depth))
% Label overlay
imageshow(labeloverlay(im, mask))
% Point cloud
pcshow(xyz);
```
----
Copyright 2026 The MathWorks, Inc.
----
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deployment | Stars: 321k | Updated | Claude CodeCodex | Verified | Install |
| xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |