How to use C language to realize BMP Image Edge Detection
This article mainly explains "how to use C language to achieve BMP image edge detection processing", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use C language to achieve BMP image edge detection processing"!
The details are as follows
Take Sobel operator as an example, the convolution code of other template operators is partly the same as Sobel operator. Such as: Gaussian operator, Laplace operator, etc.
# include # include int main (int* argc, char** argv) {FILE* fp = fopen (". / 01.bmp", "rb"); if (fp = 0) return 0; BITMAPFILEHEADER fileHead; fread (& fileHead, sizeof (BITMAPFILEHEADER), 1, fp); BITMAPINFOHEADER infoHead; fread (& infoHead, sizeof (BITMAPINFOHEADER), 1, fp); int width = infoHead.biWidth; int height = infoHead.biHeight; int biCount = infoHead.biBitCount; int lineByte = (biCount*width / 8 + 3) / 4 * 4; RGBQUAD* pColorTable PColorTable = new RGBQUAD [256]; fread (pColorTable, sizeof (RGBQUAD), 256, fp); unsigned char* pBmpBuf; pBmpBuf = new unsigned char [lineByte*height]; fread (pBmpBuf, lineByte*height, 1, fp); fclose (fp); / / New figure FILE* fop = fopen ("sobel.bmp", "wb"); if (fop = 0) return 0; unsigned char* pBmpBuf2; / / initialization pBmpBuf2 = new unsigned char [lineByte*height]; for (int I = 0; I
< height; ++i){ for (int j = 0; j < width; ++j){ *(pBmpBuf2 + i*lineByte + j) = *(pBmpBuf + i*lineByte + j); } } int ul, uc, ur, dl, dc, dr; int lu, lc, ld, ru, rc, rd; double hir, vec; for (int i = 1; i < height - 1; ++i){ for (int j = 1; j < width - 1; ++j){ // 垂直梯度算子:检测水平边缘 vec = 0; ul = *(pBmpBuf + (i + 1)*lineByte + (j - 1))*(-1); uc = *(pBmpBuf + (i + 1)*lineByte + j)*(-2); ur = *(pBmpBuf + (i + 1)*lineByte + j)*(-1); dl = *(pBmpBuf + (i - 1)*lineByte + (j - 1)) * 1; dc = *(pBmpBuf + (i - 1)*lineByte + j) * 2; dr = *(pBmpBuf + (i - 1)*lineByte + j) * 1; vec = ul + uc + ur + dl + dc + dr; // 水平梯度算子:检测垂直边缘 hir = 0; lu = *(pBmpBuf + (i + 1)*lineByte + (j - 1))*(-1); lc = *(pBmpBuf + (i - 0)*lineByte + (j - 1))*(-2); ld = *(pBmpBuf + (i - 1)*lineByte + (j - 1))*(-1); ru = *(pBmpBuf + (i + 1)*lineByte + (j + 1)) * 1; rc = *(pBmpBuf + (i - 0)*lineByte + (j + 1)) * 2; rd = *(pBmpBuf + (i - 1)*lineByte + (j + 1)) * 1; hir = lu + lc + ld + ru + rc + rd; *(pBmpBuf2+i*lineByte+j) = round(sqrt(hir*hir + vec*vec)); } } fwrite(&fileHead, sizeof(BITMAPFILEHEADER), 1, fop); fwrite(&infoHead, sizeof(BITMAPINFOHEADER), 1, fop); fwrite(pColorTable, sizeof(RGBQUAD), 256, fop); fwrite(pBmpBuf2, lineByte*height, 1, fop); fclose(fop); system("pause"); return 0;} 实验结果:
Thank you for your reading, the above is the content of "how to use C language to achieve BMP image edge detection processing". After the study of this article, I believe you have a deeper understanding of how to use C language to achieve BMP image edge detection processing, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!