Mat CmShow::HistBins(CMat& color3f, CMat& val, CStr& title, bool descendShow, CMat &with) { // Prepare data int H = 300, spaceH = 6, barH = 10, n = color3f.cols; CV_Assert(color3f.size() == val.size() && color3f.rows == 1); Mat binVal1i, binColor3b, width1i; if (with.size() == val.size()) with.convertTo(width1i, CV_32S, 400/sum(with).val[0]); // Default shown width else width1i = Mat(1, n, CV_32S, Scalar(600.0/(val.cols*val.rows))); // Default bin width = 10 int W = cvRound(sum(width1i).val[0]); color3f.convertTo(binColor3b, CV_8UC3, 255); double maxVal, minVal; minMaxLoc(val, &minVal, &maxVal); val.convertTo(binVal1i, CV_32S, H/max(maxVal, -minVal)); Size szShow(W, H + spaceH + barH); szShow.height += minVal < 0 && !descendShow ? H + spaceH : 0; Mat showImg3b(szShow, CV_8UC3, WHITE); int* binH = (int*)(binVal1i.data); Vec3b* binColor = (Vec3b*)(binColor3b.data); int* binW = (int*)(width1i.data); vector<CostiIdx> costIdx(n); if (descendShow) { for (int i = 0; i < n; i++) costIdx[i] = make_pair(binH[i], i); sort(costIdx.begin(), costIdx.end(), std::greater<CostiIdx>()); } // Show image for (int i = 0, x = 0; i < n; i++) { int idx = descendShow ? costIdx[i].second : i; int h = descendShow ? abs(binH[idx]) : binH[idx]; Scalar color(binColor[idx]); Rect reg(x, H + spaceH, binW[idx], barH); showImg3b(reg) = color; // Draw bar rectangle(showImg3b, reg, BLACK); reg.height = abs(h); reg.y = h >= 0 ? H - h : H + 2 * spaceH + barH; showImg3b(reg) = color; rectangle(showImg3b, reg, BLACK); x += binW[idx]; } putText(showImg3b, format("Min = %g, Max = %g", minVal, maxVal), Point(5, 20), CV_FONT_HERSHEY_PLAIN, 1, CV_RGB(255,0,0)); SaveShow(showImg3b, title); return showImg3b; }
void CmIllustr::Imgs(const ImgsOptions &opts, int maxImgNum) { vecS names; CmFile::MkDir(opts._outDir); int imgNum = CmFile::GetNamesNE(opts._inDir + "*" + opts._exts[0], names); FILE *f = fopen(_S(opts._texName), "w"); CV_Assert(f != NULL); //* Sort image names in order vector<pair<int, string>> costIdx(imgNum); for (int i = 0; i < imgNum; i++) costIdx[i] = make_pair(atoi(_S(names[i])), names[i]); sort(costIdx.begin(), costIdx.end()); for (int i = 0; i < imgNum; i++) names[i] = costIdx[i].second; //*/ imgNum = min(imgNum, maxImgNum); vecI heights(imgNum); for (int i = 0; i < imgNum; i++){ Mat img = imread(opts._inDir + names[i] + opts._exts[0]); heights[i] = (img.rows * opts._w + img.cols/2) / img.cols; } vecS subNames; vecI subHeights; int height = -space; for (int i = 0; i < imgNum; i++) { height += heights[i] + space; subNames.push_back(names[i]); subHeights.push_back(heights[i]); if (height > opts._H){ height = 0; WriteFigure(subNames, f, subHeights, opts); subNames.clear(); subHeights.clear(); } } WriteFigure(subNames, f, subHeights, opts); fclose(f); printf("%70s\r", ""); }