void ooChains(char *ffaDir, char *ooDir)
/* ooChains - make chains (partially ordered clone fragments) for oo dir. */
{
struct hash *cloneHash = newHash(16);
struct clone *cloneList, *clone;
char fileName[512];
int i;
struct fileInfo *chromDir = NULL, *ctgDir = NULL, *chrom, *ctg;

/* Read in input from ffaDir. */
sprintf(fileName, "%s/%s", ffaDir, infoFile);
printf("Reading %s\n", fileName);
cloneList = readCloneList(fileName, cloneHash);
for (i=0; i<ArraySize(finfFiles); ++i)
    {
    sprintf(fileName, "%s/%s", ffaDir, finfFiles[i]);
    printf("Reading %s\n", fileName);
    readFinf(fileName, cloneHash);
    }
printf("Making chains\n");
for (clone = cloneList; clone != NULL; clone = clone->next)
    {
    slReverse(&clone->fragList);
    makeChains(clone);
    }

/* Make output in each of contig dirs. */
chromDir = listDirX(ooDir, "*", FALSE);
for (chrom = chromDir; chrom != NULL; chrom = chrom->next)
    {
    char *chromName = chrom->name;
    if (chrom->isDir && strlen(chromName) <= 2 && chromName[0] != '.')
        {
	printf("Processing %s\n", chromName);
	sprintf(fileName, "%s/%s", ooDir, chromName);
	ctgDir = listDirX(fileName, "ctg*", TRUE);
	for (ctg = ctgDir; ctg != NULL; ctg = ctg->next)
	    {
	    printf("."); 
	    fflush(stdout);
	    if (ctg->isDir)
	        writeRelevantChains(ctg->name, cloneHash);
	    }
	printf("\n");
	slFreeList(&ctgDir);
	}
    }
printf("Got chains in %d of %d files.  %d total links (including phase2)\n",
	clonesWithChainsCount, slCount(cloneList), linkCount);
}
Example #2
0
void TextDetector::detect(IplImage * input,
		const struct TextDetectionParams &params,
		std::vector<Chain> &chains,
		std::vector<std::pair<Point2d, Point2d> > &compBB,
		std::vector<std::pair<CvPoint, CvPoint> > &chainBB) {
	assert(input->depth == IPL_DEPTH_8U);
	assert(input->nChannels == 3);
	// Convert to grayscale
	IplImage * grayImage = cvCreateImage(cvGetSize(input), IPL_DEPTH_8U, 1);
	cvCvtColor(input, grayImage, CV_RGB2GRAY);
	// Create Canny Image
	double threshold_low = 175;
	double threshold_high = 320;
	IplImage * edgeImage = cvCreateImage(cvGetSize(input), IPL_DEPTH_8U, 1);
	cvCanny(grayImage, edgeImage, threshold_low, threshold_high, 3);
	cvSaveImage("canny.png", edgeImage);

	// Create gradient X, gradient Y
	IplImage * gaussianImage = cvCreateImage(cvGetSize(input), IPL_DEPTH_32F,
			1);
	cvConvertScale(grayImage, gaussianImage, 1. / 255., 0);
	cvSmooth(gaussianImage, gaussianImage, CV_GAUSSIAN, 5, 5);
	IplImage * gradientX = cvCreateImage(cvGetSize(input), IPL_DEPTH_32F, 1);
	IplImage * gradientY = cvCreateImage(cvGetSize(input), IPL_DEPTH_32F, 1);
	cvSobel(gaussianImage, gradientX, 1, 0, CV_SCHARR);
	cvSobel(gaussianImage, gradientY, 0, 1, CV_SCHARR);
	cvSmooth(gradientX, gradientX, 3, 3);
	cvSmooth(gradientY, gradientY, 3, 3);
	cvReleaseImage(&gaussianImage);

	// Calculate SWT and return ray vectors
	std::vector<Ray> rays;
	IplImage * SWTImage = cvCreateImage(cvGetSize(input), IPL_DEPTH_32F, 1);
	for (int row = 0; row < input->height; row++) {
		float* ptr = (float*) (SWTImage->imageData + row * SWTImage->widthStep);
		for (int col = 0; col < input->width; col++) {
			*ptr++ = -1;
		}
	}
	strokeWidthTransform(edgeImage, gradientX, gradientY, params, SWTImage,
			rays);
	cvSaveImage("SWT_0.png", SWTImage);
	SWTMedianFilter(SWTImage, rays);
	cvSaveImage("SWT_1.png", SWTImage);

	IplImage * output2 = cvCreateImage(cvGetSize(input), IPL_DEPTH_32F, 1);
	normalizeImage(SWTImage, output2);
	cvSaveImage("SWT_2.png", output2);
	IplImage * saveSWT = cvCreateImage(cvGetSize(input), IPL_DEPTH_8U, 1);
	cvConvertScale(output2, saveSWT, 255, 0);
	cvSaveImage("SWT.png", saveSWT);
	cvReleaseImage(&output2);
	cvReleaseImage(&saveSWT);

	// Calculate legally connected components from SWT and gradient image.
	// return type is a vector of vectors, where each outer vector is a component and
	// the inner vector contains the (y,x) of each pixel in that component.
	std::vector<std::vector<Point2d> > components =
			findLegallyConnectedComponents(SWTImage, rays);

	// Filter the components
	std::vector<std::vector<Point2d> > validComponents;
	std::vector<Point2dFloat> compCenters;
	std::vector<float> compMedians;
	std::vector<Point2d> compDimensions;
	filterComponents(SWTImage, components, validComponents, compCenters,
			compMedians, compDimensions, compBB, params);

	IplImage * output3 = cvCreateImage(cvGetSize(input), 8U, 3);
	renderComponentsWithBoxes(SWTImage, validComponents, compBB, output3);
	cvSaveImage("components.png", output3);
	cvReleaseImage ( &output3 );

	// Make chains of components
	chains = makeChains(input, validComponents, compCenters, compMedians,
			compDimensions, params);

	IplImage * output = cvCreateImage(cvGetSize(grayImage), IPL_DEPTH_8U, 3);
	renderChainsWithBoxes(SWTImage, validComponents, chains, compBB, chainBB, output);
	cvSaveImage("text-boxes.png", output);


    std::cout << "-------- detect end --------" << std::endl;

	cvReleaseImage(&output);
	cvReleaseImage(&gradientX);
	cvReleaseImage(&gradientY);
	cvReleaseImage(&SWTImage);
	cvReleaseImage(&edgeImage);
	cvReleaseImage(&grayImage);
	return;
}