Exemple #1
0
void FindRegionContour(IplImage* binaryimage,vector<REGION>& vec)
{
	Deburr(binaryimage);
	int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
	int i,j,k;
	bool** flag=new bool*[binaryimage->height];
	for(i=0;i<binaryimage->height;++i)
	{
		flag[i]=new bool[binaryimage->width];
		memset(flag[i],false,sizeof(false)*binaryimage->width);
	}
	for(i=0;i<binaryimage->height;++i)
	{
		for(j=0;j<binaryimage->width;++j)
		{
			if((unsigned char)binaryimage->imageData[i*binaryimage->widthStep+j]!=(unsigned char)0)
				continue;
			if(flag[i][j]) continue;
			queue<CvPoint> temp;
			REGION region;
			temp.push(cvPoint(j,i));
			flag[i][j]=true;
			while (!temp.empty())
			{
				CvPoint pt=temp.front();
				temp.pop();
				region.region.push_back(pt);
				for(k=0;k<GetArraySize(dir);++k)
				{
					int m=pt.y+dir[k][0];
					int n=pt.x+dir[k][1];
					if(m<0 || m>=binaryimage->height || n<0 || n>=binaryimage->width)
						continue;
					if((unsigned char)binaryimage->imageData[m*binaryimage->widthStep+n]!=0)
						continue;
					if(flag[m][n])
						continue;
					temp.push(cvPoint(n,m));
					flag[m][n]=true;
				}
			}
			int sumX=0,sumY=0;
			for(k=0;k<region.region.size();++k)
				if(IsBoundary(binaryimage,region.region[k]))
				{
					region.contour.push_back(region.region[k]);
					sumX+=region.region[k].x;
					sumY+=region.region[k].y;
				}
			region.centerpoint.x=sumX/region.contour.size();
			region.centerpoint.y=sumY/region.contour.size();
			vec.push_back(region);
		}
	}
	for(i=0;i<binaryimage->height;++i)
		delete [](flag[i]);
	delete []flag;
}
Exemple #2
0
QList<HTMLSpellCheck::MisspelledWord> HTMLSpellCheck::GetMisspelledWords(const QString &orig_text,
        int start_offset,
        int end_offset,
        const QString &search_regex,
        bool first_only,
        bool include_all_words)
{
    SpellCheck *sc = SpellCheck::instance();
    QString wordChars = sc->getWordChars();
    bool in_tag = false;
    bool in_invalid_word = false;
    bool in_entity = false;
    int word_start = 0;
    QRegularExpression search(search_regex);
    QList<HTMLSpellCheck::MisspelledWord> misspellings;
    // Make sure text has beginning/end boundary markers for easier parsing
    QString text = QChar(' ') + orig_text + QChar(' ');
    // Ignore <style...</style> wherever it appears - change to spaces to keep text positions
    QRegularExpression style_re("<style[^<]*</style>");

    QRegularExpressionMatchIterator i = style_re.globalMatch(text);
    while (i.hasNext()) {
        QRegularExpressionMatch match = i.next();
        for (int pos = match.capturedStart(); pos < match.capturedEnd(); pos++) {
            text[pos] = QChar(' ');
        }
    }

    for (int i = 0; i < text.count(); i++) {
        QChar c = text.at(i);

        if (!in_tag) {
            QChar prev_c = i > 0 ? text.at(i - 1) : QChar(' ');
            QChar next_c = i < text.count() - 1 ? text.at(i + 1) : QChar(' ');

            if (IsBoundary(prev_c, c, next_c, wordChars)) {
                // If we're in an entity and we hit a boundary and it isn't
                // part of an entity then this is an invalid entity.
                if (in_entity && c != QChar(';')) {
                    in_entity = false;
                }

                // Check possibilities that would mean this isn't a word worth considering.
                if (!in_invalid_word && !in_entity && word_start != -1 && (i - word_start) > 0) {
                    QString word = Utility::Substring(word_start, i, text);

                    if (!word.isEmpty() && word_start > start_offset && word_start <= end_offset) {
                        if (include_all_words || !sc->spell(word)) {
                            int cap_start = -1;

                            if (!search_regex.isEmpty()) {
                                QRegularExpressionMatch mo = search.match(word);
                                cap_start = mo.capturedStart();
                            }

                            if (search_regex.isEmpty() || cap_start != -1) {
                                struct MisspelledWord misspelled_word;
                                misspelled_word.text = word;
                                // Make sure we account for the extra boundary added at the beginning
                                misspelled_word.offset = word_start - 1;
                                misspelled_word.length = i - word_start ;
                                misspellings.append(misspelled_word);

                                if (first_only) {
                                    return misspellings;
                                }
                            }
                        }
                    }
                }

                // We want to start the word with the character after the boundary.
                // If the next character is another boundary we'll just move forward one.
                word_start = i + 1;
                in_invalid_word = false;
            } else {
                // Ensure we're not dealing with some crazy run on text that isn't worth
                // considering as an actual word.
                if (!in_invalid_word && (i - word_start) > MAX_WORD_LENGTH) {
                    in_invalid_word = true;
                }
            }

            if (c == QChar('&')) {
                in_entity = true;
            }

            if (c == QChar(';')) {
                in_entity = false;
            }
        }

        if (c == QChar('<')) {
            in_tag = true;
            word_start = -1;
        }

        if (in_tag && c == QChar('>')) {
            word_start = i + 1;
            in_tag = false;
        }
    }

    return misspellings;
}
Exemple #3
0
void DeleteAll8TypeArea(IplImage* srcImage,IplImage* binaryImage,vector< vector<CvPoint> >& AllOutline) 
{
	int i,j,k,m,n;
	int direction[8][2] = {{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}}; //  在图像中顺时针旋转
	bool **flag;                      // need to be released
	flag=new bool*[binaryImage->height];
    for(i=0;i<binaryImage->height;++i)
	{
		flag[i]=new bool[binaryImage->width];
		memset(flag[i],false,sizeof(bool)*binaryImage->width);
	}	
	for(i=0;i<binaryImage->height;++i)
	{
		for(j=0;j<binaryImage->width;++j)
		{
			int val=(byte)binaryImage->imageData[i*binaryImage->widthStep+j];
			if(val!=0) continue;
			for(k=0;k<GetArraySize(direction);++k)
			{
				int m=i+direction[k][0];
				int n=j+direction[k][1];
				if(m<0||n<0||m>=binaryImage->height||n>=binaryImage->width)
					continue;
				val=(byte)binaryImage->imageData[m*binaryImage->widthStep+n];
				if(val==0)
					break;
			}
			if(k==GetArraySize(direction))
				binaryImage->imageData[i*binaryImage->widthStep+j]=(unsigned char)255;
		}
	}
	for(i=0;i<binaryImage->height;++i)
		for(j=0;j<binaryImage->width;++j)
		{
			if(IsBoundary(binaryImage,cvPoint(j,i)) == false )
				continue;
			if(flag[i][j]) continue;
            vector<CvPoint> outline;
			outline.push_back(cvPoint(j,i));
			flag[i][j] = true;
			CvPoint StartPoint=cvPoint(j,i);
			int preDir=0;
            while ( !outline.empty() )
            {
				CvPoint pt=outline.back();
				int times;
				for(k=preDir,times=0;times<GetArraySize(direction);k=(k+1)%GetArraySize(direction),++times)
				{
					m=pt.y+direction[k][0];
					n=pt.x+direction[k][1];
					if(m<0||m>=binaryImage->height || n<0 || n>= binaryImage->width)
						continue;
					int val=(unsigned char)binaryImage->imageData[m*binaryImage->widthStep+n];
					if(val == 255)
						continue;
					if(flag[m][n] == false)
					{
						outline.push_back(cvPoint(n,m));
						flag[m][n]=true;
						preDir=k-GetArraySize(direction)/4;
						preDir=(preDir+GetArraySize(direction))%GetArraySize(direction);
						break;
					}
					if( cvPoint(n,m)== StartPoint )
					{
						AllOutline.push_back(outline);
						{							
							for(int i=0;i<outline.size();++i)
							{
								CvPoint pt=outline[i];
								srcImage->imageData[pt.y*srcImage->widthStep+pt.x]=0;
							}
							printf("first point:%d %d\n",outline[0].y,outline[0].x);
							cvShowImage("srcImage2",srcImage);
							cvWaitKey();
						}
                        outline.clear();
						break;
					}
					else
					{
						vector<CvPoint> SmallOutline;
						CvPoint temp=outline.back();
						vector<CvPoint>::reverse_iterator rit=outline.rbegin();
						while (*rit != temp)
						     ++rit;
						copy(outline.rbegin(),rit,back_insert_iterator< vector<CvPoint> >(SmallOutline));
						if(SmallOutline.size()>1)
						{
							AllOutline.push_back(SmallOutline);
							for(int i=0;i<SmallOutline.size();++i)
							{
								CvPoint pt=SmallOutline[i];
								srcImage->imageData[pt.y*srcImage->widthStep+pt.x]=0;
							}
							printf("first point:%d %d\n",SmallOutline[0].y,SmallOutline[0].x);
							cvShowImage("srcImage2",srcImage);
							cvWaitKey();
						}
					    outline.erase((++rit).base(),outline.end());
						binaryImage->imageData[temp.y*binaryImage->widthStep+temp.x]=(unsigned char)255;
						break;
					}
				}
			}
		}
	for(i=0;i<binaryImage->height;++i)
		delete [](flag[i]);
	delete []flag;

}