Skip to content

Commit 972610f

Browse files
authored
Build the initial documentation site (#2088)
* Build initial documentation site * Address documentation review feedback * Clarify WebAssembly and native loading guidance
1 parent 2f749f5 commit 972610f

16 files changed

Lines changed: 521 additions & 116 deletions

File tree

.github/workflows/docfx.yml

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
name: DocFX
22

33
on:
4+
pull_request:
5+
paths:
6+
- "docs/**"
7+
- "src/OpenCvSharp/**"
8+
- ".github/workflows/docfx.yml"
49
push:
510
branches:
611
- main
712
workflow_dispatch:
813

914
concurrency:
10-
group: "pages"
15+
group: "docfx-${{ github.ref }}"
1116
cancel-in-progress: false
1217

1318
jobs:
@@ -45,15 +50,16 @@ jobs:
4550
path: docs/docfx/_site
4651

4752
deploy:
48-
needs: build
49-
permissions:
50-
pages: write
51-
id-token: write
52-
environment:
53-
name: github-pages
54-
url: ${{ steps.deployment.outputs.page_url }}
55-
runs-on: ubuntu-latest
56-
steps:
57-
- name: Deploy to GitHub Pages
58-
id: deployment
59-
uses: actions/deploy-pages@v5
53+
if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main'
54+
needs: build
55+
permissions:
56+
pages: write
57+
id-token: write
58+
environment:
59+
name: github-pages
60+
url: ${{ steps.deployment.outputs.page_url }}
61+
runs-on: ubuntu-latest
62+
steps:
63+
- name: Deploy to GitHub Pages
64+
id: deployment
65+
uses: actions/deploy-pages@v5

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ This profile is used by:
146146
- `OpenCvSharp5.official.runtime.linux-x64.slim`
147147

148148
## Usage
149-
For more details, refer to the **[samples](https://github.com/shimat/opencvsharp_samples/)** and **[Wiki](https://github.com/shimat/opencvsharp/wiki)** pages.
149+
For step-by-step guides, package selection, and troubleshooting, see the **[OpenCvSharp documentation](https://shimat.github.io/opencvsharp/)**. More complete programs are available in the **[samples repository](https://github.com/shimat/opencvsharp_samples/)**.
150150

151151
**Always remember to release Mat and other IDisposable resources using the `using` syntax:**
152152
```C#
@@ -193,8 +193,8 @@ https://github.com/shimat/opencvsharp_samples/
193193

194194
Interactive browser-based samples (Blazor WebAssembly) are maintained separately at https://github.com/shimat/opencvsharp_blazor_sample/, with a [live demo](https://shimat.github.io/opencvsharp_blazor_sample/).
195195

196-
## API Documents
197-
http://shimat.github.io/opencvsharp/api/OpenCvSharp.html
196+
## Documentation
197+
https://shimat.github.io/opencvsharp/
198198

199199
## NuGet
200200

docs/docfx/api/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ The API is organized into the following namespaces:
2121

2222
## Getting Started
2323

24-
See the [Introduction](../articles/intro.md) for getting started with OpenCvSharp.
24+
See the [guides overview](../articles/index.md) for getting started with OpenCvSharp.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Your First Application
2+
3+
This example reads an image, converts it to grayscale, and saves the result. It does not open a native window, so it also works with the Linux headless runtime package.
4+
5+
## Prepare an input image
6+
7+
Place a JPEG or PNG image named `input.jpg` in the project directory. Configure it to be copied to the output directory by adding the following item to the project file:
8+
9+
```xml
10+
<ItemGroup>
11+
<None Update="input.jpg" CopyToOutputDirectory="PreserveNewest" />
12+
</ItemGroup>
13+
```
14+
15+
## Add the code
16+
17+
Replace `Program.cs` with:
18+
19+
```csharp
20+
using OpenCvSharp;
21+
22+
using var source = Cv2.ImRead("input.jpg", ImreadModes.Color);
23+
if (source.Empty())
24+
{
25+
throw new InvalidOperationException("Could not read input.jpg.");
26+
}
27+
28+
using var grayscale = new Mat();
29+
Cv2.CvtColor(source, grayscale, ColorConversionCodes.BGR2GRAY);
30+
31+
if (!Cv2.ImWrite("output.png", grayscale))
32+
{
33+
throw new InvalidOperationException("Could not write output.png.");
34+
}
35+
36+
Console.WriteLine($"Created output.png ({grayscale.Width} x {grayscale.Height}).");
37+
```
38+
39+
## Run the application
40+
41+
```bash
42+
dotnet run
43+
```
44+
45+
The application creates `output.png` in its working directory.
46+
47+
## What the code does
48+
49+
- `Cv2.ImRead` decodes the input file into a `Mat`.
50+
- `source.Empty()` detects a missing or unsupported input image.
51+
- `Cv2.CvtColor` converts the image from OpenCV's default BGR channel order to grayscale.
52+
- `Cv2.ImWrite` selects an encoder from the output file extension and writes the image.
53+
- The `using` declarations release the native memory owned by both `Mat` instances.
54+
55+
OpenCvSharp objects that own native resources must be disposed. Read [Resource Management](../guides/resource-management.md) before building a long-running application.
56+
57+
## Next steps
58+
59+
- Browse the [OpenCvSharp API reference](xref:OpenCvSharp).
60+
- Explore the [OpenCvSharp sample projects](https://github.com/shimat/opencvsharp_samples).
61+
- Check [Native Library Loading](../troubleshooting/native-library-loading.md) if the application builds but fails at run time.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Installation
2+
3+
This page creates a .NET console application and installs the packages selected in [Choose a Version and Package](package-selection.md). The commands require the [.NET 8 SDK or later](https://dotnet.microsoft.com/download).
4+
5+
## Create a project
6+
7+
```bash
8+
dotnet new console -n OpenCvSharpExample
9+
cd OpenCvSharpExample
10+
```
11+
12+
## Windows x64
13+
14+
The all-in-one package is the simplest option for Windows x64:
15+
16+
```bash
17+
dotnet add package OpenCvSharp5.Windows
18+
```
19+
20+
For the reduced module set, use `OpenCvSharp5.Windows.Slim` instead.
21+
22+
## Windows ARM64
23+
24+
```bash
25+
dotnet add package OpenCvSharp5
26+
dotnet add package OpenCvSharp5.runtime.win-arm64
27+
```
28+
29+
Use `OpenCvSharp5.runtime.win-arm64.slim` instead of the full runtime when the reduced module set is sufficient.
30+
31+
## Linux x64
32+
33+
For a desktop application that uses native OpenCV windows:
34+
35+
```bash
36+
dotnet add package OpenCvSharp5
37+
dotnet add package OpenCvSharp5.official.runtime.linux-x64
38+
```
39+
40+
For a service or container that does not use `Cv2.ImShow`, `Cv2.WaitKey`, or other `highgui` APIs:
41+
42+
```bash
43+
dotnet add package OpenCvSharp5
44+
dotnet add package OpenCvSharp5.official.runtime.linux-x64.headless
45+
```
46+
47+
The full runtime requires GTK3. On a minimal Ubuntu or Debian installation, install it with:
48+
49+
```bash
50+
sudo apt-get update
51+
sudo apt-get install libgtk-3-0
52+
```
53+
54+
The official Linux x64 packages require glibc 2.28 or later.
55+
56+
## Linux ARM64
57+
58+
```bash
59+
dotnet add package OpenCvSharp5
60+
dotnet add package OpenCvSharp5.runtime.linux-arm64
61+
```
62+
63+
## macOS
64+
65+
For Apple Silicon:
66+
67+
```bash
68+
dotnet add package OpenCvSharp5
69+
dotnet add package OpenCvSharp5.runtime.osx.arm64
70+
```
71+
72+
For an Intel Mac:
73+
74+
```bash
75+
dotnet add package OpenCvSharp5
76+
dotnet add package OpenCvSharp5.runtime.osx.x64
77+
```
78+
79+
## WebAssembly
80+
81+
WebAssembly uses static native linking and requires a Blazor WebAssembly project rather than the console project created above. Install the WebAssembly build tools and create a standalone Blazor WebAssembly application:
82+
83+
```bash
84+
dotnet workload install wasm-tools
85+
dotnet new blazorwasm -n OpenCvSharpWasmExample
86+
cd OpenCvSharpWasmExample
87+
dotnet add package OpenCvSharp5
88+
dotnet add package OpenCvSharp5.runtime.wasm
89+
```
90+
91+
Add the following properties to the project file:
92+
93+
```xml
94+
<PropertyGroup>
95+
<WasmInitialHeapSize>268435456</WasmInitialHeapSize>
96+
<WasmAllowUndefinedSymbols>true</WasmAllowUndefinedSymbols>
97+
</PropertyGroup>
98+
```
99+
100+
The larger initial heap accommodates the statically linked OpenCV runtime. `WasmAllowUndefinedSymbols` is currently a package and toolchain-specific linker workaround: it allows the static link to complete even though the managed assembly declares APIs for OpenCV modules that are not included in the WebAssembly build. Calling one of those unavailable APIs will still fail at run time. This property does not configure exception handling; the runtime package supplies the native archive and its required `WasmEnableExceptionHandling` setting.
101+
102+
See the [OpenCvSharp Blazor sample](https://github.com/shimat/opencvsharp_blazor_sample) for a complete browser application.
103+
104+
## Verify the package references
105+
106+
```bash
107+
dotnet list package
108+
dotnet restore
109+
dotnet build
110+
```
111+
112+
The package list should contain `OpenCvSharp5` and exactly one runtime package for the deployment target, unless you selected a Windows convenience package.
113+
114+
## Next step
115+
116+
Continue to [Your First Application](first-application.md).
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Choose a Version and Package
2+
3+
OpenCvSharp uses separate managed and native packages. Every application needs the managed OpenCvSharp package and a native runtime package for its target operating system and architecture. The Windows convenience packages include both.
4+
5+
## Choose OpenCvSharp5 or OpenCvSharp4
6+
7+
| Package family | OpenCV version | .NET support | Recommended use |
8+
|---|---|---|---|
9+
| `OpenCvSharp5.*` | OpenCV 5.x | .NET 8 or later | New applications |
10+
| `OpenCvSharp4.*` | OpenCV 4.13 | .NET Framework 4.6.1 or later, .NET Standard 2.0/2.1, and .NET 8 or later | Existing applications and applications that require older .NET targets |
11+
12+
The examples in this documentation use OpenCvSharp5. Replace `OpenCvSharp5` with the corresponding `OpenCvSharp4` package only when your application must use the OpenCvSharp4 family.
13+
14+
## Choose a runtime package
15+
16+
| Target | Packages |
17+
|---|---|
18+
| Windows x64 | `OpenCvSharp5.Windows` |
19+
| Windows ARM64 | `OpenCvSharp5` and `OpenCvSharp5.runtime.win-arm64` |
20+
| Linux x64 with GUI support | `OpenCvSharp5` and `OpenCvSharp5.official.runtime.linux-x64` |
21+
| Linux x64 without GUI support | `OpenCvSharp5` and `OpenCvSharp5.official.runtime.linux-x64.headless` |
22+
| Linux x64 with a reduced module set | `OpenCvSharp5` and `OpenCvSharp5.official.runtime.linux-x64.slim` |
23+
| Linux ARM64 | `OpenCvSharp5` and `OpenCvSharp5.runtime.linux-arm64` |
24+
| macOS Intel | `OpenCvSharp5` and `OpenCvSharp5.runtime.osx.x64` |
25+
| macOS Apple Silicon | `OpenCvSharp5` and `OpenCvSharp5.runtime.osx.arm64` |
26+
| WebAssembly | `OpenCvSharp5` and `OpenCvSharp5.runtime.wasm` |
27+
28+
Use only one native runtime package for a given deployment target. The runtime package must match the operating system and process architecture.
29+
30+
## Full, headless, and slim Linux packages
31+
32+
The full Linux x64 package contains the complete module set, including `highgui`, and therefore uses GTK3 for APIs such as `Cv2.ImShow` and `Cv2.WaitKey`.
33+
34+
The headless package contains the same module set as the full package except for `highgui`. It is the usual choice for services and containers that need features such as `videoio`, DNN, ML, or contrib modules but do not display native windows.
35+
36+
The slim package has no GUI dependency and reduces the native module set. It omits contrib, DNN, `videoio`, and `highgui`, among other modules. Choose it only when the reduced feature set is sufficient.
37+
38+
## Windows convenience packages
39+
40+
`OpenCvSharp5.Windows` combines the managed library, Windows x64 native runtime, GDI+ extensions, and WPF extensions where applicable. `OpenCvSharp5.Windows.Slim` provides the corresponding reduced native module set.
41+
42+
For Windows ARM64, reference `OpenCvSharp5` and `OpenCvSharp5.runtime.win-arm64` separately. FFmpeg-based video I/O is not included in the Windows ARM64 runtime package.
43+
44+
## Next step
45+
46+
Continue to [Installation](installation.md).
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Resource Management
2+
3+
Many OpenCvSharp types wrap native OpenCV objects. Dispose objects that implement `IDisposable` as soon as they are no longer needed so that long-running applications do not retain native memory.
4+
5+
## Prefer using declarations
6+
7+
Use a `using` declaration for each `Mat` that owns a result:
8+
9+
```csharp
10+
using var source = Cv2.ImRead("input.jpg", ImreadModes.Color);
11+
using var blurred = new Mat();
12+
13+
Cv2.GaussianBlur(source, blurred, new Size(5, 5), 0);
14+
Cv2.ImWrite("blurred.png", blurred);
15+
```
16+
17+
The variables are disposed automatically at the end of the current scope.
18+
19+
## Dispose temporary results in loops
20+
21+
Objects created inside a loop should normally be disposed during each iteration:
22+
23+
```csharp
24+
foreach (var fileName in fileNames)
25+
{
26+
using var source = Cv2.ImRead(fileName);
27+
using var resized = new Mat();
28+
29+
Cv2.Resize(source, resized, new Size(640, 480));
30+
Cv2.ImWrite(Path.ChangeExtension(fileName, ".resized.jpg"), resized);
31+
}
32+
```
33+
34+
Waiting for garbage collection is not a substitute for deterministic disposal. The garbage collector tracks managed memory and does not have an accurate view of the native memory held by a `Mat`.
35+
36+
## Mat expressions
37+
38+
OpenCvSharp arithmetic operators build a managed `MatExpr` expression. Intermediate expression nodes do not own native resources. Dispose the input matrices and the materialized result:
39+
40+
```csharp
41+
using var source = Cv2.ImRead("input.jpg", ImreadModes.Grayscale);
42+
using Mat adjusted = 255 - source * 0.8;
43+
44+
Cv2.ImWrite("adjusted.png", adjusted);
45+
```
46+
47+
The intermediate `source * 0.8` expression does not need its own `using` declaration.
48+
49+
## Returning a Mat
50+
51+
Do not dispose a `Mat` before returning it to the caller. Transfer ownership explicitly through the method contract:
52+
53+
```csharp
54+
static Mat LoadGrayscale(string fileName)
55+
{
56+
using var source = Cv2.ImRead(fileName, ImreadModes.Color);
57+
var grayscale = new Mat();
58+
try
59+
{
60+
Cv2.CvtColor(source, grayscale, ColorConversionCodes.BGR2GRAY);
61+
return grayscale;
62+
}
63+
catch
64+
{
65+
grayscale.Dispose();
66+
throw;
67+
}
68+
}
69+
70+
using var image = LoadGrayscale("input.jpg");
71+
```
72+
73+
The caller owns and disposes the returned `Mat`. The method disposes the result itself if an exception occurs before ownership can be transferred to the caller.
74+
75+
## Windows and UI objects
76+
77+
`Window` and other OpenCvSharp wrappers that implement `IDisposable` follow the same rule. Scope them with `using` rather than relying on finalization.
78+
79+
## Related API
80+
81+
- [Mat](xref:OpenCvSharp.Mat)
82+
- [MatExpr](xref:OpenCvSharp.MatExpr)
83+
- [Cv2](xref:OpenCvSharp.Cv2)

0 commit comments

Comments
 (0)