Ejemplo n.º 1
0
		int analyse(const char* filename, const char* outFile){
			Mat img = cv_imread(filename), imgG, imgTmp;
			if(!img.data){
				//cerr << "invalid image" << endl;
				throw "[ghosts] invalid image";
			}
			cvtColor(img, imgG, CV_BGR2GRAY);
			int bCntX = (img.cols - w ) / bSize, bCntY = (img.rows - w) / bSize;
			int bCnt = bCntX * bCntY;
			int bCntR = bCnt;
			int res = 0;
			vector<vector<vector<double> > > bGhosts(bCnt);
			vector<vector<vector<double> > > bInv(bCnt);
			vector<double> bPredict(bCnt);
			for(int i = 0; i < bCnt; i++){
				bGhosts[i].resize(w * w);
				for(int j = 0; j < w * w; j++)
					bGhosts[i][j].resize(101);
			}
			double elaAv = 0.0;
			for(int q = min(qNorm, qMin); q <= qMax; q++){
				if(!(q == qNorm || (q >= qMin && q <= qMax)))
					continue;
				for(int j = 0; j < w * w; j++){
					Rect curRoi = Rect(j % w, j / w, img.cols - j % w, img.rows - j / w);
					vector<int> compParams;
					vector<uchar> buff;
					compParams.push_back(CV_IMWRITE_JPEG_QUALITY);
					compParams.push_back(q);
					imencode(".jpg", imgG(curRoi), buff, compParams); 
					imgTmp = imdecode(buff, 0);
					for(int i = 0; i < bCnt; i++){
						bGhosts[i][j][q] = calcDiff(imgG(curRoi), imgTmp, bCoordX, bCoordY, bCoordX + bSize, bCoordY + bSize);
						if(q == qNorm && j == 1)
							elaAv += bGhosts[i][j][q];
					}
				}
				fprintf(stderr, "\r%d", q);
			}
			elaAv /= bCnt;
			for(int i = 0; i < bCnt; i++){
				vector<double> predictArgsV;
				predictArgsV.push_back(bCoordX);
				predictArgsV.push_back(bCoordY);
				predictArgsV.push_back(elaAv);
				predictArgsV.push_back(bGhosts[i][1][qNorm]);
				bPredict[i] = predictF(imgG, predictArgsV);
				if(bPredict[i] == 1)
					bCntR--;
			}
			for(int i = 0; i < bCnt; i++)
				for(int j = 0; j < w * w; j++)
					bGhosts[i][j] = nrm(bGhosts[i][j]);
			if(1){
				for(int i = 0; i < bCnt; i++){
					bInv[i].resize(101);
					if(bPredict[i] == 1)
						continue;
					vector<double> m1V(101), m2V(101);
					for(int q = qMin; q <= qMax; q++){
						for(int j = 1; j < w * w; j++) // -- j = 1
							m1V[q] += bGhosts[i][j][q];
						m1V[q] /= w * w - 1;
					}
					for(int q = qMin; q <= qMax; q++){
						for(int j = 1; j < w * w; j++) // -- j = 1
							m2V[q] += bGhosts[i][j][q] * bGhosts[i][j][q];
						m2V[q] = sqrt(m2V[q] / (w * w - 1) - m1V[q] * m1V[q]);
					}
					for(int q = qMin; q <= qMax; q++){
						double maxD = -1, minD = 2;
						for(int j = 1; j < w * w; j++){ // -- j = 1
							if(bGhosts[i][j][q] < minD)
								minD = bGhosts[i][j][q];	
							if(bGhosts[i][j][q] > maxD)
								maxD = bGhosts[i][j][q];
						}
						double dlt = max((maxD - minD) / 3.0, 0.01);
						double dlt1 = max((maxD - minD) / 6.0, 0.004);
						if((bGhosts[i][0][q] < minD - dlt || bGhosts[i][0][q] > maxD + dlt)/* && q <= qMax - 3 && q >= qMin + 3*/){
							bInv[i][q].push_back(1);	
						}else
							bInv[i][q].push_back(0);
						if((bGhosts[i][0][q] < minD - dlt1 || bGhosts[i][0][q] > maxD + dlt1)/* && q <= qMax - 3 && q >= qMin + 3*/){
							bInv[i][q].push_back(1);	
						}else
							bInv[i][q].push_back(0);
					}
				}
				vector<double> qMask(101);
				int qMaskCnt = 0;
				for(int q = qMin; q <= qMax; q++){
					for(int i = 0; i < bCnt; i++){
						if(bPredict[i] == 1)
							continue;
						if(bInv[i][q][0] == 1)
							qMask[q]++;
					}
					if(qMask[q] / bCntR > 0.5){
						qMask[q] = 1;
						qMaskCnt++;
					}else
						qMask[q] = 0;
					cerr << qMask[q];
				}
				cerr << endl;
				if(qMaskCnt < 3){
					//cerr << "don't want to analyse" << endl;
					//throw "[ghosts] don't want to analyse";
					return -1;
				}
				for(int i = 0; i < bCnt; i++){
					int clr;
					if(bPredict[i] == 1)
						clr = 0;
					else{
						int tmpI2 = 0;
						for(int q = qMin; q <= qMax; q++)
							if(qMask[q] == 1 && bInv[i][q][1] == 1){
								tmpI2++;
							}
						if((tmpI2 <= 0 && qMaskCnt >= 3) ||
							(tmpI2 <= 1 && qMaskCnt >= 4) ||
							(tmpI2 <= 2 && qMaskCnt >= 5) || 
							(tmpI2 <= 3 && qMaskCnt >= 8))
							tmpI2 = 0;
						clr = (tmpI2 == 0);
					}
					if(clr){
						res++;
						for(int ix = bCoordX; ix < bCoordX + bSize; ix++)
							for(int iy = bCoordY; iy < bCoordY + bSize; iy++)
								img.at<Vec3b>(iy, ix)[2] = 255;
					}
				}
				if(outFile)
					cv_imwrite(outFile, img);
			}
			return res;
		}
Ejemplo n.º 2
0
void TimeIndependentSolver::inverseIteration(double coordStep, int gridColumns, double eigenValue, Task *task, double threshold, double *output)
{
    int rank, commSize;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &commSize);
    int n = gridColumns / commSize;
    int additionalElementsCount = 0;
    if (rank == commSize - 1) {
        additionalElementsCount = gridColumns % commSize;
    }
    int totalCount = n + additionalElementsCount;

    double * eigenVector = new double[totalCount];
    double * calcVector = new double[totalCount];
    double * diagElements = new double[totalCount];
    double * f = new double[totalCount];
    double * c = new double[totalCount];

    #pragma omp parallel for
    for (int i = 0; i < totalCount; ++i) {
        diagElements[i] = getDiagValue(task, rank * n + i, coordStep) - eigenValue;
    }

    calcFirstApproximation(eigenVector, diagElements, coordStep, totalCount, n, task);
    Logger::verbose("First approximation calculated");

    for (int iteration = 1;;++iteration) {
        Logger::debug("Start iteration #" + std::to_string(iteration));
        memcpy(c, diagElements, totalCount * sizeof(double));

        #pragma omp parallel for
        for (int i = 0; i < totalCount; ++i) {
            f[i] = eigenVector[i] / eigenValue;
        }

        prepareTDA(totalCount, f, c, rank, commSize);
        Logger::debug("PrepareTDA success");
        calcNewVector(c, f, calcVector, totalCount, rank, commSize);
        Logger::debug("Calc new vector success");

        double diff = calcDiff(totalCount, eigenVector, calcVector);
        Logger::debug("Diff calculated: " + std::to_string(diff / threshold));

        if (diff < threshold) {
            Logger::verbose("Diff: " + std::to_string(diff / threshold));
            Logger::verbose("Threshold: " + std::to_string(threshold / threshold));
            Logger::verbose("Iteration: " + std::to_string(iteration));
            break;
        }

        double *qwe = eigenVector;
        eigenVector = calcVector;
        calcVector = qwe;
    }

    MPI_Gather(eigenVector, n, MPI_DOUBLE, output, n, MPI_DOUBLE, 0, MPI_COMM_WORLD);

    delete f;
    delete c;
    delete calcVector;
    delete eigenVector;
    delete diagElements;
}
Ejemplo n.º 3
0
static void addMeasurement(Packet_t *packet)
{
    xvector[vectorPos] = packet->accX;
    yvector[vectorPos] = packet->accY;
    zvector[vectorPos] = packet->accZ;
    vectorPos++;
    if (vectorPos < NUM_SAMPLES) return;

    vectorPos = 0;

    float s = stdev(xvector) + stdev(yvector) + stdev(zvector);
//    PRINTF("stdev = %u\n", (uint16_t) s);
    float accelVector[3] = {
        average(xvector) - groundVector[0],
        average(yvector) - groundVector[1],
        average(zvector) - groundVector[2]
    };

    // PRINTF("accel=(%d %d %d)\n", (int)accelVector[0], (int)accelVector[1], (int)accelVector[2]);

    bool nowFwd, nowBack;
    float accLen = vlength(accelVector);
    if (accLen < ONE_G / 10) {
        nowBack = nowFwd = false;
    } else {
        normalizeQuick(accelVector, accLen);
        nowFwd = sameDirection(fwdVector, accelVector);
        nowBack = inverseDirection(fwdVector, accelVector);
    }
    int now = nowFwd ? 1 : (nowBack ? -1 : 0);

    bool diffFromPrevious;
    static bool notFirstTime;
    if (notFirstTime) {
        // float difference = (calcDiff(xvector, prevxvector) 
        //     + calcDiff(yvector, prevyvector)           
        //     + calcDiff(zvector, prevzvector)) / 3.0;
        float difference = calcDiff(xvector, prevavgxvector) 
                + calcDiff(yvector, prevavgyvector)           
                + calcDiff(zvector, prevavgzvector);
        // PRINTF("difference=%d\n", (int)difference);
        diffFromPrevious = (difference >= 30 * (ONE_G / 10));
    } else {
        notFirstTime = true;
        diffFromPrevious = false;
    }

    bool pastBack = false;
    bool pastFwd = false;
    if (past[0] + past[1] + past[2] >= 1) {
        pastFwd = true;
    } else if (past[0] + past[1] + past[2] <= -1) {
        pastBack = true;
    }
    past[0] = past[1];
    past[1] = past[2];
    past[2] = now;

    bool wasMoving = !bayesStandingMode;

#if 0
    PRINTF("SSD       \t%d\n",  (int)(s >= THRESH_LOW));
    PRINTF("MSD       \t%d\n",  (int)(s >= THRESH_MEDIUM));
    PRINTF("LSD       \t%d\n" , (int)(s > THRESH_HIGH));
    PRINTF("Accelerating\t%d\n", (int)(nowFwd));
    PRINTF("PastAccel \t%d\n", (int)(pastFwd));
    PRINTF("PastBrake \t%d\n", (int)(pastBack));
    PRINTF("WasMoving \t%d\n", (int)(wasMoving));
    PRINTF("Diff      \t%d\n", (int)(diffFromPrevious));
#endif

    calcNewProb(s, wasMoving, pastBack, pastFwd, nowFwd, diffFromPrevious);
}