[英](c++)(visual studio) Applying Gaussian Blur Filter to Grayscale Image From RGB
我將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更改為灰度的事實是否在方式上有所不同我在灰度圖像上讀寫像素值。
任何幫助都將不勝感激。
您不必檢查是否違反了圖像/陣列邊界。
您知道映像和內核的大小,因此您要做的就是選擇有效的索引。 如果圖像的寬度為200像素,則不應嘗試索引<0或> 199的x坐標。
這是圖像處理中的常見問題。 通常,您有兩個選擇。 假設我們有一個5x5內核:
根據過濾器的類型和您的應用程序,您可以選擇不同的方式來創建該邊距。 常數值,鏡像值,各種外推法...
我沒有對您的計算進行過多研究,但是您始終可以在線參考高斯實現的示例。
我也鼓勵您不要只寫一個高斯濾波器。 實現卷積,然后可以與任何高斯內核或其他內核一起使用。
還請記住,高斯濾波器是可分離的!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.