// 索引图像转灰度图像 bool CImg::Index2Gray() { int i; if (!IsIndexedImg()) return false; RGBQUAD *table = (RGBQUAD*)m_lpvColorTable; m_pBMIH->biBitCount = 8; // 更新颜色数据 for (i=0; i<GetHeight(); i++) { for (int j=0; j<GetWidthPixel(); j++) { RGBQUAD rgb = *(table+GetGray(j, i)); BYTE gray = rgb.rgbBlue * 0.114 + rgb.rgbGreen * 0.587 + rgb.rgbRed * 0.299 + 0.5; SetPixel(j, i, RGB(gray, gray, gray)); } } // 更新颜色表 for (i=0; i<256; i++) { (table + i)->rgbBlue = i; (table + i)->rgbGreen = i; (table + i)->rgbRed = i; (table + i)->rgbReserved = 0; } m_nColorTableEntries = 256; return true; }
byte *tga_t::GrabData(byte *buff, int size) { if (bits == 32) return GetRGBA (buff, size); else if (bits == 24) return GetRGB (buff, size); else if (bits == 8) return GetGray (buff, size); return 0; }
CImg CImg::operator ! () { CImg grayRet = *this; grayRet.InitPixels(255); //结果图像置白 int nHeight = GetHeight(); int nWidth = GetWidthPixel(); int i,j; for(i=0; i<nHeight; i++) { for(j=0; j<nWidth; j++) { int pixel = 255 - GetGray(j, i); grayRet.SetPixel(j, i, RGB(pixel, pixel, pixel)); } } return grayRet; }
BOOL CImg::operator == (CImg& gray) { int nHeight = GetHeight(); int nWidth = GetWidthPixel(); if(nHeight != gray.GetHeight()) return false; if(nWidth != gray.GetWidthPixel()) return false; for(int i=0; i<nHeight; i++) { for(int j=0; j<nWidth; j++) { if( GetGray(j, i) != gray.GetGray(j, i) ) return false; } } return true; }
HRESULT CImage::GetGrayF(float x, float y, int nBand, BYTE* pGray, int nResampleMethod) { if(m_pGdalImage==NULL&&m_fpRaws==NULL) { return S_FALSE; } if(nResampleMethod == 0) { int nCol, nRow; nCol = int(x + 0.5); nRow = int(y + 0.5); if(nCol >= 0 && nCol < m_nCols && nRow >= 0 && nRow < m_nRows) { GetGray(nRow,nCol,nBand,pGray); } else { memset(pGray,0,m_nBPB); } } else { int nCol1, nCol2, nRow1, nRow2; float Dx, Dy; static BYTE pBuf11[512], pBuf12[512], pBuf21[512], pBuf22[512]; //, pBufDest[512]; nCol1 = int(x); nRow1 = int(y); nCol2 = nCol1 + 1; nRow2 = nRow1 + 1; //be sure the second pixel in image nCol2=__min(nCol2,m_nCols-1); nRow2=__min(nRow2,m_nRows-1); Dx = x - nCol1; Dy = y - nRow1; UINT nDataType; GetDataType(&nDataType); int nBPB; GetBPB(&nBPB); if(nRow1 >= 0 && nRow1 < m_nRows && nCol1 >= 0 && nCol1 < m_nCols) { GetGray(nRow1,nCol1,nBand,pBuf11); GetGray(nRow1,nCol2,nBand,pBuf21); GetGray(nRow2,nCol1,nBand,pBuf12); GetGray(nRow2,nCol2,nBand,pBuf22); (* FuncTable_GDAL[nDataType])(pBuf11, pBuf21, pBuf22, pBuf12, Dx,Dy, pGray); } else { memset(pGray,0,nBPB); } } return S_OK; }
/******************* CImg CImg::operator - (CImg gray) 功能:图像按位减 参数: CImg 对象 返回值: CImg: 相减后的 CImg 类图像对象 *******************/ CImg CImg::operator - (CImg &gray) { CImg grayRet = *this; //返回图像 //取得图像的高和宽 int nHeight = GetHeight(); int nWidth = GetWidthPixel(); int i, j;//循环变量 //不能在CImg类对象中直接进行像素相减,因为相减的结果可能小于0 vector< vector<int> > GrayMat;//相减后暂存图像的灰度点阵 vector<int> vecRow(nWidth, 0); //GrayMat中的一行(初始化为0) for(i=0; i<nHeight; i++) { GrayMat.push_back(vecRow); } //最大、最小灰度和值 int nMax = -255; int nMin = 255; //逐行扫描图像 for(i=0; i<nHeight; i++) { for(j=0; j<nWidth; j++) { //按位相加 GrayMat[i][j] = GetGray(j, i) - gray.GetGray(j, i); //统计最大、最小值 if( GrayMat[i][j] > nMax) nMax = GrayMat[i][j]; if( GrayMat[i][j] < nMin) nMin = GrayMat[i][j]; }// j }// i //将GrayMat的取值范围重新归一化到[0, 255] int nSpan = nMax - nMin; for(i=0; i<nHeight; i++) { for(j=0; j<nWidth; j++) { BYTE bt; if(nSpan > 0) bt = (GrayMat[i][j] - nMin)*255/nSpan; else if(GrayMat[i][j] <= 255) bt = GrayMat[i][j] ; else bt = 255; grayRet.SetPixel(j, i, RGB(bt, bt, bt)); }// for j }// for i return grayRet; }