I have an image img
in c++ of size mxn
of type CV_8UC3
in OpenCV.
I have another vector b
of size 1xn
splitting img
"horizontally" into two parts:
upper_part = {(row,col)|1<=col<=n, 1<=row<=b(1,col)}
lower_part = {(row,col)|1<=col<=n, b(1,col)<row<=m}
,
where 1<=b(1,col)<=m (1<=col<=n).
For these two parts of the image I'd like to have the covariance matrices M_u
and M_l
sort of "per" channel. This means the resulting matrices should have size 3x3 and should be derived like:
M_u = 1/(N_u-1) * sum_{(row,col)\in upper_part} (img(row,col)-mu_u)*(img(row,col)-mu_u)^T
,
where N_u is the number of elements in the upper part, mu_u a 3x1 vector describing the average RGB values in the upper part and img(row,col)
is a 3x1 vector with the RGB values of img
at position (row,col)
. M_l
is calculated equivalently with N_l
and mu_l
considering the lower_part
.
Furthermore, I also (sometimes) have to calculate the covariance for an CV_8UC1
image. Of course then the matrix is just a scalar.
Is there a solution primarily for the CV_8UC3
type and if yes is there a solution which also works for the CV_8UC1
type image?
My current solution is to iterate over each pixel and calculate it by getting the values with img.at<Vec3b>(row,col)
or img.at<unsigned char>(row,col)
respectively (first for the mean, then for the covariance, thus two loops over all pixels), but what I've heard and now see is that this function is quite inefficient/slow. As I've to do this process of calculating M_u
and M_l
within a loop I'd like to derive the covariances efficiently.
Any ideas?
Thank you.
PS: m~1280
and n~960
.
.at<.,.>(.,.)
would be helpful. – SemtexB