Your First Application
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.
Prepare an input image
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:
<ItemGroup>
<None Update="input.jpg" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
Add the code
Replace Program.cs with:
using OpenCvSharp;
using var source = Cv2.ImRead("input.jpg", ImreadModes.Color);
if (source.Empty())
{
throw new IOException("Could not read input.jpg.");
}
using var grayscale = new Mat();
Cv2.CvtColor(source, grayscale, ColorConversionCodes.BGR2GRAY);
if (!Cv2.ImWrite("output.png", grayscale))
{
throw new IOException("Could not write output.png.");
}
Console.WriteLine($"Created output.png ({grayscale.Width} x {grayscale.Height}).");
Run the application
dotnet run
The application creates output.png in its working directory.
What the code does
Cv2.ImReaddecodes the input file into aMat.source.Empty()detects a missing or unsupported input image.Cv2.CvtColorconverts the image from OpenCV's default BGR channel order to grayscale.Cv2.ImWriteselects an encoder from the output file extension and writes the image.- The
usingdeclarations release the native memory owned by bothMatinstances.
OpenCvSharp objects that own native resources must be disposed. Read Resource Management before building a long-running application.
Next steps
- Browse the OpenCvSharp API reference.
- Explore the OpenCvSharp sample projects.
- Check Native Library Loading if the application builds but fails at run time.