Click here to Skip to main content
16,015,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i using this code to make (text) image segmentation

C++
#include <cv.h>
#include <highgui.h>
#include <cvaux.h>
#include <stdio.h>

const char *WIN1="Segmented";  // This will be the window name

IplImage* imagen;
IplImage* imagen_color;
IplImage* smooth;
IplImage* threshold;
IplImage* open_morf;
IplImage* blobResult;
IplImage* img_contornos;
CvMoments moments;
CvHuMoments humoments;
CvSeq* contour;
CvSeq* contourLow;

int main()
{
		
	/* Image PATH here */
	char* filename= "C:\\Users\\Mostafa\\Desktop\\Untitled.jpg";

	/* load image  in gray level */
	imagen = cvLoadImage(filename,0); // 0 value here is for gray

	/* to load the image in RGB ||  1 value here is for RGB */
	imagen_color = cvLoadImage(filename,1); // this colored image is the one to be display

	/* Creating the edited images: smooth, threshold and open_morf */
	smooth= cvCreateImage(cvSize(imagen->width, imagen->height), IPL_DEPTH_8U, 1);
	threshold= cvCreateImage(cvSize(imagen->width, imagen->height), IPL_DEPTH_8U, 1);
	open_morf= cvCreateImage(cvSize(imagen->width, imagen->height), IPL_DEPTH_8U, 1);
	
	//Initializaing the variables for countours
	contour = 0;
	contourLow = 0;

	//Create storage needed for contour detection
	CvMemStorage* storage = cvCreateMemStorage(0);

	//Creating window, WIN1 = "Segmented" the window name we described early in this code :1:
	cvNamedWindow(WIN1); // or cvNamedWindow(WIN1, 0) as u like


	//Smooth image
	cvSmooth(imagen, smooth, CV_GAUSSIAN, 3, 0, 0, 0);
	
	CvScalar avg;
	CvScalar avgStd;
	cvAvgSdv(smooth, &avg, &avgStd, NULL);
	

	//threshold image, this is an adaptive threshold
	cvThreshold(smooth, threshold, (int)avg.val[0]-7*(int)(avgStd.val[0]/8), 255, CV_THRESH_TOZERO_INV);
		//CV_THRESH_BINARY_INV);	/*_||_*/

	//Applying Morfologic filters
	cvErode(threshold, open_morf, NULL,1); 
	cvDilate(open_morf, open_morf, NULL,1); 

	//Duplicate image for countour
	img_contornos = cvCloneImage(open_morf);

	//Search countours in preprocesed image
	cvFindContours( img_contornos, storage, &contour, sizeof(CvContour),
			CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0) );

	//Optimize contours, reduce points
	contourLow=cvApproxPoly(contour, sizeof(CvContour), storage,CV_POLY_APPROX_DP,1,1);
	

	//For each contour found
	for( ; contourLow != 0; contourLow = contourLow->h_next )
	{
		CvScalar color = CV_RGB( rand()&200, rand()&200, rand()&200 ); //Generate Random colors which is used to draw the rectangles around the letters

		//We can draw the contour of object
		cvDrawContours( imagen_color, contourLow, color, color, -1, 0, 8, cvPoint(0,0) );

		//Or detect bounding rect of contour	
		CvRect rect;
		CvPoint pt1, pt2;
		rect=cvBoundingRect(contourLow, NULL);
		pt1.x = rect.x;
        pt2.x = (rect.x+rect.width);
        pt1.y = rect.y;
        pt2.y = (rect.y+rect.height);

		cvRectangle(imagen_color, pt1,pt2, color, 1, 8, 0); //the rectangle is drawn on the Colored Image
		
		/*For calculate objects features we can use hu moments.
		this calculate 7 features that are independent of position, size and orientation*/

		//First calculate object moments
		cvMoments(contourLow, &moments, 0);

		//Now calculate hu moments
		cvGetHuMoments(&moments, &humoments);

	}

	cvShowImage(WIN1, imagen_color);
	cvWaitKey(0);

	/*
	//Main Loop
	for(;;) 
	{
		int c;

		c = cvWaitKey(10);
		if( (char) c == 27 )
			break;
		else if((char) c == '3')
			cvShowImage(WIN1, threshold);
		else if((char) c=='1')
			cvShowImage(WIN1, imagen);
		else if((char) c=='2')
			cvShowImage(WIN1, smooth);
		else if((char) c=='4')
			cvShowImage(WIN1, open_morf);
		else if((char) c=='r')
			cvShowImage(WIN1, imagen_color);

	}
	*/

	cvDestroyWindow(WIN1);
	//cvReleaseImage(&imagen_color); // optional :-)

	return 0;
}


but when i run this code didn't make segmentation to the dark background image
or colored background image

any help in this code
Thanks .
Posted
Comments
nv3 26-Mar-12 16:13pm    
It is very hard for someone else to analyze your program without any further information. Unfortunately we cannot paste images in this forum, which would make things easier. So my first question would be: Is your image open_morf still correct? If yes, how many contours does cvFindContours return? I would try to draw these contours into the original image, so you can check whether they make sense. If all went ok to this point, we do the next step. How many objects does cvApproxPoly return? If you arrived there, you will probably know what went wrong.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900