I am working on a C++ implement of Felzenszwalb segmentation algorithm and now I am in trouble with the Gaussian convolution. What I need is a convolution which keeps precision after decimal but so far, the program still seems to cut away numbers after points even if the Mat
was converted to float
. Here is my code:
#include <opencv2/opencv.hpp>
int main(int argc, char **argv)
{
if (argc != 3) {
std::cout << "Usage: ./a.out image sigma ..." << std::endl;
return -1;
}
double sigma = std::stod(argv[2]);
cv::Mat image = cv::imread(argv[1], cv::IMREAD_COLOR);
cv::Mat f_image;
cv::Mat f_filtered;
image.convertTo(f_image, CV_32FC3);
// cv::GaussianBlur(f_image, f_filtered, cv::Size(9, 9), sigma);
cv::Mat kernel_1D = cv::getGaussianKernel(9, sigma);
cv::sepFilter2D(f_image, f_filtered, -1, kernel_1D, kernel_1D);
std::cout << f_filtered.type() << std::endl;
for (int i = 0; i != f_filtered.rows; ++i) {
for (int j = 0; j != f_filtered.cols; ++j) {
std::cout << f_filtered.at<cv::Vec3b>(i, j) << std::endl;
}
}
My first try was use cv::GaussianBlur
directly (which was commented out above), although the type of f_filtered
was 21
(from this table it should be a 32 bit float) but the following loop output something like
21
...
[137, 231, 66]
[154, 231, 248]
[65, 10, 102]
[62, 65, 101]
[201, 228, 66]
[91, 136, 246]
[65, 119, 226]
[66, 65, 249]
[185, 229, 66]
[120, 233, 238]
[65, 137, 40]
[79, 65, 100]
[174, 234, 66]
[78, 194, 219]
[65, 136, 198]
[96, 65, 152]
[87, 245, 66]
[23, 101, 193]
...
And I do not really believe these are float numbers as there is not any decimal points. And after that I use cv::sepFilter2D
with cv::getGaussianKernel
but the result seems to be same. Another thing that convince me there are problems on data types as I have previously implemented this algorithm with Python. At that time I got into a quite similar trouble and I solved by converting data to float (img = img.astype(np.float)
) before (img = cv2.filter2D(img, ddepth=-1, kernel=gaussain_kernel)
). But this time type convert seems will not change result precision and I do not know what to do. So, any suggestions?