image processing problem, using mfc GetDIBits, SetDIBits, Dilation algorithm doesn't properly work

-2

I need to make gray image Dilation function using mfc (visual studio 2019) But when I run the below code, output image doesn't appear properly. repeated images with vertical or diagonal lines in it. original dog image dog image after dilation process

input image is gray(but bmih.biBitCount = 24)

it's dialog based mfc. m_pic1 is variable of first picture control on the dialog. I load the dilated bitmap image using SetBitmap function on the second picture control (m_pic2 is it's variable)

void Ctest1Dlg::OnBnClickedDilation()
{  
  //TODO: dilation
    CRect rc;
    m_pic1.GetClientRect(&rc);

    CDC* pDC = m_pic1.GetDC();
    HDC hDC = pDC->m_hDC;
    hMemDC = CreateCompatibleDC(hDC);
    SetBitmap1Info();  // set bmfh, bmih

    // extract bitmap data
    // https://infototal.tistory.com/entry/GetDIBits
    //GetDIBits(hDC, hBitmap, 0, bit.bmHeight, NULL, (LPBITMAPINFO)&bmih, DIB_RGB_COLORS);

    lpBits = new BYTE[bit.bmWidthBytes * bit.bmHeight]();  
    GetDIBits(hDC, hBitmap, 0, bit.bmHeight, lpBits, (LPBITMAPINFO)&bmih, DIB_RGB_COLORS);
    ReleaseDC(pDC);

    COLORREF* pPixel = NULL;
    COLORREF* pdPixel = NULL;   

    LPBYTE dilationlpBits = new BYTE[bit.bmWidthBytes * bit.bmHeight]();  

    for (int y = 1; y < bmih.biHeight-1 ; ++y)
    {
        pdPixel = (COLORREF*)dilationlpBits + y * bmih.biWidth;
        //printf("%ld \n", pdPixel);   //1390949108
        pPixel = (COLORREF*)lpBits + y * bmih.biWidth;
        for (int x = 1; x < bmih.biWidth-1 ; ++x)
        {
            COLORREF biggestVal = 0x00000000;
            for (int h = -1; h < 2; h++)
            {
                pPixel = (COLORREF*)lpBits + (y + h) * bmih.biWidth;
                for (int w = -1; w < 2; w++)
                {
                    if (biggestVal < *(pPixel + x + w))
                        biggestVal = *(pPixel + x + w);
                }
            }   
            *(pdPixel + x) = biggestVal;
        }

    }


    pDC = m_pic2.GetDC();
    hDC = pDC->m_hDC;


    HBITMAP hDilationBitmap = CreateCompatibleBitmap(hDC, bmih.biWidth, bmih.biHeight);

    int success = SetDIBits(hDC, hDilationBitmap, 0, bmih.biHeight, dilationlpBits, (LPBITMAPINFO)&bmih, DIB_RGB_COLORS);
    if (success == 0)
        MessageBox(_T("SetDIBits fail!"));
    //SetDIBitsToDevice(hDC, 0, 0, bmih.biWidth, bmih.biHeight, 0, 0, 0, bmih.biHeight, dilatedlpBits, (LPBITMAPINFO)&bmih, DIB_RGB_COLORS);
    m_pic2.SetBitmap(hDilationBitmap);
    delete[] dilationlpBits;
}
c++
image-processing
mfc
getdibits
dilation
asked on Stack Overflow Jan 15, 2020 by LakesideStar • edited Jan 15, 2020 by LakesideStar

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0