bool topSort::compare(const QImage &a, const QImage &b,int err) { QSize sz=a.size(); int ca,cb; int sum=0; for(int i=0;i<sz.height();i++) { ca=a.pixelIndex(sz.width()-1,i); cb=b.pixelIndex(0,i); if(abs(ca-cb)>127) sum++; } // qDebug()<<sum; return sum<err; }
void ImportRM2K3Monster::importRM2K3Monster(QString fileLocation) { MonsterAnimation *animation = new MonsterAnimation; QImage oldImage = QImage(fileLocation); QImage newImage = QImage(oldImage.width(), oldImage.height(), QImage::Format_ARGB32); QString monsterLocation = ProjectData::projectLocation + "/images/monsters/" + QFileInfo(fileLocation).baseName() + ".png"; for (int y = 0; y < oldImage.height(); ++y) { for (int x = 0; x < oldImage.width(); ++x) { if (oldImage.pixelIndex(x, y) == 0) newImage.setPixel(x, y, qRgba(0, 0, 0, 0)); else newImage.setPixel(x, y, oldImage.pixel(x, y)); } } if (!isImageEmpty(newImage)) newImage.scaled(newImage.width() * 2, newImage.height() * 2).save(monsterLocation); animation->setName(QFileInfo(fileLocation).baseName()); for (int i = 0; i < 32; ++i) { animation->setPoseLocation(i, monsterLocation); animation->setPoseNumFrames(i, 1); } }
QString MainWindow::renderGlyph(const QString &utf8, const QImage &image) { QByteArray bitmap; int counter = 0; uchar b = 0; QString glyph = QString( "static const struct glib_bitmap glyph_%1 = {\n" " .width = %2,\n" " .height = %3,\n" " .data = {\n ") .arg(utf8) .arg(image.width()) .arg(image.height()); for (int y = 0; y < image.height(); y++) { for (int x = 0; x < image.width(); x++) { b <<= 1; b |= !image.pixelIndex(x, y); if (++counter % 8 == 0) { bitmap.append(b); b = 0; } } printf("\n"); } // Append last byte if necessary if (counter % 8 != 0) { // Remove the gap b <<= 8 - (counter % 8); bitmap.append(b); } bitmap = bitmap.toHex(); for (int i = 0; i < bitmap.size(); i += 2) { glyph.append(QString("0x%1%2") .arg(bitmap[i]) .arg(bitmap[i+1])); if (i < (bitmap.size() - 2)) { if ((i + 2) % 16 == 0) glyph.append(",\n "); else glyph.append(", "); } } glyph += "\n }\n};\n\n"; return glyph; }
static inline int pixelIndex ( const QImage &in_Image, int x, int y ){ if ( x<0 ) x= -x; else if ( x >= in_Image.width() ) x = 2 * in_Image.width() - x - 1; if ( y<0 ) y=-y; else if ( y >= in_Image.height() ) y = 2 * in_Image.height() - y - 1; return in_Image.pixelIndex(x,y); }
bool topSort::judgeEnd(const QImage &a) { QSize sz=a.size(); int ca; for(int i=0;i<sz.height();i++) { ca=a.pixelIndex(sz.width()-1,i); if(ca!=255) return false; } return true; }
QImage ImageGrayLevelTransformator::process8BitImageInGLT(QImage image,int oldLow,int oldHigh,int newLow,int newHigh) { QImage eightBitImage = image; int w = image.width(); int h = image.height(); int i; int j; int byMap[256]; // 定义灰度映射表 for (i = 0; i <= oldLow; i++) //当灰度级小于要增强的灰度级时 { newLow > 0 ? byMap[i] = newLow: byMap[i] = 0; } for (; i <= oldHigh; i++) { if (oldHigh != oldLow) // 判断orahig是否等于oralow(防止母为0) { byMap[i] = newLow + (int) ((newHigh - newLow) * (i - oldLow) / (oldHigh - oldLow)); } else { byMap[i] = newLow; //直接赋值为newlow } } for (; i < newHigh; i++) { if (newHigh <= 255) //判断d是否大于255 { byMap[i] = newHigh; //直接赋值为newhig } else { byMap[i] = 255; //直接赋值为255 } } for(int y = 0; y < h;y++) //对图像的每个像素值进行变换 { for(int x = 0; x < w; x++) //每列 { // 指向DIB第i行,第j个象素的指针 //lpSrc = (unsigned char*)lpDIBBits + lmageWidth * (lmageHeight - 1 - y) + x; //*lpSrc = byMap[*lpSrc]; //用新的灰度替代原有的灰度 uint v = byMap[eightBitImage.pixelIndex(x, h - 1- y)]; if(v < 0) v = 0; if(v > 255) v = 255; eightBitImage.setPixel( x, h - 1- y, v); } } return eightBitImage; }
void LabelImage::init(const QImage& qim, const QMap< unsigned int, signed char >& map) { if (qim.width() != width_ || qim.height() != height_) Image< signed char >::init( qim.width(), qim.height() ); bool use_index = qim.depth() < 24; for( int j=0; j<qim.height(); j++ ) for( int i=0; i<qim.width(); i++ ){ unsigned int p = use_index ? qim.pixelIndex(i, j) : qim.pixel( i, j ); if ( map.contains( p ) ) (*this)(i,j) = map[ p ]; else if (use_index) qWarning( "Color not found in map: %d !", p ); else qWarning( "Color not found in map: %x !", p ); } }
int LifeMainWindow::neighborCount(const QImage& img, int x, int y) { int retval = 0; for (int c = x-1; c < x+2; c++) { int wc = c; if (wc >= img.width()) wc = 0; if (wc < 0) wc = img.width() - 1; for (int r = y-1; r < y+2; ++r) { if ((c == x) && (r == y)) continue; /* I am not my own neighbor */ int wr = r; if (wr < 0) wr = img.height() - 1; if (wr >= img.height()) wr = 0; if (ALIVE == img.pixelIndex(wc, wr)) retval ++; } } return retval; }
void maskImageWidget::automaticMask(const QPoint &p) { QImage image = (pimpl_->render_area_->palette().base().texture()).toImage(); QImage out; fillImage fi; fi.Compute(image, p.x(), p.y(), pimpl_->threshold_gradient_, pimpl_->threshold_fixed_, out); const size_t width(image.width()), height(image.height()); QImage temp(pimpl_->render_area_->alphaMask()); for (size_t i = 0; i < width; ++i) { for (size_t j = 0; j < height; ++j) { if (out.pixelIndex(i, j) > 0) temp.setPixel(i, j, QColor(Qt::black).rgba()); } } //temp.save("temp.jpg","jpg"); pimpl_->render_area_->setAlphaMask(temp); }
QPixmap *Board::lighten(QPixmap *src) { const float FACTOR = 1.3; QImage img = src->convertToImage(); if(img.depth() > 8) { // at least high-color for(int y = 0; y < src->height(); y++) { uchar *p = (uchar *)img.scanLine(y); for(int x = 0; x < src->width() * 4; x++) { *p = (unsigned char)MIN(255, (int)(FACTOR * (*p))); p++; } } } else { // image has a palette // get background color index int idx = img.pixelIndex(8, 1); // should work for all tiles img.setColor(idx, QColor(img.color(idx)).light().rgb()); } QPixmap *pm = new QPixmap(); pm->convertFromImage(img); return pm; }
QList<QImage> splitPath(const QImage& img) { QImage up = img; QImage down = img; for (int x = 0; x < img.width(); ++x) { bool found = false; for (int i = 1; i < 10; ++i) { int y = img.height() / 2 + i / 2 * ((i%2) ? 1 : -1); if (img.pixelIndex(x, y) <= 127) { for (int z = 0; z < y; ++z) down.setPixel(x, z, 0); for (int z = y+1; z < up.height(); ++z) up.setPixel(x, z, 0); found = true; break; } } if (!found) { return QList<QImage>() << img; } } return QList<QImage>() << split(up) << split(down); }
void RandomdotStereogram::recreateStereogram() { if (!texDepth()) return; if (!texDepth()->valid()) return; QImage imageTemp = texDepth()->image(); QSize s = imageTemp.size(); // Create left and right tex QImage imageL = QImage(s, QImage::Format_Indexed8); QImage imageR = QImage(s, QImage::Format_Indexed8); int i, j; // Color Map imageL.setNumColors(maxColor_); imageR.setNumColors(maxColor_); for (i = 0; i < maxColor_; i++) { QRgb c = colors_[i]; imageL.setColor(i, c); imageR.setColor(i, c); } const int divisor = 255 / offset(); // Random BG for (j = 0; j < s.height(); j++) { for (i = 0; i < s.width(); i++) { char col = rand()%abs(maxColor_-excColor_); imageL.setPixel(i, j, col); imageR.setPixel(i, j, col); } } // Offset FG if (style() == Stereogram::convex) { for (j = 0; j < s.height(); j++) { for (i = 0; i < s.width(); i++) { char col = rand()%(maxColor_-excColor_)+excColor_; unsigned char o = roundf(((float)qGray(imageTemp.pixel(i, j))) / divisor); if (o > 0) { // For convex objects, the destination in the picture is offset unsigned char oL = floor(0.5f*o); unsigned char oR = o - oL; if (i+oL < s.width()) imageL.setPixel(i+oL, j, col); if (i-oR > 0) imageR.setPixel(i-oR, j, col); } } } } else // (style() == Stereogram::concave) { QImage imageS = QImage(s, QImage::Format_Indexed8); imageS.setNumColors(maxColor_); for (i = 0; i < maxColor_; i++) { QRgb c = colors_[i]; imageS.setColor(i, c); } for (j = 0; j < s.height(); j++) { for (i = 0; i < s.width(); i++) { char col = rand()%(maxColor_-excColor_)+excColor_; imageS.setPixel(i, j, col); } } for (j = 0; j < s.height(); j++) { for (i = 0; i < s.width(); i++) { unsigned char o = roundf(((float)qGray(imageTemp.pixel(i, j))) / divisor); if (o > 0) { // For concave objects, the source of the picture is offset imageL.setPixel(i, j, imageS.pixelIndex((i+o)%imageS.width(), j)); imageR.setPixel(i, j, imageS.pixelIndex(i, j)); } } } } shared_ptr<Texture> texLeft = shared_ptr<Texture>(new Texture(imageL)); shared_ptr<Texture> texRight = shared_ptr<Texture>(new Texture(imageR)); setTexLeft(texLeft); setTexRight(texRight); }
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); if (a.arguments().size() < 3) { qCritical() << "this application requires more arguments!"; return -1; } const QString trainDirName = a.arguments().at(1); const QString testDirName = a.arguments().at(2); const QString outputName = a.arguments().at(3); const QString extractorName = a.arguments().at(4); const QStringList extractorArgs = a.arguments().mid(5); ExtractorInterface *extractor = ExtractorFactory::getExtractor(extractorName, extractorArgs); if (extractor == NULL) { qCritical() << "failed to initialise extractor" << extractorName; return -2; } QDir trainDir(trainDirName); QStringList subdirs = QStringList() << "wood" << "straw" << "salt" << "linen"; QList<quint8> labels = QList<quint8>() << 32 << 96 << 160 << 224; QVector<LabelledData> trainData; #ifdef HAS_ELAPSED_TIMER QElapsedTimer extractionTimer; #else QTime extractionTimer; #endif int threadCount = 1; #ifdef _OPENMP #pragma omp parallel { #pragma omp single { threadCount = omp_get_num_threads(); } } #endif qDebug() << "using" << threadCount << "threads."; extractionTimer.start(); unsigned int imagesCount = 0; for (int j = 0; j < subdirs.size(); j++) { trainDir.cd(subdirs.at(j)); const QFileInfoList fileList = trainDir.entryInfoList(QStringList() << "*.png"); #ifdef HAS_ELAPSED_TIMER QElapsedTimer extractorTimer; #else QTime extractorTimer; #endif extractorTimer.start(); for (int i = 0; i < fileList.size(); i++) { imagesCount++; const QString filename = fileList.at(i).filePath(); const QImage image(filename); if (image.format() != QImage::Format_Indexed8) { qCritical("Image is not greyscale!"); return -1; } extractor->preprocess(image); if (extractor->extracts()) { unsigned int count = trainData.size(); trainData.resize(trainData.size() + image.width() * image.height()); LabelledData *trainDataPtr = trainData.data(); #pragma omp parallel for for (int x = 0; x < image.width(); x++) { for (int y = 0; y < image.height(); y++) { const QVector<nnreal> res = extractor->extract(image, x, y); Q_ASSERT(res.size() == extractor->size()); LabelledData li(res, labels.at(j)); const unsigned int idx = count + x * image.height() + y; trainDataPtr[idx] = li; } } } const QVector<QVector<nnreal> > ppFeatures = extractor->postprocess(image); const int ppCount = ppFeatures.size(); if (ppCount > 0) { const int count = trainData.size(); trainData.resize(trainData.size() + ppFeatures.size()); LabelledData *trainDataPtr = trainData.data(); #pragma omp parallel for for (int k = 0; k < ppCount; k++) { Q_ASSERT(ppFeatures.at(k).size() == extractor->size()); LabelledData ld(ppFeatures.at(k), labels.at(j)); trainDataPtr[count + k] = ld; } } qDebug() << fileList.at(i).filePath() << extractorTimer.restart(); } trainDir.cdUp(); } const int msecs = extractionTimer.elapsed(); qDebug() << "trainSize:" << trainData.size() << "extraction of " << imagesCount << "images took" << msecs << "msecs, average" << float(msecs) / imagesCount << "msecs per image"; const QString trainOutFilename(outputName + "_" + extractorName + "_train.out"); QFile trainOutput(trainOutFilename); if (!trainOutput.open(QIODevice::WriteOnly)) { qCritical() << "failed to open output file" << trainOutFilename; return -3; } { #ifdef HAS_ELAPSED_TIMER QElapsedTimer saveTimer; #else QTime saveTimer; #endif saveTimer.start(); QDataStream outstream(&trainOutput); saveFeatures(outstream, extractorName, extractorArgs, extractor->size(), trainData); int msecs = saveTimer.elapsed(); qDebug() << "saving took" << msecs << "msecs"; } trainOutput.close(); trainData.clear(); { QDir testDir(testDirName); const QFileInfoList dataFileList = testDir.entryInfoList(QStringList() << "test*.png"); const QFileInfoList labelFileList = testDir.entryInfoList(QStringList() << "label*.png"); Q_ASSERT(dataFileList.size() == labelFileList.size()); #ifdef HAS_ELAPSED_TIMER QElapsedTimer extractorTimer; #else QTime extractorTimer; #endif extractorTimer.start(); QTextStream out(stdout); for (int i = 0; i < dataFileList.size(); i++) { const QImage dataImage(dataFileList.at(i).filePath()); const QImage labelImage(labelFileList.at(i).filePath()); QVector<LabelledData> testData; extractor->preprocessTest(dataImage, labelImage); int cnt = 0; if (extractor->extracts()) { unsigned int count = testData.size(); testData.resize(dataImage.width() * dataImage.height()); LabelledData *testDataPtr = testData.data(); #pragma omp parallel for for (int x = 0; x < dataImage.width(); x++) { #pragma omp critical { cnt++; out << cnt * 100 / dataImage.width() << "%" << '\r'; out.flush(); } for (int y = 0; y < dataImage.height(); y++) { const QVector<nnreal> res = extractor->extract(dataImage, x, y); const quint8 c = labelImage.pixelIndex(x, y); LabelledData li(res, c); li.squeeze(); const unsigned int idx = count + x * dataImage.height() + y; testDataPtr[idx] = li; } } out << endl; } const QVector<LabelledData> ppFeatures = extractor->postprocessTest(dataImage, labelImage); testData << ppFeatures; qDebug() << dataFileList.at(i).filePath() << extractorTimer.restart(); const QString testOutFilename(outputName + "_" + extractorName + "_test" + QString::number(i) + ".out"); QFile testOutput(testOutFilename); if (!testOutput.open(QIODevice::WriteOnly)) { qCritical() << "failed to open output file" << testOutFilename; return -3; } { #ifdef HAS_ELAPSED_TIMER QElapsedTimer saveTimer; #else QTime saveTimer; #endif saveTimer.start(); QDataStream outstream(&testOutput); saveFeatures(outstream, extractorName, extractorArgs, extractor->size(), testData); int msecs = saveTimer.elapsed(); qDebug() << "saving took" << msecs << "msecs"; } testOutput.close(); } } delete extractor; return 0; }