cv::Scalar colorLevel(int index, hsv::Levels levels) { // TODO move these outside and pass as arguments? int numColors = levels[0] * levels[1] * levels[2]; int numGrays = levels[2] + 1; arma::ivec grays = arma::linspace<arma::ivec>(1, 256, numGrays); if (index > numColors - 1) { // grays int level = index - numColors; int value = grays(level) - 1; return cv::Scalar(value, value, value); } else { // colors hsv::Levels indices = ind2sub(levels, index); cv::Vec3f temp; for (int i = 0; i < 3; i++) { temp[i] = static_cast<float>(indices[i] + 1) / levels[i]; BOOST_ASSERT(temp[i] >= 0. and temp[i] <= 1.); } temp[0] *= 360.; cv::Mat bgr(1, 1, CV_32FC3); bgr.at<cv::Vec3f>(0) = temp; cv::cvtColor(bgr, bgr, CV_HSV2BGR); cv::Vec3f& val = bgr.at<cv::Vec3f>(0); val *= 255.; return cv::Scalar(val[0], val[1], val[2]); } }
arma::fvec computeHistogram(const cv::Mat& quantized, const arma::ivec3& levels, bool normalize) { BOOST_ASSERT(quantized.depth() == cv::DataType<T>::type); const cv::Size& sz = quantized.size(); cv::Mat data = quantized.reshape(1, sz.width * sz.height); BOOST_ASSERT(cv::Size(3, sz.width * sz.height) == data.size()); BOOST_ASSERT(1 == data.channels()); const int& hue_bins = levels[0]; const int& sat_bins = levels[1]; const int& val_bins = levels[2]; arma::icube colors(hue_bins, sat_bins, val_bins); colors.zeros(); arma::ivec grays(val_bins + 1); grays.zeros(); // for each HSV tuple increment the corresponding histogram bin for (int i = 0; i < data.rows; i++) { cv::Mat t = data.row(i); const T& h = t.at<T>(0); const T& s = t.at<T>(1); const T& v = t.at<T>(2); if (v == 1) { // black grays(0)++; } else if (s == 1 and v > 1) { // grays grays(v-1)++; } else { colors(h-1, s-2, v-2)++; } } colors.reshape(hue_bins * sat_bins * val_bins, 1, 1); arma::ivec values = arma::join_cols(arma::ivec(colors), grays); return arma::conv_to<arma::fvec>::from(values) /= arma::as_scalar(arma::sum(values)); }
void Grayscale_equalization(QImage &image) { int width = image.width(); int height = image.height(); int histSize = 256; std::vector<double> hist(histSize, 0.0); std::vector< std::vector<int> > grays(width, std::vector<int>(height) ); for (int y = 0; y < height; ++y) for (int x = 0; x < width; ++x) { QRgb oldColor = image.pixel(x, y); int gray = qPow( 0.2126 * qPow(qRed(oldColor), 2.2) + 0.7152 * qPow(qGreen(oldColor), 2.2) + 0.0722 * qPow(qBlue(oldColor), 2.2), 1/2.2 ); grays[x][y] = gray; ++hist[gray]; } for (int i = 1; i < histSize; ++i) hist[i] += hist[i - 1]; for (int i = 0; i < histSize; ++i) hist[i] /= width * height; for (int y = 0; y < height; ++y) for (int x = 0; x < width; ++x) { QRgb oldColor = image.pixel(x, y); int red = qRed(oldColor); int green = qGreen(oldColor); int blue = qBlue(oldColor); int gray = grays[x][y]; int newGray = qMin(qFloor(histSize * hist[gray]), histSize - 1); int delta = newGray - gray; int newRed = qMax(qMin(red + delta, 255), 0); int newGreen = qMax(qMin(green + delta, 255), 0);; int newBlue = qMax(qMin(blue + delta, 255), 0);; image.setPixel(x, y, qRgb(newRed, newGreen, newBlue)); } }
void MainWindow::on_actionOtsu_global_triggered() { QPixmap pixmap = pixmapItem->pixmap().copy(); QImage image = pixmap.toImage(); int width = image.width(); int height = image.height(); if (width == 0 || height == 0) { ui->statusBar->showMessage( tr("Error. Image bad size"), 3000 ); return; } std::vector<int> grays(width * height, 0); int finalThreshold = otsu(image, grays, 0, 0, width, height); QRgb black = qRgb(0,0,0); QRgb white = qRgb(255,255,255); QRgb newColor; for (int y = 0; y < height; ++y) for (int x = 0; x < width; ++x) { int gray = grays[x + y * width]; if ( gray < finalThreshold ) newColor = black; else newColor = white; image.setPixel(x, y, newColor); } pixmap.convertFromImage(image); pixmapItem_2->setPixmap(pixmap); scene_2->setSceneRect(QRectF(pixmap.rect())); calcHist(pixmap, hist_2, maxLevel_2); drawHist(pixmapItem_4, hist_2, maxLevel_2); }
void Reconstructor::runReconstruction() { for(int i=0; i< numOfCams; i++) { if(cameras[i].distortion.empty()) { std::cout<<"Camera "<<i<< "is not set.\n"; exit(-1); } if(pathSet[i] == false) { std::cout<<"Image path for camera "<< i <<" is not set."; exit(-1); } } GrayCodes grays(proj_w,proj_h); numOfColBits = grays.getNumOfColBits(); numOfRowBits = grays.getNumOfRowBits(); numberOfImgs = grays.getNumOfImgs(); for(int i=0; i < numOfCams; i++) { //findProjectorCenter(); cameras[i].position = cv::Point3f(0,0,0); cam2WorldSpace(cameras[i],cameras[i].position); camera = &cameras[i]; camsPixels[i] = new cv::vector<cv::Point>[proj_h*proj_w]; camPixels = camsPixels[i]; loadCamImgs(camFolder[i],imgPrefix[i],imgSuffix[i]); colorImgs.push_back(cv::Mat()); colorImgs[i] = color; computeShadows(); if(saveShadowMask_) { std::stringstream path; path<<"cam"<<i+1<<"Mask.png"; saveShadowImg(path.str().c_str()); } decodePaterns(); unloadCamImgs(); } //reconstruct points3DProjView = new PointCloudImage( proj_w, proj_h , true ); for(int i = 0; i < numOfCams; i++) { for(int j=i+1; j< numOfCams; j++) triangulation(camsPixels[i],cameras[i],camsPixels[j],cameras[j],i,j); } }
void MainWindow::on_actionBrightness_quantization_triggered() { QPixmap pixmap = pixmapItem->pixmap().copy(); QImage image = pixmap.toImage(); int width = image.width(); int height = image.height(); if (width == 0 || height == 0) { ui->statusBar->showMessage( tr("Error. Image bad size"), 3000 ); return; } std::vector<int> grays(width * height); for (int y = 0; y < height; ++y) for (int x = 0; x < width; ++x) { QRgb oldColor = image.pixel(x, y); int gray = qPow( 0.2126 * qPow(qRed(oldColor), 2.2) + 0.7152 * qPow(qGreen(oldColor), 2.2) + 0.0722 * qPow(qBlue(oldColor), 2.2), 1/2.2 ); grays[x + y * width] = gray; } std::vector<unsigned int> Rs(256, 0); std::vector<unsigned int> Gs(256, 0); std::vector<unsigned int> Bs(256, 0); std::vector<int> hist(256, 0); for (int y = 0; y < height; ++y) for (int x = 0; x < width; ++x) { int num = grays[x + y * width]; Rs[num] += qRed( image.pixel(x, y) ); Gs[num] += qGreen( image.pixel(x, y) ); Bs[num] += qBlue( image.pixel(x, y) ); ++hist[num]; } int quantsCountMaximum = 0; std::map<int, QRgb> colors; for (int i = 0; i < 256; ++i) { if (hist[i] == 0) continue; Rs[i] /= hist[i]; Gs[i] /= hist[i]; Bs[i] /= hist[i]; colors[i] = qRgb(Rs[i], Gs[i], Bs[i]); ++quantsCountMaximum; } DialogQuantization dialog; dialog.setQuantCountMaximum(quantsCountMaximum); if (dialog.exec() == QDialog::Rejected) return; int quantsCount = dialog.quantsCount(); double shift = 256 / quantsCount; double cur = 0.0; double next = 0.0; for (int i = 0; i < quantsCount; ++i) { next += shift; int c = qFloor(cur); int n = qFloor(next); int minC = 256; for (std::map<int, QRgb>::iterator it = colors.begin(); it != colors.end(); ++it) if (c <= (it->first) && (it->first) < n) if (it->first < minC) minC = it->first; QRgb newColor = colors[minC]; for (int y = 0; y < height; ++y) for (int x = 0; x < width; ++x) { int num = grays[x + y * width]; if (c <= num && num < n) image.setPixel(x, y, newColor); else continue; } cur = next; } pixmap.convertFromImage(image); pixmapItem_2->setPixmap(pixmap); scene_2->setSceneRect(QRectF(pixmap.rect())); //ui->graphicsView_2->fitInView(scene_2->itemsBoundingRect(), Qt::KeepAspectRatio); calcHist(pixmap, hist_2, maxLevel_2); drawHist(pixmapItem_4, hist_2, maxLevel_2); }
void MainWindow::on_actionOtsu_local_triggered() { QPixmap pixmap = pixmapItem->pixmap().copy(); QImage image = pixmap.toImage(); int width = image.width(); int height = image.height(); if (width == 0 || height == 0) { ui->statusBar->showMessage( tr("Error. Image bad size"), 3000 ); return; } DialogOtsuLocal dialog; dialog.setSpinBoxes(pixmapItem->pixmap().width(), pixmapItem->pixmap().height()); if (dialog.exec() == QDialog::Rejected) return; std::vector<int> grays(width * height, 0); int countX = dialog.gridX(); int countY = dialog.gridY(); double shiftY = ( (double) height ) / countY; double shiftX = ( (double) width ) / countX; double curX = 0.0; double curY = 0.0; double nextX = 0.0; double nextY = 0.0; int cX; int cY; int nX; int nY; QRgb black = qRgb(0,0,0); QRgb white = qRgb(255,255,255); QRgb newColor; for (int i = 0; i < countY; ++i) { nextY += shiftY; cY = qFloor(curY); nY = qFloor(nextY); curX = 0.0; nextX = 0.0; for (int j = 0; j < countX; ++j) { nextX += shiftX; cX = qFloor(curX); nX = qFloor(nextX); int threshold = otsu(image, grays, cX, cY, nX, nY); for (int y = cY; y < nY; ++y) for (int x = cX; x < nX; ++x) { int gray = grays[x + y * (nX - cX)]; if ( gray < threshold ) newColor = black; else newColor = white; image.setPixel(x, y, newColor); } curX = nextX; } curY = nextY; } pixmap.convertFromImage(image); pixmapItem_2->setPixmap(pixmap); scene_2->setSceneRect(QRectF(pixmap.rect())); calcHist(pixmap, hist_2, maxLevel_2); drawHist(pixmapItem_4, hist_2, maxLevel_2); }
void MainWindow::on_actionBrightness_gradient_triggered() { QPixmap pixmap = pixmapItem->pixmap().copy(); QImage image = pixmap.toImage(); int width = image.width(); int height = image.height(); if (width == 0 || height == 0) { ui->statusBar->showMessage( tr("Error. Image bad size"), 3000 ); return; } std::vector< std::vector<int> > grays( width, std::vector<int>(height) ); for (int y = 0; y < height; ++y) for (int x = 0; x < width; ++x) { QRgb oldColor = image.pixel(x, y); int gray = qPow( 0.2126 * qPow(qRed(oldColor), 2.2) + 0.7152 * qPow(qGreen(oldColor), 2.2) + 0.0722 * qPow(qBlue(oldColor), 2.2), 1/2.2 ); grays[x][y] = gray; } int G_x; int G_y; int G; unsigned long int dividend = 0; unsigned int divisor = 0; for (int y = 0; y < height; ++y) for (int x = 0; x < width; ++x) { if (x == 0) G_x = grays[x+1][y]; else if (x == width - 1) G_x = grays[x-1][y]; else G_x = grays[x+1][y] - grays[x-1][y]; if (y == 0) G_y = grays[x][y+1]; else if (y == height - 1) G_y = grays[x][y-1]; else G_y = grays[x][y+1] - grays[x][y-1]; G = qMax( qAbs(G_x), qAbs(G_y) ); dividend += grays[x][y] * G; divisor += G; } int threshold = dividend / divisor; if (0 <= threshold && threshold <= 255) { QRgb black = qRgb(0,0,0); QRgb white = qRgb(255,255,255); QRgb newColor; for (int y = 0; y < height; ++y) for (int x = 0; x < width; ++x) { int gray = grays[x][y]; if ( gray < threshold ) newColor = black; else newColor = white; image.setPixel(x, y, newColor); } } else ui->statusBar->showMessage(tr("Error. Invalid threshold"), 3000); pixmap.convertFromImage(image); pixmapItem_2->setPixmap(pixmap); scene_2->setSceneRect(QRectF(pixmap.rect())); calcHist(pixmap, hist_2, maxLevel_2); drawHist(pixmapItem_4, hist_2, maxLevel_2); }