// Show a label map. labelNum: how many number of random colors used for show, use default colors if is -1 Mat CmShow::Label(CMat& label1i, CStr& title, int labelNum, bool showIdx) { bool useRandom = labelNum > 0; labelNum = useRandom ? labelNum : COLOR_NU_NO_GRAY; vector<Vec3b> colors(labelNum); if (useRandom) for (size_t i = 0; i < colors.size(); i++) colors[i] = RandomColor(); else for (size_t i = 0; i < colors.size(); i++) colors[i] = gColors[i]; Mat showImg = Mat::zeros(label1i.size(), CV_8UC3); for (int y = 0; y < label1i.rows; y++) { Vec3b* showD = showImg.ptr<Vec3b>(y); const int* label = label1i.ptr<int>(y); for (int x = 0; x < label1i.cols; x++) if (label[x] >= 0) { showD[x] = colors[label[x] % labelNum]; if (showIdx) showD[x][2] = (byte)(label[x]); } } SaveShow(showImg, title); return showImg; }
void CmShow::Pseudocolor(CMat& matfd1, CStr& title) { Mat hsvMat[3], hsvM; matfd1.convertTo(hsvMat[0], CV_32FC1, -240, 240); hsvMat[1] = hsvMat[2] = Mat::ones(matfd1.size(), CV_32F); merge(hsvMat, 3, hsvM); cvtColor(hsvM, hsvM, CV_HSV2BGR); SaveShow(hsvM, title); }
Mat CmShow::PntList(const PointSeti &pntList, Mat &showMat, CStr &title, int viewStep, int waite) { Vec3b color(50, 50, 255); for (size_t i = 0; i < pntList.size(); i++) { color[2] = (color[2] + 1)%256; if (color[2] == 0 && i != 0) { color[0] = rand()%255; color[1] = rand()%255; } showMat.at<Vec3b>(pntList[i]) = color; if (i % viewStep == viewStep - 1) { SaveShow(showMat, title); if (waite >=0) waitKey(waite); } } SaveShow(showMat, title); return showMat; }
void CmShow::showTinyMat(CStr &title, CMat &m) { int scale = 50, sz = m.rows * m.cols; while (sz > 200) { scale /= 2; sz /= 4; } Mat img; resize(m, img, Size(), scale, scale, CV_INTER_NN); if (img.channels() == 3) cvtColor(img, img, CV_RGB2BGR); SaveShow(img, title); }
void CmShow::mulChannelMat(CMat &mulChaMatNf, CStr &title, int numShow) { printf("An %dX%d mat with %d channels, with range:\n", mulChaMatNf.rows, mulChaMatNf.cols, mulChaMatNf.channels()); vecM mats; split(mulChaMatNf, mats); numShow = min((int)mats.size(), numShow); for (int i = 0; i < numShow; i++) { double minV, maxV; minMaxLoc(mats[i], &minV, &maxV); printf("\t%d[%g %g]\n", i, minV, maxV); normalize(mats[i], mats[i], 0, 1, NORM_MINMAX); SaveShow(mats[i], format(_S(title), i)); waitKey(1); } }
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 CmShow::ColorCircle(CStr &tile, int radius, int flag) { int diameter = radius * 2; float maxDist = (float)(radius * radius); Mat showImg = Mat::zeros(diameter, diameter, CV_8UC3); Ornt2HueFunc ornt2HueFunc = flag & ORNT2HUE_SYM4 ? Ornt2HueSym4 : Ornt2Hue; for (int r = 0; r < diameter; r++) { byte* showV = showImg.ptr<byte>(r); for (int c = 0; c < diameter; c++, showV += 3) { float x = (float)(c-radius), y = (float)(r-radius); if (x*x + y*y > maxDist) continue; showV[0] = ornt2HueFunc(cvFastArctan(y, x)); showV[1] = showV[2] = 255; } } cvtColor(showImg, showImg, CV_HSV2BGR); SaveShow(showImg, tile); }
Mat CmShow::Complex(CMat& _ang, CMat& _mag, CStr& title, float minMagShowAng, int flag) { CV_Assert(_ang.size() == _mag.size() && _ang.channels() == 1 && _mag.channels() == 1); Mat ang, mag; _ang.convertTo(ang, CV_32F); _mag.convertTo(mag, CV_32F); if (flag & SHOW_MAG_LOG) { cv::log(mag + 1, mag); CmCv::FFTShift(mag); } normalize(mag, mag, 0, 255, NORM_MINMAX, CV_8U); minMagShowAng *= 255; int rows = ang.rows, cols = ang.cols; Mat img8U3C(rows, flag & MAG_AS_SAT ? cols : cols * 2, CV_8UC3); if (!(flag & MAG_AS_SAT)) cvtColor(mag, img8U3C(Rect(cols, 0, cols, rows)), CV_GRAY2BGR); Mat showAng = img8U3C(Rect(0, 0, cols, rows)); Ornt2HueFunc ornt2HueFunc = flag & ORNT2HUE_SYM4 ? Ornt2HueSym4 : Ornt2Hue; for (int y = 0; y < rows; y++) { float* angV = ang.ptr<float>(y); byte* magV = mag.ptr<byte>(y); byte* angShow = showAng.ptr<byte>(y); for (int x = 0; x < cols; x++, angShow += 3) { if (magV[x] < minMagShowAng) continue; angShow[0] = ornt2HueFunc(angV[x]); angShow[1] = flag & MAG_AS_SAT ? magV[x] : 255; angShow[2] = 255; } } cvtColor(showAng, showAng, CV_HSV2BGR); SaveShow(img8U3C, title); return img8U3C; }