// load format(imgW, i) and add information to the back of imgs and lens void CmIllustr::LoadImgs(CStr &imgW, vecM &imgs, vecD &lens, int W, int H) { bool toRow = W > H; double crnt = -space; if (imgs.size()){ // There exist a predefined image for sketch lens.push_back(toRow ? H*imgs[0].cols*1./imgs[0].rows : W*imgs[0].rows*1./imgs[0].cols); crnt += lens[0] + space; } for (int i = 0; i < 500; i++){ string imgN = format(_S(imgW), i), inDir, maskN; vecS names; int subN = CmFile::GetNames(imgN, names, inDir); if (subN == 0) continue; Mat img = imread(inDir + names[0]); if (img.data == NULL){ printf("Can't load image file %-70s\n", _S(names[0])); continue; } if (subN > 1){ Mat mask1u = imread(inDir + names[1], CV_LOAD_IMAGE_GRAYSCALE), big1u; dilate(mask1u, big1u, Mat(), Point(-1, -1), 5); bitwise_xor(mask1u, big1u, mask1u); img.setTo(Scalar(0, 0, 255), mask1u); } lens.push_back(toRow ? H*img.cols*1./img.rows : W*img.rows*1./img.cols); imgs.push_back(img); crnt += lens[lens.size() - 1] + space; if (crnt >= max(H, W)) break; } int num = imgs.size(); if (num && abs(crnt - max(H,W)) > abs(crnt - lens[num - 1] - space - max(H,W))) imgs.resize(num - 1), lens.resize(num - 1); printf("%s: %d\n", _S(imgW), num); if (crnt < max(H, W)) { printf(_S(imgW + ": not enough images\n")); exit(0); } }
Mat CmIllustr::ArrangeImgs(vecM &imgs, vecD &len, int W, int H, bool toRow) { int imgN = (int)(imgs.size()), s = 0; CV_Assert(len.size() == imgN); double ratio, sumL = 0, err = 0; for (int i = 0; i < imgN; i++) sumL += len[i]; ratio = ((toRow ? W : H) - (imgN - 1) * space) / sumL; Mat dstImg(H, W, CV_8UC3); dstImg = Scalar(255, 255, 255); for (int i = 0; i < imgN; i++) { len[i] *= ratio; int l = cvRound(len[i] + err); Rect reg = toRow ? Rect(s, 0, l, H) : Rect(0, s, W, l); resize(imgs[i], dstImg(reg), reg.size()); err = len[i] + err - l; s += l + space; } CV_Assert(s - space == (toRow ? dstImg.cols : dstImg.rows)); return dstImg; }