【发布时间】:2018-01-19 00:18:44
【问题描述】:
我将 RGB 图片转换为灰度。现在我正在尝试将高斯模糊滤镜应用于此灰度图像。这就是我最初访问图像的方式:
pDoc = GetDocument();
int iBitPerPixel = pDoc->_bmp->bitsperpixel; // used to see if grayscale(8 bits) or RGB (24 bits)
int iWidth = pDoc->_bmp->width;
int iHeight = pDoc->_bmp->height;
BYTE *pImg = pDoc->_bmp->point; // pointer used to point at pixels in the image
int Wp = iWidth;
const int area = iWidth * iHeight;
这是我用来将我的 RGB 图像转换为灰度的代码:
double r; // red pixel value
double g; // green pixel value
double b; // blue pixel value
int gray; // gray pixel value
// convert RGB values to grayscale at each pixel, then put in grayscale array
for (int i = 0; i < iHeight; i++)
for (int j = 0; j < iWidth; j++)
{
r = pImg[i*iWidth * 3 + j * 3 + 2];
g = pImg[i*iWidth * 3 + j * 3 + 1];
b = pImg[i*Wp + j * 3];
r = static_cast<double>(r) * 0.299;
g = static_cast<double>(g) * 0.587;
b = static_cast<double>(b) * 0.114;
gray = std::round(r + g + b);
pImg[i*iWidth * 3 + j * 3 + 2] = gray;
pImg[i*iWidth * 3 + j * 3 + 1] = gray;
pImg[i*Wp + j * 3] = gray;
}
}
然后这里是我尝试应用高斯模糊滤镜的地方:
// gaussian filter kernel
float gauss[3][3] = { {1, 2, 1},
{2, 4, 2},
{1, 2, 1} };
int convHeight; // height value of convolution filter, gaussian in this case
int convWidth; // width value of convolution filter, gaussian in this case
//////gaussian blur/////////
for (int i = 0; i < iHeight; i++) {
for (int j = 0; j < iWidth; j++) {
gaussPixel = 0;
for (int x = 0; x < convHeight; x++) {
for (int y = 0; y < convWidth; y++) {
//gaussPixel += OldImage[x - convWidth / 2 + i, y - convHeight / 2 + j] * gauss[i, j];
}
}
//NewImage[x, y] = gaussPixel;
}
}
我的一些问题如下:1)我不确定如何为其中一个高斯模糊内核正在查看图像之外的点并因此未查看像素和 2)创建条件我从 Visual Studio 收到一条错误消息,说“访问冲突读取位置 0x....”,我认为这与问题 1 有关。另外我不知道我将图像从 RGB 更改为灰度这一事实是否有任何区别以我在灰度图像上读取和写入像素值的方式。
非常感谢任何和所有帮助。
【问题讨论】:
标签: c++ visual-studio image-processing