//================================================================================= /// GetPictures /// /// This function collects all the pictures the user chooses into a vector. //================================================================================= void CSLICSuperpixelsDlg::GetPictures(vector<string>& picvec) { CFileDialog cfd(TRUE,NULL,NULL,OFN_OVERWRITEPROMPT,L"*.*|*.*|",NULL); cfd.m_ofn.Flags |= OFN_ALLOWMULTISELECT; //cfd.PostMessage(WM_COMMAND, 40964, NULL); CString strFileNames; cfd.m_ofn.lpstrFile = strFileNames.GetBuffer(2048); cfd.m_ofn.nMaxFile = 2048; BOOL bResult = cfd.DoModal() == IDOK ? TRUE : FALSE; strFileNames.ReleaseBuffer(); //if(cfd.DoModal() == IDOK) if( bResult ) { POSITION pos = cfd.GetStartPosition(); while (pos) { CString imgFile = cfd.GetNextPathName(pos); PictureHandler ph; string name = ph.Wide2Narrow(imgFile.GetString()); picvec.push_back(name); } } else return; }
// ./Imgs/*.jpg ./Sal/ int main(int argc,char *argv[]) { CV_Assert(argc == 3); string imgW = argv[1], salDir = argv[2]; string imgDir, imgExt; vecS namesNE; CmFile::MkDir(salDir); int imgNum = CmFile::GetNamesNE(imgW, namesNE, imgDir, imgExt); for (int i = 0; i < imgNum; i++){ if (CmFile::FilesExist(salDir + namesNE[i] + "_FT.png")) continue; vector<UINT> img(0);// or UINT* imgBuffer; int width(0); int height(0); PictureHandler picHand; picHand.GetPictureBuffer(imgDir + namesNE[i] + imgExt, img, width, height ); int sz = width*height; Saliency sal; vector<double> salmap(0); sal.GetSaliencyMap(img, width, height, salmap, true); vector<UINT> outimg(sz); for( int i = 0; i < sz; i++ ){ int val = int(salmap[i] + 0.5); outimg[i] = val << 16 | val << 8 | val; } picHand.SavePicture(outimg, width, height, namesNE[i], salDir, 0, "_FT");// 0 is for BMP and 1 for JPEG) Mat sal1u = imread(salDir + namesNE[i] + "_FT.bmp", CV_LOAD_IMAGE_GRAYSCALE); imwrite(salDir + namesNE[i] + "_FT.png", sal1u); CmFile::RmFile(salDir + namesNE[i] + "_FT.bmp"); } }
//=========================================================================== /// OnBnClickedButtonCreatesuperpixels /// /// The main function //=========================================================================== void CSLICSuperpixelsDlg::OnBnClickedButtonCreatesuperpixels() { PictureHandler picHand; vector<string> picvec(0); picvec.resize(0); GetPictures(picvec);//user chooses one or more pictures string saveLocation = "C:\\rktemp\\"; BrowseForFolder(saveLocation); int numPics( picvec.size() ); //if(m_spcount < 0 || m_spcount < 20) m_spcount = 20; if(m_spcount < 0) m_spcount = 200; for( int k = 0; k < numPics; k++ ) { UINT* img = NULL; int width(0); int height(0); picHand.GetPictureBuffer( picvec[k], img, width, height ); int sz = width*height; if(m_spcount > sz) AfxMessageBox(L"Number of superpixels exceeds number of pixels in the image"); int* labels = new int[sz]; int numlabels(0); SLIC slic; slic.PerformSLICO_ForGivenK(img, width, height, labels, numlabels, m_spcount, m_compactness);//for a given number K of superpixels //slic.PerformSLICO_ForGivenStepSize(img, width, height, labels, numlabels, m_stepsize, m_compactness);//for a given grid step size //slic.DrawContoursAroundSegments(img, labels, width, height, 0);//for black contours around superpixels slic.DrawContoursAroundSegmentsTwoColors(img, labels, width, height);//for black-and-white contours around superpixels slic.SaveSuperpixelLabels(labels,width,height,picvec[k],saveLocation); if(labels) delete [] labels; picHand.SavePicture(img, width, height, picvec[k], saveLocation, 1, "_SLICO");// 0 is for BMP and 1 for JPEG) if(img) delete [] img; } AfxMessageBox(L"Done!", 0, 0); }
//=========================================================================== /// BrowseForFolder /// /// The main function //=========================================================================== bool CSLICSuperpixelsDlg::BrowseForFolder(string& folderpath) { IMalloc* pMalloc = 0; if(::SHGetMalloc(&pMalloc) != NOERROR) return false; BROWSEINFO bi; memset(&bi, 0, sizeof(bi)); bi.hwndOwner = m_hWnd; bi.lpszTitle = L"Please select a folder and press 'OK'."; LPITEMIDLIST pIDL = ::SHBrowseForFolder(&bi); if(pIDL == NULL) return false; TCHAR buffer[_MAX_PATH]; if(::SHGetPathFromIDList(pIDL, buffer) == 0) return false; PictureHandler pichand; folderpath = pichand.Wide2Narrow(buffer); folderpath.append("\\"); return true; }