shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.22k stars 1.13k forks source link

Image.Set not working correctly #1564

Closed Kjelldor closed 12 months ago

Kjelldor commented 1 year ago

Summary of your issue

I am trying to add white pixels to an in-memory Mat, but instead it seems to add the RGB values spread out over the image.

Environment

Windows 11 pro Version 10.022621 AMD Ryzen 9 5900X GTX 3080 Ti

What did you do when you faced the problem?

I attempted to check whether or not my code was right by creating a single pixel example on a black canvas. This produced the same error, however.

Example code:

// Create an empty image of 20 by 20 with a black background. Mat image = new Mat(new Size(20, 20), MatType.CV_8UC3, new Scalar(0, 0, 0)); // Create a completely white brush var color = new Scalar(255, 255, 255); // Set the pixel of the image on coordinate 10,10 to white. image.Set(10, 10, color); // Save the image to disk Cv2.ImWrite("Test.jpg", image);

Output:

test

I apologize for the small size, however, by keeping it this small and only referencing a single pixel, I feel like the idea comes across better.

What did you intend to be?

I intended it to be a black square with a single white pixel, like so: image

this was made with opencv in python, though also a different implementation, the issue doesnt seem to be with opencv itself.

BalashovK commented 1 year ago

You are absolutely right.

Setting scalar produces funky result, exaggerated by saving to .jpg (saving to .png would give a better clue)

Replacing Scalar with Vec3b resolves the problem.

//var color = new Scalar(255, 255, 255); Vec3b color = new Vec3b(255, 255, 255);

Saved image looks exactly like your intended result.

Kjelldor commented 12 months ago

Thanks! This has indeed solved my issue.