【发布时间】:2020-06-21 11:46:18
【问题描述】:
我正在使用 C++ 中的 OpenCV 学习图像处理。为了实现一个基本的下采样算法,我需要在像素级别上工作——删除行和列。但是,当我使用 mat.at(i,j) 分配值时,会分配其他值 - 例如 1e-38。
代码如下:
Mat src, dst;
src = imread("diw3.jpg", CV_32F);//src is a 479x359 grayscale image
//dst will contain src low-pass-filtered I checked by displaying it works fine
Mat kernel;
kernel = Mat::ones(3, 3, CV_32F) / (float)(9);
filter2D(src, dst, -1, kernel, Point(-1, -1), 0, BORDER_DEFAULT);
// Now I try to remove half the rows/columns result is stored in downsampled
Mat downsampled = Mat::zeros(240, 180, CV_32F);
for (int i =0; i<downsampled.rows; i ++){
for (int j=0; j<downsampled.cols; j ++){
downsampled.at<uchar>(i,j) = dst.at<uchar>(2*i,2*j);
}
}
由于我在这里读到OpenCV outputing odd pixel values 需要转换 cout,所以我写了downsampled.at<uchar>(i,j) = (int) before dst.at<uchar> 但它也不起作用。
【问题讨论】:
-
首先,将
CV_32F作为imread的第二个参数传递没有任何意义。接下来,您将downsampled设为CV_32F,但由于某种原因,您以uchar的身份访问元素。这意味着你用其他东西覆盖了 32 位浮点数的四分之一位......垃圾是这样做的预期结果。 -
在您发表评论后,我已将下采样更改为 CV_U8,现在值在 (0, 255) 中。但是,当我尝试复制图像时,下采样中仅存在 dst 的左上四分之一
标签: c++ opencv pixel downsampling