µ±Ç°Î»ÖÃ: Ê×Ò³ >> ³ÌÐòÉè¼Æ >> Êý¾Ý½á¹¹ºÍËã·¨ >> »ùÓڸĽøµÄÑÕɫֱ·½Í¼µÄͼÏñ¼ìË÷Ëã·¨
 

»ùÓڸĽøµÄÑÕɫֱ·½Í¼µÄͼÏñ¼ìË÷Ëã·¨

×÷Õߣº      À´Ô´£ºblog.csdn.net/tember/archive/2006/06/05/773456.asp     ·¢±íʱ¼ä£º2006-06-05     ä¯ÀÀ´ÎÊý£º      ×ֺţº´ó  ÖР Ð¡

ÖйúÔ´ÂëÍøÄÚÏà¹ØÖ÷ÌâÁ´½Ó
  • »ùÓڸĽøµÄÑÕɫֱ·½Í¼µÄͼÏñ¼ì...
  •  

    ¸½Â¼

    Ö÷Òªº¯Êý£º

    // pictureÖÐÏÔʾͼƬ

    void CImageRetrievalDlg::ShowPic(CString pathfile,int idc)

    {    

           CBitmap hbmp;

           HBITMAP hbitmap;        

           //½«pStaticÖ¸ÏòÒªÏÔʾµÄµØ·½     

           CStatic *pStaic; 

           pStaic=(CStatic*)GetDlgItem(idc);

           //×°ÔØ×ÊÔ´

           hbitmap=(HBITMAP)::LoadImage (::AfxGetInstanceHandle(),pathfile,                  IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION); 

           hbmp.Attach(hbitmap);      

           //»ñȡͼƬ¸ñʽ 

           BITMAP bm; 

           hbmp.GetBitmap(&bm);   

           //´´½¨ÁÙʱµÄÄÚ´æDC¶ÔÏó

           CDC dcMem;     

           dcMem.CreateCompatibleDC(GetDC());      

           CBitmap *poldBitmap=(CBitmap*)dcMem.SelectObject(hbmp);            

           CRect lRect; 

           pStaic->GetClientRect(&lRect);      

           lRect.NormalizeRect();

                  //ÏÔʾλͼ  

           pStaic->GetDC()->StretchBlt(lRect.left ,lRect.top ,lRect.Width(),lRect.Height(),           

                  &dcMem,0 ,0,bm.bmWidth,bm.bmHeight,SRCCOPY);     

           dcMem.SelectObject(&poldBitmap);

       

           pStaic->ReleaseDC(&dcMem);

      

    }

    //´ò¿ª¹Ø¼üͼ

    void CImageRetrievalDlg::OnOpenImage()

    {

           CFileOpen fileOpenDlg(TRUE);

           if (fileOpenDlg.DoModal () != IDOK)   return;         

           //ʹ²Ëµ¥¿ÉÓÃ

           CMenu*  mmenu  =  GetMenu();   

        mmenu->EnableMenuItem(ID_START_RETRIEVAL, 0);

           mmenu->EnableMenuItem(ID_STOP_RETRIEVAL, 0 );

           mmenu->EnableMenuItem(ID_SHOW_IM, 0 );

           mmenu->EnableMenuItem(ID_SHOW_IM2, 0 );

           mmenu->EnableMenuItem(ID_SHOW_INFO, 0 );

        // »­¸ñ×Ó

           CWnd* pWnd = GetDlgItem(IDC_VIEW1);

           CDC* pDC = pWnd->GetDC();

           pWnd->Invalidate();

           pWnd->UpdateWindow();       

           CPen* pPenRed = new CPen;

           pPenRed->CreatePen(PS_SOLID, 1, RGB(255,0,0));

           CPen* pOldPen = pDC->SelectObject(pPenRed);     

    /*    pDC->MoveTo(1,1);        //PictureBox ¿í194¸ß172

           pDC->LineTo(1, 172);

           pDC->MoveTo(1,86);

           pDC->LineTo(211,86);

           pDC->MoveTo(97,1);

           pDC->LineTo(97,190);

           pDC->MoveTo(48,1);

           pDC->LineTo(48,190);

           pDC->MoveTo(145,1);

           pDC->LineTo(145,190);

           pDC->MoveTo(1,43);

           pDC->LineTo(211,43);

           pDC->MoveTo(1,129);

           pDC->LineTo(211,129);*/

           //È¡µÃÎļþ·¾¶

           POSITION pos = fileOpenDlg.GetStartPosition();

           strfile = fileOpenDlg.GetNextPathName(pos);

           ShowPic(strfile,IDC_VIEW1); 

    }

    //Ñ¡ÔñͼÏñ¿âĿ¼

    void CImageRetrievalDlg::OnMutiImageIndb()

    {

           CString m_strPath;     

           tempi=0;

           //´ò¿ªÍ¨ÓöԻ°¿ò

           BROWSEINFO browse;

           ZeroMemory(&browse,sizeof(browse));//fills a block of memory with zeros.

           browse.hwndOwner = NULL;

           browse.pszDisplayName = m_strPath.GetBuffer(MAX_PATH);

           browse.lpszTitle = "ÇëÑ¡ÔñÒ»¸öͼÏñĿ¼";

           LPITEMIDLIST lpItem = SHBrowseForFolder(&browse);

           if(lpItem == NULL) return ;

           m_strPath.ReleaseBuffer();

           if(SHGetPathFromIDList(lpItem,m_strPath.GetBuffer(MAX_PATH)) == false) return;

           m_strPath.ReleaseBuffer();

           AfxMessageBox("ÄúÑ¡ÔñµÄĿ¼Ϊ:"+m_strPath,MB_ICONINFORMATION|MB_OK); 

           //µÃµ½Í¼ÏñĿ¼ÏÂÎļþµÄ·¾¶

           CString tempath;

           CString temps;

           tempath=m_strPath;

           tempath.TrimRight();tempath.TrimLeft(); //È¥³ýǰºó¶àÓà

           CString strfilepath=tempath;

           counts=0;

           StartDir(strfilepath);

        temps.Format("¸ÃĿ¼Ï¹²ÓÐ%d·ùͼÏñ!",counts);

        AfxMessageBox(temps,MB_ICONINFORMATION|MB_OK);

           //for (int i = 0; i<counts; i++)

           //{

           //     AfxMessageBox(temp[i],MB_ICONINFORMATION|MB_OK);

           //}

    }

    //¶ÔËùÓÐĿ¼²éÕÒ,È¡µÃͼÏñĿ¼ÏÂÎļþ·¾¶

    void CImageRetrievalDlg::StartDir(const CString& strfile1)

    {

           BOOL yesno;

           CFileFind find;     

           char tempFileFind[200];

           sprintf(tempFileFind,"%s\\*.*",strfile1);   

           RunDir(strfile1);

           yesno = (BOOL)find.FindFile(tempFileFind);

           //²éÕÒϼ¶Ä¿Â¼

           while(yesno)

           {

                  yesno = find.FindNextFile();

                  if (find.IsDots() != TRUE)

                  {

                         char foundFileName[200];

                         strcpy(foundFileName,find.GetFileName().GetBuffer(200));

                        

                         if((find.IsDirectory() == TRUE))

                         {

                                char tempDir[200];

                                sprintf(tempDir,"%s\\%s",strfile1,foundFileName);

                                // µÝ¹éµ÷Óà      

                                StartDir(tempDir);                   

                         }                 

                  }

           }  

           find.Close();

           return;

    }

    //¶Ô*.bmpÎļþ²éÕÒ

    void CImageRetrievalDlg::RunDir(const CString& strfile2)

    {    

           BOOL yesno;     

           CFileFind find;

           char tempFileFind[200];

           sprintf(tempFileFind,"%s\\*.bmp",strfile2);

           yesno = find.FindFile(tempFileFind);

           while(yesno)

           {

                  yesno = find.FindNextFile();

                  char foundFileName[200];

                  strcpy(foundFileName,find.GetFileName().GetBuffer(200));

                  if(!find.IsDots())

                  {

                         char tempFileName[200];

                         sprintf(tempFileName,"%s\\%s",strfile2,foundFileName);

                CString strfilepath1;

                         strfilepath1.Format("%s",tempFileName);

                         counts++;

                        

                         temp[tempi] = new CString(strfilepath1);

                         tempi++;                    

                  }

           }

           find.Close();

           return;

    }

    //µ¥¸öͼÏñÌØÕ÷ÏòÁ¿

    void CImageRetrievalDlg::OnSingleImageIndb()

    {

           CFileOpen fileOpenDlg1(TRUE);

           if (fileOpenDlg1.DoModal () != IDOK)  return;         

           //È¡µÃÎļþ·¾¶

           POSITION pos = fileOpenDlg1.GetStartPosition();

           strfile = fileOpenDlg1.GetNextPathName(pos);

          

           AfxMessageBox(strfile);    

    }

    //ÏÔʾ¹Ø¼üͼµÄÖ±·½Í¼

    void CImageRetrievalDlg::OnShowIm()

    {

           CString imagefilepath = strfile;

        //if (imagefilepath = ' ') {AfxMessageBox("ÇëÑ¡ÔñÒ»·ù¹Ø¼üͼ");return;}

           CalculateColor(imagefilepath);   

        HistogramShow(IDC_DLG_HIST_SHOW);

    }

    //¼ÆËãÑÕɫֱ·½Í¼

    void CImageRetrievalDlg::CalculateColor(CString pathfile)

    {

           CBitmap hbmp;

           HBITMAP hbitmap;

       int R,G,B,H,S,V;

           int i,j;     

           hbitmap=(HBITMAP)::LoadImage (::AfxGetInstanceHandle(),pathfile,           IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);  

           hbmp.Attach(hbitmap);      

           BITMAP bm; 

           hbmp.GetBitmap(&bm);   

           CDC dcMem;     

           dcMem.CreateCompatibleDC(GetDC());      

           dcMem.SelectObject(hbmp);    

           int width=bm.bmWidth/4;

           int height=bm.bmHeight/4;

           COLORREF clr; //¶¨ÒåÒ»¸öCOLORREF½á¹¹£¬ÒòΪÌáÈ¡µÄÏóËØµãµÄÑÕÉ«ÊÇÒÔRGBÐÎʽ±íʾµÄ

                  int x,y;

           for(int fda=0;fda<4;fda++)

                  for(int sda=0;sda<4;sda++)

                         for(int tda=0;tda<256;tda++){

                                Ha[fda][sda][tda]=0;

                         }

           for(int fdb=0;fdb<4;fdb++)

                  for(int sdb=0;sdb<4;sdb++)

                         for(int tdb=0;tdb<256;tdb++){

                                Sa[fdb][sdb][tdb]=0;

                         }

           for(int fdc=0;fdc<4;fdc++)

                  for(int sdc=0;sdc<4;sdc++)

                         for(int tdc=0;tdc<256;tdc++){

                                Va[fdc][sdc][tdc]=0;

                         }

           for( i=0;i<4;i++)

                  for( j=0;j<4;j++)

                         for(int k=0;k<width;k++)

                                for(int l=0;l<height;l++){

                                       x=j*width+l;

                                       y=i*height+k;

                                       clr=dcMem.GetPixel(x,y);

                                       R=GetRValue(clr);

                                       G=GetGValue(clr);

                                       B=GetBValue(clr);

                                       //RGBתHSV

                                       int maxp=max(R,G);

                                       int max=max(maxp,B); 

                                       int minp=min(R,G);

                                       int min=min(minp,B);

                                       if(max!=0) {S=(max-min)/max;} else {S=0;H=-1;break;}  //±¥ºÍ¶È

                                       if(max==min) {break;}

                                       if (R == max)                            

                                              H = (G-B)/(max-min);

                                       else if (G == max)

                                              H = 2 + (B-R)/(max-min);                                                                                      else

                                              H = 4 + (R-G)/(max-min); 

                                       H = H * 60 ;

                                       if (H < 0)

                                              H = H + 360 ; //É«²Ê¶È

                                       V=max;            //ÁÁ¶È  

                                      

                                       Ha[i][j][H]++;

                        Sa[i][j][S]++;                   

                                       Va[i][j][V]++;

                                }

    }

    //»æÖÃÖ±·½Í¼

    void CImageRetrievalDlg::HistogramShow(int idc)

    {           

           int m;

                  CWnd* pWnd = GetDlgItem(idc);// »ñÈ¡»æÖÆÖ±·½Í¼Îı¾¿òµÄ±êÇ©

                  // »ñÈ¡É豸ÉÏÏÂÎÄ

           CDC* pDC = pWnd->GetDC();

           pWnd->Invalidate();

           pWnd->UpdateWindow();

                  pDC->Rectangle(0, 0, 367,338);     

                  CPen* pPenRed = new CPen;// ´´½¨»­±Ê¶ÔÏó  

                  pPenRed->CreatePen(PS_SOLID, 1, RGB(255,0,0));// ´´½¨ºìÉ«»­±Ê£¨ÓÃÓÚ»æÖÆ×ø±êÖᣩ      

                  CPen* pOldPen = pDC->SelectObject(pPenRed);// Ñ¡ÈëºìÉ«»­±Ê£¬²¢±£´æÒÔǰµÄ»­±Ê    

                  pDC->MoveTo(40,40);// »æÖÆ×ø±êÖá 

                  pDC->LineTo(40, 300);// »æÖÆ´¹Ö±Öá 

                  pDC->LineTo(340, 300);// »æÖÆË®Æ½Öá

                  // »æÖÆXÖá¿Ì¶ÈÖµ

           CString strTemp;

           strTemp.Format("0");

           pDC->TextOut(40, 303, strTemp);

           strTemp.Format("50");

           pDC->TextOut(90, 303, strTemp);

           strTemp.Format("100");

           pDC->TextOut(140, 303, strTemp);

           strTemp.Format("150");

           pDC->TextOut(190, 303, strTemp);

           strTemp.Format("200");

           pDC->TextOut(240, 303, strTemp);

           strTemp.Format("255");

           pDC->TextOut(295, 303, strTemp);

                  // »æÖÆXÖá¿Ì¶È

           for (m = 0; m < 256; m += 5)

           {

                  if ((m & 1) == 0)

                  {

                         // 10µÄ±¶Êý

                         pDC->MoveTo(m + 40, 303);

                         pDC->LineTo(m + 40, 307);

                  }

                  else

                  {

                         // 5µÄÆæÊý±¶Êý

                         pDC->MoveTo(m + 40, 303);

                         pDC->LineTo(m + 40, 310);

                  }

           }

                  // »æÖÆXÖá¼ýÍ·

           pDC->MoveTo(335,295);

           pDC->LineTo(340,300);

           pDC->LineTo(335,305);

                  // »æÖÆYÖá¼ýÍ·

           pDC->MoveTo(40,40);

           pDC->LineTo(35,45);

           pDC->MoveTo(40,40);

           pDC->LineTo(45,45);

          

           LONG lMaxCount = 0;// Ö±·½Í¼ÖÐ×î´ó¼ÆÊýÖµ

                  // ¼ÆËã×î´ó¼ÆÊýÖµ

           for (m = 0; m <= 255; m ++)

           {           

                  // ÅжÏÊÇ·ñ´óÓÚµ±Ç°×î´óÖµ

                  if (Ha[1][1][m] > lMaxCount)