int main (int argc, char const* argv[]) { diamond(3,'*'); puts(""); diamond(5,'+'); return 0; }
//szum Perlina 2d krok square void Map::square(int x, int y, int step, double amp) { if(step > 0) { //p. srodkowy hmap[x][y] = 0.25*(hmap[x-step][y-step]+hmap[x-step][y+step]+hmap[x+step][y-step]+hmap[x+step][y+step]) + noise(amp); diamond(x-step, y, step, amp); diamond(x+step, y, step, amp); diamond(x, y-step, step, amp); diamond(x, y+step, step, amp); } }
void test_diamond() { assert( diamond('A') == "A\n" ); assert( diamond('B') == " A\n" "B B\n" " A\n" ); assert( diamond('C') == " A\n" " B B\n" "C C\n" " B B\n" " A\n" ); assert( diamond('1') == "Please give letter between 'A' and 'Z'.\n" ); }
void Particle::draw(){ ofTranslate(ofGetWidth()/2, ofGetHeight()/2); for (int i=0; i<7; i++) { ofSetColor(ofRandom(0, 255), ofRandom(100, 255), ofRandom(100, 255), 255); for (int i=0; i<7; i++) { for (int i=0; i<7; i++) { ofRotateX(rotationX); ofRotateY(rotationY); ofRotateZ(rotationZ); diamond(center2, rotationX, rotationY); } } } }
void ArcTerrain::square(int T[], int B[], int L[], int R[], int level) { //std::cout << "Square " << level << ":\n"; int num2Average = 4; float sum = 0; float average = 0; bool top = true, bottom = true, left = true, right = true; // Error Check / find average if (((T[0] >= 0) && (T[1] >= 0)) && (((T[0] < mSize) && (T[1] < mSize)))) sum += mMap[T[0]][T[1]]; else {num2Average--; top = false;} if (((B[0] >= 0) && (B[1] >= 0)) && (((B[0] < mSize) && (B[1] < mSize)))) sum += mMap[B[0]][B[1]]; else {num2Average--; bottom = false;} if (((R[0] >= 0) && (R[1] >= 0)) && (((R[0] < mSize) && (R[1] < mSize)))) sum += mMap[R[0]][R[1]]; else {num2Average--; right = false;} if (((L[0] >= 0) && (L[1] >= 0)) && (((L[0] < mSize) && (L[1] < mSize)))) sum += mMap[L[0]][L[1]]; else {num2Average--; left = false;} if (num2Average != 0) average = sum / num2Average; //std::cout << average << " "; // Find midpoint int midX = T[0]; int midY = L[1]; // Set midpoint to average + random height mMap[midX][midY] = average+ randomHeight(level--); if (mMap[midX][midY] > mMax) mMax = mMap[midX][midY]; if (mMap[midX][midY] < mMin) mMin = mMap[midX][midY]; //level--; if (level > 0) { // find corners int mid[2] = {midX, midY}; int tL[2] = {L[0], T[1]}; int tR[2] = {R[0], T[1]}; int bL[2] = {L[0], B[1]}; int bR[2] = {R[0], B[1]}; // diamond each quadrant if they exist if (top && left) {diamond(tL, T, L, mid, level);} if (top && right) {diamond(T, tR, mid, R, level);} if (bottom && left) {diamond(L, mid, bL, B, level);} if (bottom && right) {diamond(mid, R, B, bR, level);} } }
void MapIcon::paintDiamond(QPainter&p, const QPoint& point, int size, int sizeWH) { QPointArray diamond(4); diamond.setPoint(0, point.x(), point.y() + size); diamond.setPoint(1, point.x() + size, point.y()); diamond.setPoint(2, point.x(), point.y() - size); diamond.setPoint(3, point.x() - size, point.y()); p.drawPolygon(diamond); }
void Fractal::square(int size, int X, int Y, float range){ //top if (size <= 1){ return; } if ((Y - 1) < 0){ m_heightMap[Y][X + size / 2] = (m_heightMap[Y][X] + m_heightMap[Y][X + size] + m_heightMap[Y + size / 2][X + size / 2]) / 3; } else{ m_heightMap[Y][X + size / 2] = (m_heightMap[Y][X] + m_heightMap[Y - 1][X + size / 2] + m_heightMap[Y][X + size] + m_heightMap[Y + size / 2][X + size / 2]) / 4; } //right if ((X + size + 1) > m_size - 1){ m_heightMap[Y + size / 2][X + size] = (m_heightMap[Y][X + size] + m_heightMap[Y + size][X + size] + m_heightMap[Y + size / 2][X + size / 2]) / 3; } else{ m_heightMap[Y + size / 2][X + size] = (m_heightMap[Y][X + size] + m_heightMap[Y + size / 2][X + size + 1] + m_heightMap[Y + size][X + size] + m_heightMap[Y + size / 2][X + size / 2]) / 4; } //bottom if ((Y + size + 1) > m_size - 1){ m_heightMap[Y + size][X + size / 2] = (m_heightMap[Y + size / 2][X + size / 2] + m_heightMap[Y + size][X + size] + m_heightMap[Y + size][X]) / 3; } else{ m_heightMap[Y + size][X + size / 2] = (m_heightMap[Y + size / 2][X + size / 2] + m_heightMap[Y + size + 1][X + size / 2] + m_heightMap[Y + size][X + size] + m_heightMap[Y + size][X]) / 4; } //left if ((X - 1) < 0){ m_heightMap[Y + size / 2][X] = (m_heightMap[Y][X] + m_heightMap[Y + size / 2][X + size / 2] + m_heightMap[Y + size][X]) / 3; } else{ m_heightMap[Y + size / 2][X] = (m_heightMap[Y][X] + m_heightMap[Y + size / 2][X + size / 2] + m_heightMap[Y + size][X] + m_heightMap[Y + size / 2][X - 1]) / 4; } range *= pow(2, -roughness); diamond(size / 2, X, Y, range); diamond(size / 2, X + size / 2, Y, range); diamond(size / 2, X, Y + size / 2, range); diamond(size / 2, X + size / 2, Y + size / 2, range); }
KDiamond::Board::Board(KGameRenderer* renderer) : m_difficultyIndex(Kg::difficultyLevel() / 10 - 2) , m_size(boardSizes[m_difficultyIndex]) , m_colorCount(boardColorCounts[m_difficultyIndex]) , m_paused(false) , m_renderer(renderer) , m_diamonds(m_size * m_size, 0) { for (QPoint point; point.x() < m_size; ++point.rx()) for (point.ry() = 0; point.y() < m_size; ++point.ry()) { //displacement vectors needed for the following algorithm const QPoint dispY1(0, -1), dispY2(0, -2); const QPoint dispX1(-1, 0), dispX2(-2, 0); //roll the dice to get a color, but ensure that there are not three of a color in a row from the start int color; while (true) { color = qrand() % m_colorCount + 1; // +1 because numbering of enum KDiamond::Color starts at 1 //condition: no triplet in y axis (attention: only the diamonds above us are defined already) if (point.y() >= 2) //no triplet possible for i = 0, 1 { const int otherColor1 = diamond(point + dispY1)->color(); const int otherColor2 = diamond(point + dispY2)->color(); if (otherColor1 == color && otherColor2 == color) continue; //roll the dice again } //same condition on x axis if (point.x() >= 2) { const int otherColor1 = diamond(point + dispX1)->color(); const int otherColor2 = diamond(point + dispX2)->color(); if (otherColor1 == color && otherColor2 == color) continue; } break; } rDiamond(point) = spawnDiamond(color); diamond(point)->setPos(point); } }
//-------------------------------------------------------------- void ofApp::draw(){ // ofBackground(0); ofSetColor(255,0,0); // // ofEllipse(circleX, ofGetHeight()/2, 200, 200); diamond(center2, width2, height2); ofDrawBitmapString("hello", 500,600); cout<< "test" << endl; }
int main(int argc, char *argv[]) { int n = 0; char c = '*'; printf("input the num of diamond to n: \n"); scanf("%d", &n); printf("now the diamond is as below : \n"); diamond(n, c); return 0; }
void ArcTerrain::generate() { mMap[0 ][0 ] = -2;//(rand() % (int)mMaxHeight * 10) / 20; mMap[0 ][mSize - 1] = -2;//(rand() % (int)mMaxHeight * 10) / 20; mMap[mSize - 1][0 ] = -2;//(rand() % (int)mMaxHeight * 10) / 20; mMap[mSize - 1][mSize - 1] = -2;//(rand() % (int)mMaxHeight * 10) / 20; int tL[] = {0, 0}; int tR[] = {mSize - 1, 0}; int bL[] = {0, mSize - 1}; int bR[] = {mSize - 1, mSize - 1}; diamond(tL, tR, bL, bR, mLevel - 1); //diamondSquare(tL, tR, bL, bR, mLevel - 1); }
//-------------------------------------------------------------- void ofApp::draw(){ // //change background colour // ofBackground(255,180,210); // // //set color // ofSetColor(255,0,0); // ofEllipse(circleX, ofGetHeight()/2, 200,200); // //add text to the screen // ofDrawBitmapString("Hello World!", 100,100); // // cout << "circleX" << circleX << endl; diamond(center, width, height); }
main() { int year,num,diam,pilih; char text[50],hh[100],menu; lagi:system("cls"); puts("\tPAP-01"); puts("\t------"); printf("1. Tahun Kabisat\n2. Hitung Vokal\n3. Cek Prima\n4. Char Frequency\n5. Diamond\n"); pilih=0; while(pilih<1||pilih>5) { printf("Pilih: "); scanf("%d",&pilih); } switch(pilih) { case 1: printf("\tInput tahun: "); scanf("%d",&year); printf("\t%d ==> %s",year,iskabisat(year)?"true":"false"); break; case 2: printf("\tInput string: "); fflush(stdin);gets(text); printf("\tjumlah = %d",hitungvokal(text)); break; case 3: printf("\tInput angka: "); scanf("%d",&num); printf("\t%d ==> %s",num,checkprima(num)?"true":"false"); break; case 4: printf("\tInput string: "); fflush(stdin);gets(hh);printf("\n"); charFrequency(hh); break; case 5: printf("\tInput baris: "); scanf("%d",&diam);printf("\n"); diamond(diam); break; } printf("\n\nke menu[y/other]: "); fflush(stdin);scanf("%c",&menu); if(menu=='y') { goto lagi; }system("pause"); }
int main(int argc,char **argv) { printf("the main function is begin.\n"); printf("please input a value above zero\n"); int b,c; b=c=0; char a; a = getchar(); printf ("the ascii value of a is %d\n",a); b = input(&a); printf ("the ascii value of b is %d\n",b); c = diamond(&a); printf ("the ascii value of c is %d\n",c); printf("the main function is end.\n"); return 0; }
//-------------------------------------------------------------- void ofApp::draw(){ ofSetColor(100, 90, 50); ofFill(); ofDrawEllipse(ofGetWidth()/2, ofGetHeight()/2, 100, 300); ofDrawLine(0, 0, ofGetWidth(), ofGetHeight()); ofPoint bob; bob.x = ofGetWidth()/2; bob.y = ofGetHeight()/2; float length = ofGetHeight()/4; float width = ofGetHeight()/4; diamond(bob, mouseX, mouseY); shapeChange(); }
void DiamondSquare::passDiamondSquare(PassSettings *settings) { // Pass diamond for (int x = settings->passSize / 2; x < settings->mapSize; x += settings->passSize) { for (int y = settings->passSize / 2; y < settings->mapSize; y += settings->passSize) { diamond(x, y, settings); } } // Pass square for (int x = 0; x < settings->mapSize; x += settings->passSize / 2) { for (int y = settings->passSize / 2; y < settings->mapSize; y += settings->passSize) { square(x, y, settings); } x += settings->passSize / 2; if (x < settings->mapSize) { for (int y = 0; y < settings->mapSize; y += settings->passSize) { square(x, y, settings); } } } }
int main() { int r = inputInt("please input the radius: "); diamond(r); return 0; }
int main(void) { int width = 0; int height = 0; int diagonal = 0; char nom[FILENAME_MAX+1] = ""; int len = 0; Image mask = init_mask(); /** * Demande à l'utilisateur : * - le nom du fichier * - la hauteur de l'image * - la largueur de l'image * - la longueur de la diagonal du losange */ do { printf("Entrez un nomn de fichier : "); fgets(nom, FILENAME_MAX+1, stdin); len = strlen(nom) - 1; if ((len >= 0) && nom[len] == '\n') { nom[len] = '\0'; } else { while(!feof(stdin) && !ferror(stdin) && getc(stdin) != '\n'); } } while ((len < 1) && !feof(stdin) && !ferror(stdin)); char c_par = ' '; // si c_par != '\n', cela veut dire que l'utilisateur a entré des lettres et des chiffres while(height <= 0 || height > MAX_IMAGE_HEIGHT || c_par != '\n') { printf("Hauteur de l'image (max : %d) : ", MAX_IMAGE_HEIGHT); scanf("%d%c", &height, &c_par); if(c_par != '\n') { fprintf(stderr, "Erreur: entrez seulement des nombres !\n"); while(!feof(stdin) && !ferror(stdin) && getc(stdin) != '\n'); } } while(width <= 0 || width > MAX_IMAGE_WIDTH || c_par != '\n') { printf("Largeur de l'image (max : %d) : ", MAX_IMAGE_WIDTH); scanf("%d%c", &width, &c_par); if(c_par != '\n') { fprintf(stderr, "Erreur: entrez seulement des nombres !\n"); while(!feof(stdin) && !ferror(stdin) && getc(stdin) != '\n'); } } if(width % 2 == 0 || height % 2 == 0) { if (width == 100) { // si la dimension = 100, elle sera corrigée vers le bas width = 99; } if(height == 100) { height = 99; } width |= 1; //force les valeurs à être impaires height |= 1; printf("Les dimmensions ont été ajustées à des valeurs impaires. \n"); printf("Hauteur : %d, Largeur : %d\n", height, width); } do { printf("Diagonal du losange : "); scanf("%d%c", &diagonal, &c_par); if(c_par != '\n') { fprintf(stderr, "Erreur: entrez seulement des nombres !\n"); while(!feof(stdin) && !ferror(stdin) && getc(stdin) != '\n'); } } while(diagonal < 0 || (diagonal > height && diagonal > width) || c_par != '\n'); if(diagonal % 2 == 0 && (diagonal == height + 1 || diagonal == width + 1)) { // si la diagonale est égale à une des dimensions entrées (avant ajustement), elle sera corrigée diagonal = diagonal - 1; } /** * dessin du losange et affichage de l'image */ Image i = diamond(height, width, diagonal); display(stdout, i); /** * écriture de l'image dans un fichier puis lecture du fichier */ write_to_file(nom, i); Image r = read_from_file(nom); display(stdout, r); /** * application du filtre et affichage du résultat */ Image res = filter(i, mask); display(stdout, res); return 0; }
void FlowPart::orientationPixmap(uint orientation, QPixmap & pm) const { const QSize size = pm.size(); if (!(allowedOrientations() & (1 << orientation))) { kdWarning() << k_funcinfo << "Requesting invalid orientation of " << orientation << endl; return; } QBitmap mask(50, 50); QPainter maskPainter(&mask); mask.fill(Qt::color0); maskPainter.setBrush(Qt::color1); maskPainter.setPen(Qt::color1); QPainter p(&pm); // p.setBrush(m_brushCol); p.setPen(Qt::black); // In order: right corner, top corner, left corner, bottom corner QPoint c[4] = { QPoint(int(0.7*size.width()), int(0.5*size.height())), QPoint(int(0.5*size.width()), int(0.4*size.height())), QPoint(int(0.3*size.width()), int(0.5*size.height())), QPoint(int(0.5*size.width()), int(0.6*size.height())) }; QPoint d[4]; d[0] = c[0] + QPoint(7, 0); d[1] = c[1] + QPoint(0, -7); d[2] = c[2] + QPoint(-7, 0); d[3] = c[3] + QPoint(0, 7); if (m_stdInput && m_stdOutput && m_altOutput) { //BEGIN Draw diamond outline QPointArray diamond(4); for (uint i = 0; i < 4; ++i) diamond[i] = c[i]; p.drawPolygon(diamond); maskPainter.drawPolygon(diamond); //END Draw diamond outline //BEGIN Draw input int pos0 = nodeDirToPos(diamondNodePositioning[orientation][0]); p.drawLine(c[pos0], d[pos0]); maskPainter.drawLine(c[pos0], d[pos0]); //END Draw input //BEGIN Draw "true" output as a tick QPointArray tick(4); tick[0] = QPoint(-3, 0); tick[1] = QPoint(0, 2); tick[2] = QPoint(0, 2); tick[3] = QPoint(4, -2); int pos1 = nodeDirToPos(diamondNodePositioning[orientation][1]); tick.translate(d[pos1].x(), d[pos1].y()); p.drawLineSegments(tick); maskPainter.drawLineSegments(tick); //END Draw "true" output as a tick //BEGIN Draw "false" output as a cross QPointArray cross(4); cross[0] = QPoint(-2, -2); cross[1] = QPoint(2, 2); cross[2] = QPoint(-2, 2); cross[3] = QPoint(2, -2); int pos2 = nodeDirToPos(diamondNodePositioning[orientation][2]); cross.translate(d[pos2].x(), d[pos2].y()); p.drawLineSegments(cross); maskPainter.drawLineSegments(cross); //END Draw "false" output as a cross } else if (m_stdInput || m_stdOutput) { p.drawRoundRect(int(0.3*size.width()), int(0.4*size.height()), int(0.4*size.width()), int(0.2*size.height())); maskPainter.drawRoundRect(int(0.3*size.width()), int(0.4*size.height()), int(0.4*size.width()), int(0.2*size.height())); int hal = 5; // half arrow length int haw = 3; // half arrow width QPoint arrows[4][6] = { { QPoint(hal, 0), QPoint(0, -haw), QPoint(hal, 0), QPoint(-hal, 0), QPoint(hal, 0), QPoint(0, haw) }, { QPoint(0, -hal), QPoint(-haw, 0), QPoint(0, -hal), QPoint(0, hal), QPoint(0, -hal), QPoint(haw, 0) }, { QPoint(-hal, 0), QPoint(0, -haw), QPoint(-hal, 0), QPoint(hal, 0), QPoint(-hal, 0), QPoint(0, haw) }, { QPoint(0, hal), QPoint(-haw, 0), QPoint(0, hal), QPoint(0, -hal), QPoint(0, hal), QPoint(haw, 0) } }; int inPos = -1; int outPos = -1; if (m_stdInput && m_stdOutput) { inPos = nodeDirToPos(inOutNodePositioning[orientation][0]); outPos = nodeDirToPos(inOutNodePositioning[orientation][1]); } else if (m_stdInput) { inPos = nodeDirToPos(inNodePositioning[orientation]); } else if (m_stdOutput) { outPos = nodeDirToPos(outNodePositioning[orientation]); } if (inPos != -1) { QPointArray inArrow(6); for (int i = 0; i < 6; ++i) { inArrow[i] = arrows[(inPos + 2) % 4][i]; } inArrow.translate(d[inPos].x(), d[inPos].y()); p.drawPolygon(inArrow); maskPainter.drawPolygon(inArrow); } if (outPos != -1) { QPointArray outArrow(6); for (int i = 0; i < 6; ++i) { outArrow[i] = arrows[outPos][i]; } outArrow.translate(d[outPos].x(), d[outPos].y()); p.drawPolygon(outArrow); maskPainter.drawPolygon(outArrow); } } pm.setMask(mask); }
void test_diamond (void) { diamond(5); }
int main() { // Read input image cv::Mat image= cv::imread(IMAGE_FOLDER "/building.jpg",0); if (!image.data) return 0; // resize for book printing cv::resize(image, image, cv::Size(), 0.7, 0.7); // Display the image cv::namedWindow("Image"); cv::imshow("Image",image); // Create the morphological features instance MorphoFeatures morpho; morpho.setThreshold(40); // Get the edges cv::Mat edges; edges= morpho.getEdges(image); // Display the edge image cv::namedWindow("Edge Image"); cv::imshow("Edge Image",edges); // Get the corners morpho.setThreshold(-1); cv::Mat corners; corners= morpho.getCorners(image); cv::morphologyEx(corners,corners,cv::MORPH_TOPHAT,cv::Mat()); cv::threshold(corners, corners, 35, 255, cv::THRESH_BINARY_INV); // Display the corner image cv::namedWindow("Corner Image"); cv::imshow("Corner Image",corners); // Display the corner on the image morpho.drawOnImage(corners,image); cv::namedWindow("Corners on Image"); cv::imshow("Corners on Image",image); // Read and display image of square image= cv::imread(IMAGE_FOLDER "/square.bmp",0); cv::namedWindow("Square Image"); cv::imshow("Square Image",image); // Creating the cross-shaped structuring element cv::Mat cross(5,5,CV_8U,cv::Scalar(0)); for (int i=0; i<5; i++) { cross.at<uchar>(2,i)= 1; cross.at<uchar>(i,2)= 1; } // Dilate with a cross cv::Mat result; cv::dilate(image,result,cross); // Display the result cv::namedWindow("Dilated square with cross"); cv::imshow("Dilated square with cross",result); // Creating the diamond-shaped structuring element cv::Mat diamond(5,5,CV_8U,cv::Scalar(1)); diamond.at<uchar>(0,0)= 0; diamond.at<uchar>(0,1)= 0; diamond.at<uchar>(1,0)= 0; diamond.at<uchar>(4,4)= 0; diamond.at<uchar>(3,4)= 0; diamond.at<uchar>(4,3)= 0; diamond.at<uchar>(4,0)= 0; diamond.at<uchar>(4,1)= 0; diamond.at<uchar>(3,0)= 0; diamond.at<uchar>(0,4)= 0; diamond.at<uchar>(0,3)= 0; diamond.at<uchar>(1,4)= 0; // Erode with a diamond cv::Mat result2; cv::erode(result,result2,diamond); // Display the result cv::namedWindow("Eroded square with diamond"); cv::imshow("Eroded square with diamond",result2); // Combine the images into one cv::Mat final(100,300,CV_8U); cv::Mat window= final(cv::Rect(0,0,100,100)); image.copyTo(window); window= final(cv::Rect(100,0,100,100)); result.copyTo(window); window= final(cv::Rect(200,0,100,100)); result2.copyTo(window); // Display the combined result cv::namedWindow("Combined"); cv::imshow("Combined",final); // Save combined result cv::imwrite("squares.bmp",final); cv::waitKey(); return 0; }
// // Классификация элементов int classify(void) /*----------------------------------------------------------+ | This function searches through all elements every time. | | Some optimisation will definitely bee needed | | | | But it also must me noted, that this function defines | | the strategy for insertion of new nodes | | | | It's MUCH MUCH better when the ugliest element is found | | as one with highest ratio of R/r !!! (before it was | | element with greater R) | +----------------------------------------------------------*/ { int e, ei, ej, ek,si,sj,sk,ugly; double ratio=-GREAT, F; ugly=OFF; for(e=0; e<Ne; e++) if(elem[e].mark!=OFF) { ei=elem[e].ei; ej=elem[e].ej; ek=elem[e].ek; F=(node[elem[e].i].F + node[elem[e].j].F + node[elem[e].k].F)/3.0; elem[e].state=WAITING; /*--------------------------+ | 0.577 is ideal triangle | +--------------------------*/ if(elem[e].R < 0.700*F) elem[e].state=DONE; /* 0.0866; 0.07 */ /*------------------------+ | even this is possible | +------------------------*/ if(ei!=OFF && ej!=OFF && ek!=OFF) if(elem[ei].state==DONE && elem[ej].state==DONE && elem[ek].state==DONE) elem[e].state=DONE; } /*--------------------------------------+ | Diamond check. Is it so important ? | +--------------------------------------*/ diamond(); /*------------------------------------------------+ | First part of the trick: | | search through the elements on the boundary | +------------------------------------------------*/ for(e=0; e<Ne; e++) if(elem[e].mark!=OFF && elem[e].state!=DONE) { si=elem[e].si; sj=elem[e].sj; sk=elem[e].sk; if(side[si].mark!=0) elem[e].state=ACTIVE; if(side[sj].mark!=0) elem[e].state=ACTIVE; if(side[sk].mark!=0) elem[e].state=ACTIVE; if(elem[e].state==ACTIVE && elem[e].R/elem[e].r > ratio) {ratio=max(ratio, elem[e].R/elem[e].r); ugly=e;} } /*-------------------------------------------------+ | Second part of the trick: | | if non-acceptable element on the boundary is | | found, ignore the elements inside the domain | +-------------------------------------------------*/ if(ugly==OFF) for(e=0; e<Ne; e++) if(elem[e].mark!=OFF) { if(elem[e].state!=DONE) { ei=elem[e].ei; ej=elem[e].ej; ek=elem[e].ek; if(ei!=OFF) if(elem[ei].state==DONE) elem[e].state=ACTIVE; if(ej!=OFF) if(elem[ej].state==DONE) elem[e].state=ACTIVE; if(ek!=OFF) if(elem[ek].state==DONE) elem[e].state=ACTIVE; if(elem[e].state==ACTIVE && elem[e].R/elem[e].r > ratio) {ratio=max(ratio, elem[e].R/elem[e].r); ugly=e;} } } g_ratio=ratio; return ugly; }
int main() { int r = inputInt("输入菱形半径"); diamond(r); return 0; }
int main(int argc, char **argv) { std::cout << diamond('E'); std::cout << diamond('G'); std::cout << diamond('C'); }
vector<PlateRegion> DetectorMorph::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest) { Mat frame_gray,frame_gray_cp; if (frame.channels() > 2) { cvtColor( frame, frame_gray, CV_BGR2GRAY ); } else { frame.copyTo(frame_gray); } frame_gray.copyTo(frame_gray_cp); blur(frame_gray, frame_gray, Size(5, 5)); vector<PlateRegion> detectedRegions; for (int i = 0; i < regionsOfInterest.size(); i++) { Mat img_open, img_result; Mat element = getStructuringElement(MORPH_RECT, Size(30, 4)); morphologyEx(frame_gray, img_open, CV_MOP_OPEN, element, cv::Point(-1, -1)); img_result = frame_gray - img_open; if (config->debugDetector && config->debugShowImages) { imshow("Opening", img_result); } //threshold image using otsu thresholding Mat img_threshold, img_open2; threshold(img_result, img_threshold, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY); if (config->debugDetector && config->debugShowImages) { imshow("Threshold Detector", img_threshold); } Mat diamond(5, 5, CV_8U, cv::Scalar(1)); diamond.at<uchar>(0, 0) = 0; diamond.at<uchar>(0, 1) = 0; diamond.at<uchar>(1, 0) = 0; diamond.at<uchar>(4, 4) = 0; diamond.at<uchar>(3, 4) = 0; diamond.at<uchar>(4, 3) = 0; diamond.at<uchar>(4, 0) = 0; diamond.at<uchar>(4, 1) = 0; diamond.at<uchar>(3, 0) = 0; diamond.at<uchar>(0, 4) = 0; diamond.at<uchar>(0, 3) = 0; diamond.at<uchar>(1, 4) = 0; morphologyEx(img_threshold, img_open2, CV_MOP_OPEN, diamond, cv::Point(-1, -1)); Mat rectElement = getStructuringElement(cv::MORPH_RECT, Size(13, 4)); morphologyEx(img_open2, img_threshold, CV_MOP_CLOSE, rectElement, cv::Point(-1, -1)); if (config->debugDetector && config->debugShowImages) { imshow("Close", img_threshold); waitKey(0); } //Find contours of possibles plates vector< vector< Point> > contours; findContours(img_threshold, contours, // a vector of contours CV_RETR_EXTERNAL, // retrieve the external contours CV_CHAIN_APPROX_NONE); // all pixels of each contours //Start to iterate to each contour founded vector<vector<Point> >::iterator itc = contours.begin(); vector<RotatedRect> rects; //Remove patch that are no inside limits of aspect ratio and area. while (itc != contours.end()) { //Create bounding rect of object RotatedRect mr = minAreaRect(Mat(*itc)); if (mr.angle < -45.) { mr.angle += 90.0; swap(mr.size.width, mr.size.height); } if (!CheckSizes(mr)) itc = contours.erase(itc); else { ++itc; rects.push_back(mr); } } //Now prunning based on checking all candidate plates for a min/max number of blobsc Mat img_crop, img_crop_b, img_crop_th, img_crop_th_inv; vector< vector< Point> > plateBlobs; vector< vector< Point> > plateBlobsInv; double thresholds[] = { 10, 40, 80, 120, 160, 200, 240 }; const int num_thresholds = 7; int numValidChars = 0; Mat rotated; for (int i = 0; i < rects.size(); i++) { numValidChars = 0; RotatedRect PlateRect = rects[i]; Size rect_size = PlateRect.size; // get the rotation matrix Mat M = getRotationMatrix2D(PlateRect.center, PlateRect.angle, 1.0); // perform the affine transformation warpAffine(frame_gray_cp, rotated, M, frame_gray_cp.size(), INTER_CUBIC); //Crop area around candidate plate getRectSubPix(rotated, rect_size, PlateRect.center, img_crop); if (config->debugDetector && config->debugShowImages) { imshow("Tilt Correction", img_crop); waitKey(0); } for (int z = 0; z < num_thresholds; z++) { cv::threshold(img_crop, img_crop_th, thresholds[z], 255, cv::THRESH_BINARY); cv::threshold(img_crop, img_crop_th_inv, thresholds[z], 255, cv::THRESH_BINARY_INV); findContours(img_crop_th, plateBlobs, // a vector of contours CV_RETR_LIST, // retrieve the contour list CV_CHAIN_APPROX_NONE); // all pixels of each contours findContours(img_crop_th_inv, plateBlobsInv, // a vector of contours CV_RETR_LIST, // retrieve the contour list CV_CHAIN_APPROX_NONE); // all pixels of each contours int numBlobs = plateBlobs.size(); int numBlobsInv = plateBlobsInv.size(); float idealAspect = config->avgCharWidthMM / config->avgCharHeightMM; for (int j = 0; j < numBlobs; j++) { cv::Rect r0 = cv::boundingRect(cv::Mat(plateBlobs[j])); if (ValidateCharAspect(r0, idealAspect)) numValidChars++; } for (int j = 0; j < numBlobsInv; j++) { cv::Rect r0 = cv::boundingRect(cv::Mat(plateBlobsInv[j])); if (ValidateCharAspect(r0, idealAspect)) numValidChars++; } } //If too much or too lcittle might not be a true plate //if (numBlobs < 3 || numBlobs > 50) continue; if (numValidChars < 4 || numValidChars > 50) continue; PlateRegion PlateReg; // Ensure that the rectangle isn't < 0 or > maxWidth/Height Rect bounding_rect = PlateRect.boundingRect(); PlateReg.rect = expandRect(bounding_rect, 0, 0, frame.cols, frame.rows); detectedRegions.push_back(PlateReg); } } return detectedRegions; }
//fonction main. int main(){ //initialisation des variables. int height = 0; int width = 0; int diagonal = 0; //On demande à l'utilisateur d'entrer des données (hauteur, largeur et diagonale du losange). Et on contrôle ses entrées! int j = 0; do{ printf("Entrez une hauteur positive et plus petite que %d, et impaire si possible: ", MAX_IMAGE_HEIGHT); fflush(stdout); j = scanf("%d", &height); if (j != 1) { printf("Je vous ai demandé un nombre !\n"); while (!feof(stdin) && !ferror(stdin) && getc(stdin) != '\n'); } }while (!feof(stdin) && !ferror(stdin) && ((j!=1) || (height<1) || (height>MAX_IMAGE_HEIGHT))); do{ printf("Entrez une largeur positive et plus petite que %d, et impaire si possible: ", MAX_IMAGE_WIDTH); fflush(stdout); j = scanf("%d", &width); if (j != 1) { printf("Je vous ai demandé un nombre !\n"); while (!feof(stdin) && !ferror(stdin) && getc(stdin) != '\n'); } }while (!feof(stdin) && !ferror(stdin) && ((j!=1) || (width<1) || (width>MAX_IMAGE_WIDTH))); do{ printf("Entrez une diagonale positive et plus petite que la largeur et la hauteur: "); j = scanf("%d", &diagonal); if (j != 1) { printf("Je vous ai demandé un nombre !\n"); while (!feof(stdin) && !ferror(stdin) && getc(stdin) != '\n'); } }while(!feof(stdin) && !ferror(stdin) && !(diagonal > 0 && diagonal <= height && diagonal <= width && j == 1)); //définitiondes variables utiles à la récupération d'un nom de fichier char filename[FILENAME_MAX+1] = ""; int lg; //On demande à l'utilisateur un nom de fichier. do { printf("Dans quel fichier voulez vous écrire ? \n"); //contrôle de la validité du nom du fichier. fgets(filename, FILENAME_MAX+1, stdin); lg = strlen(filename) - 1; if ((lg >= 0) && (filename[lg] == '\n')){ filename[lg] = '\0'; } } while ((lg < 1) && !feof(stdin) && !ferror(stdin) ); //définition de l'image. Image image = diamond(height,width,diagonal); //définition du masque. Image mask; mask.width = 3; mask.height = 3; for(int i = 0; i<mask.width; ++i){ for(int j = 0; j < mask.height; ++j){ mask.pixels[i][j] = 2 * (j-1); } } //filtrage de l'image. image = filter(image, mask); //écriture de l'image dans le fichier donné par l'utilisateur. write_to_file(filename, image); //lecture de l'image depuis le fichier. Image img_from_file = read_from_file(filename); //ouverture du fichier tampon qui nous sert à afficher l'image sans toucher au fichier dans lequel l'image est stockée. FILE* file = fopen("display.txt", "w"); if (file == NULL) { fprintf(stderr, "ERREUR: Le fichier \"display.txt\" n'a pas pu être ouvert !\n"); } //remplissage du fichier tampon. display(file, img_from_file); //fermeture du fichier tampon. fclose(file); //réouverture du fichier tampon en mode lecture pour afficher son contenu. file = fopen("display.txt", "r"); if (file == NULL) { fprintf(stderr, "ERREUR: Le fichier \"display.txt\" ne peut pas être ouvert.\n"); } //affichage du contenu du fichier tampon. char c; while((c = fgetc(file))!= EOF){ printf("%c", c); } fclose(file); return 0; }