I am working on image processing with C# and implementing integral histogram. I am not getting into the details, but assume that I have MxN matrix and each cell value is the sum of itself and its left and upper neighbor, minus left upper corner neighbor. This works fast but I want to make it faster for large images or for real time image processing performance.
matrix[i,j] += matrix[i-1,j] + matrix[i,j-1] - matrix[i-1,j-1];
The actual implementation is:
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
{
int left = 0, upper = 0, u_l_corner = 0;
if (j - 1 >= 0)
{
left = matrix[i, j - 1];
}
if (i - 1 >= 0)
{
upper = matrix[i - 1, j];
}
if (j - 1 >= 0 && i - 1 >= 0)
u_l_corner = matrix[i - 1, j - 1];
matrix[i, j] += left + upper - u_l_corner;
}
So the calculation is dependent on the previous values of cells. Therefore, it does not look like it can be implemented in parallel(at least to me). But still, just want to make sure before go any further..
Can this algorithm be implemented in parallel using Parallel.For or any other method in C#? If so a simple example is highly appreciated, but if not, I better work on to find a “parallel image histogram algorithm”, if any exists.
Thanks in advance.
As far as I’m concerned it is possible to make this algorithm parallel but i find no use of doing so if your matrix is relatively small ( time to process is less than few miliseconds ).
If you are very interested in making this algorithm parallel you could make this task splitted to exactly “j” tasks ( number of items in “y” axis ).
Key to doing so is starting first thread to calculate points in first row of this matrice ( [i, 0] ) then starting second thread delayed – second thread should chase first thread – must never overtake preceding thread.