I'm trying to create a convolution function but I'm having trouble during the access to the kernel data (cv::Mat).
I create the 3x3 kernel:
cv::Mat krn(3, 3, CV_32FC1);
krn.setTo(1);
krn = krn/9;
And I try to loop over it. Next the image
Mat will be the image to which I want to apply the convolution operator and output will be the result of convolution:
for (int r = 0; r < image.rows - krn.rows; ++r) {
for (int c = 0; c < image.cols - krn.cols; ++c) {
int sum = 0;
for (int rs = 0; rs < krn.rows; ++rs) {
for (int cs = 0; cs < krn.cols; ++cs) {
sum += krn.data[rs * krn.cols + cs] * image.data[(r + rs) * image.cols + c + cs];
}
}
output.data[(r+1)*src.cols + c + 1]=sum; // assuming 3x3 kernel
}
}
However the output is not as desired (only randomic black and white pixel). However, if I change my code this way:
for (int r = 0; r < image.rows - krn.rows; ++r) {
for (int c = 0; c < image.cols - krn.cols; ++c) {
int sum = 0;
for (int rs = 0; rs < krn.rows; ++rs) {
for (int cs = 0; cs < krn.cols; ++cs) {
sum += 0.11 * image.data[(r + rs) * image.cols + c + cs]; // CHANGE HERE
}
}
output.data[(r+1)*src.cols + c + 1]=sum; // assuming 3x3 kernel
}
}
Using 0.11
instead of the kernel values seems to give the correct output. For this reason I think I'm doing something wrong accessing the kernel's data.
PS: I cannot use krn.at<float>(rs,cs)
.
Thanks!
cv::Mat::data
is pointer of type uchar
.
By data[y * cols + x]
you access some byte of stored float
values in krn
. To get full float values use at
method template:
krn.at<float>(rs,cs)
Consider changing type of sum
variable to be real. Without this, you may lose partial results when calculating convolution .
So, if you cannot use at
, just read 4 bytes from data pointer:
float v = 0.0;
memcpy(&v, krn.data + (rs * krn.step + cs * sizeof(float)), 4);
step
- means total bytes occupied by one line in mat.
Instead of needlessly using memcpy
, you can just cast the pointer. I'll use a C-style cast because why not.
cv::Mat krn = 1 / (cv::Mat_<float>(3,3) <<
1, 2, 3,
4, 5, 6,
7, 8, 9);
for (int i = 0; i < krn.rows; i += 1)
{
for (int j = 0; j < krn.cols; j += 1)
{
// to see clearly what's happening
uint8_t *byteptr = krn.data + krn.step[0] * i + krn.step[1] * j;
float *floatptr = (float*) byteptr;
// or in one step:
float *floatptr = (float*) (krn.data + krn.step[0] * i + krn.step[1] * j);
cout << "krn_{" << (i+1) << "," << (j+1) << "} = " << (*floatptr) << endl;
}
}
krn_{1,1} = 1
krn_{1,2} = 0.5
krn_{1,3} = 0.333333
krn_{2,1} = 0.25
krn_{2,2} = 0.2
krn_{2,3} = 0.166667
krn_{3,1} = 0.142857
krn_{3,2} = 0.125
krn_{3,3} = 0.111111
Note that pointer arithmetic may not be obvious. if you have a uint8_t*
, adding 1
moves it by one uint8_t , and if you have a float*
, adding 1
moves it by one float which is four bytes.
Consult the documentation for details, which include information on the step[]
array that contains the strides/steps to calculate the offset given a tuple of indices into the matrix.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.