I would like to accumulate grayscale images in C# with opencv. Therefore I used this code so far:
// Convert to gray image first
Mat dst = new Mat();
Cv2.CvtColor(imageMat, dst, ColorConversionCodes.BGR2GRAY);
var numOfAverage = 5;
for (int i = 0; i < numOfAverage; i++)
{
Cv2.Accumulate(dst, acc, 1);
}
I think the problem is exactly the “1” which stands for mask. I thought it is an optional parameter but I do get an error if I do not use it.
So how would you set this parameter?
I would simply average over a number n images and get the averaged image back.
Mat bmAccu = new Mat();
Mat mask = new Mat();
var numOfAverage = 5;
for (int i = 0; i < numOfAverage; i++)
Cv2.Accumulate(bmDenoise, bmAccu, mask);
bmAccu = bmAccu / numOfAverage;
So when changing the dst to the same size as src like this:
Mat bmAccu = new Mat(bmDenoise.Rows, bmDenoise.Cols, MatType.CV_8UC1);
Mat mask = new Mat();
var numOfAverage = 5;
for (int i = 0; i < numOfAverage; i++)
Cv2.Accumulate(bmDenoise, bmAccu, mask);
dst: Accumulator image with the same number of channels as input image, and a depth of CV_32F or CV_64F.
there is probably no “accumulation function” to accumulate into 8UC1. I can see why they’d focus on 32F and 64F accumulators only but it’s slightly disappointing.
I accumulate 5 images. Here is the result of the input image (bmDenoise) and the accumulated image:
// Accumulate images
Mat bmAccu = new Mat(bmDenoise.Rows, bmDenoise.Cols, MatType.CV_32F);
Mat mask = new Mat();
var numOfAverage = 5;
for (int i = 0; i < numOfAverage; i++)
Cv2.Accumulate(bmDenoise, bmAccu, mask);
bmAccu = bmAccu / numOfAverage;
Changed the code accordingly. With dividing by 255 I do get an image but I can not see any averaging at all. Here the comparison between the incoming (Original) image and the averaged image:
// Accumulate images
Mat bmAccu = Mat.Zeros(640, 512, (MatType)MatType.CV_32F);
Mat mask = new Mat();
for (int i = 0; i < numOfAverage; i++)
Cv2.Accumulate(imageMat, bmAccu, mask);
bmAccu /= numOfAverage; // average by 20
Cv2.ImShow("Original", imageMat);
Cv2.ImShow("Average", bmAccu / 255);
Cv2.WaitKey(1);
At this point, you should pursue structured learning materials, perhaps take a class with a teacher. This self-directed ad hoc approach is not appropriate for your current level of expertise.