www.pudn.com > ColorSegment.rar > colorseg.cpp


#include "cv.h" 
#include "highgui.h" 
#include "iplwrapper.h" 
 
IplImage* colorSeg(IplImage* src, int h1, int h2, int s1, int s2, int v1, int v2) 
{ 
	int th_h1 = h1; 
	int th_h2 = h2; 
	int th_s1 = s1; 
	int th_s2 = s2; 
	int th_v1 = v1; 
	int th_v2 = v2; 
	IplImage* dst = cvCreateImage(cvGetSize(src), 8, 1); 
		 
    IplImage* h_plane = cvCreateImage( cvGetSize(src), 8, 1 ); 
    IplImage* s_plane = cvCreateImage( cvGetSize(src), 8, 1 ); 
    IplImage* v_plane = cvCreateImage( cvGetSize(src), 8, 1 ); 
    IplImage* hsv = cvCreateImage( cvGetSize(src), 8, 3 ); 
 
	cvCvtColor( src, hsv, CV_BGR2YCrCb ); 
    cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 ); 
	if(src->origin == 1) 
	{ 
		cvFlip(h_plane,h_plane); 
		cvFlip(s_plane,s_plane); 
		cvFlip(v_plane,v_plane); 
	} 
 
	Image8UC1 h_img(h_plane); 
	Image8UC1 s_img(s_plane); 
	Image8UC1 v_img(v_plane); 
 
	Image8UC1 dst_img(dst); 
 
	int W = src->width; 
	int H = src->height; 
	int i,j; 
/* 
	for(i = 0; i < H; i++) 
	{ 
		for(j = 0; j < W; j++) 
		{ 
			cout << setw(4) << int(h_img[i][j]); 
		} 
		cout << endl; 
	} 
*/ 
 
	for(i = 0; i < H; i++) 
	{ 
		for(j = 0; j < W; j++) 
		{ 
			int hval = h_img[i][j]; 
			if(hval > th_h1 && hval < th_h2) 
			{ 
				int sval = s_img[i][j]; 
				if(sval > th_s1 && sval < th_s2) 
				{ 
					int vval = v_img[i][j]; 
					if(vval > th_v1 && vval < th_v2) 
					{ 
						dst_img[i][j] = 255; 
					} 
					else 
					{ 
						dst_img[i][j] = 0; 
					} 
				} 
				else 
				{ 
					dst_img[i][j] = 0; 
				} 
 
			} 
			else 
			{ 
				dst_img[i][j] = 0; 
			} 
		} 
	} 
 
//	cvNamedWindow("h",1); 
//	cvNamedWindow("s",1); 
//	cvNamedWindow("v",1); 
//	cvShowImage("h",h_plane); 
//	cvShowImage("s",s_plane); 
//	cvShowImage("v",v_plane); 
 
	cvReleaseImage(&h_plane); 
	cvReleaseImage(&s_plane); 
	cvReleaseImage(&v_plane);	 
	cvReleaseImage(&hsv); 
 
	return dst; 
} 
/* 
 
void main() 
{ 
	CvCapture* capture = NULL;	 
	capture = cvCaptureFromAVI("F:\\Road data\\vip28.avi"); 
 
	if(capture == NULL) 
	{ 
		cout << "fail to capture from avi...\n"; 
		return; 
	} 
 
	IplImage* frame = cvQueryFrame(capture); 
	if(frame == NULL) return; 
	int numFrames = (int) cvGetCaptureProperty(capture,  CV_CAP_PROP_FRAME_COUNT); 
 
	cvNamedWindow("src",1); 
	cvNamedWindow("dst",1); 
	int th_h1 = 0; 
	int th_h2 = 255; 
	int th_s1 = 0; 
	int th_s2 = 255; 
	int th_v1 = 0; 
	int th_v2 = 255; 
	cvCreateTrackbar("track h1", "dst", &th_h1, 255, NULL); 
	cvCreateTrackbar("track h2", "dst", &th_h2, 255, NULL); 
	cvCreateTrackbar("track s1", "dst", &th_s1, 255, NULL); 
	cvCreateTrackbar("track s2", "dst", &th_s2, 255, NULL); 
	cvCreateTrackbar("track v1", "dst", &th_v1, 255, NULL); 
	cvCreateTrackbar("track v2", "dst", &th_v2, 255, NULL); 
 
	int iKey  = -1; 
	int frameNum = 0; 
	do 
	{ 
		frameNum++; 
		frame = cvQueryFrame(capture); 
		if(frame == NULL) return; 
 
		IplImage* dst = colorSeg(frame, th_h1, th_h2, th_s1, th_s2, th_v1, th_v2); 
		cvShowImage("dst", dst); 
		cvShowImage("src",frame); 
		 
		iKey=cvWaitKey(10); 
 
		if(iKey == 13) 
		{ 
			frameNum = 1; 
			cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES, frameNum); 
		} 
 
		if(iKey == 68) 
		{ 
			frameNum -= 100; 
			if(frameNum < 1) frameNum = 1; 
			cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES, frameNum); 
		} 
		if(iKey == 32) 
		{ 
			frameNum += 100; 
			if(frameNum > numFrames) frameNum = numFrames -1; 
			cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES, frameNum); 
		} 
 
		cvReleaseImage(&dst); 
		 
	}while(iKey!=27); 
 
	cvReleaseCapture(&capture); 
	cvDestroyAllWindows(); 
 
} 
 
*/ 
/* 
void main() 
{ 
	IplImage* frame = cvLoadImage("up.bmp"); 
	cvFlip(frame); 
 
	cvNamedWindow("src",1); 
	cvNamedWindow("dst",1); 
 
 
	int th_h1 = 0; 
	int th_h2 = 255; 
	int th_s1 = 0; 
	int th_s2 = 255; 
	int th_v1 = 0; 
	int th_v2 = 255; 
//	cvCreateTrackbar("track h1", "dst", &th_h1, 255, NULL); 
//	cvCreateTrackbar("track h2", "dst", &th_h2, 255, NULL); 
	cvCreateTrackbar("track Cr", "dst", &th_s1, 255, NULL); 
//	cvCreateTrackbar("track Cr2", "dst", &th_s2, 255, NULL); 
//	cvCreateTrackbar("track v1", "dst", &th_v1, 255, NULL); 
//	cvCreateTrackbar("track v2", "dst", &th_v2, 255, NULL); 
 
	int iKey  = -1; 
	int frameNum = 0; 
	while(1) 
	{ 
		IplImage* dst = colorSeg(frame, th_h1, th_h2, th_s1, th_s2, th_v1, th_v2); 
		cvShowImage("dst", dst); 
		cvShowImage("src",frame); 
 
		iKey = cvWaitKey(20); 
 
		if(iKey == 27) 
			break; 
		else if(iKey == 's') 
		{ 
			cvSaveImage("result.bmp",dst); 
			cvWaitKey(10); 
		} 
 
		cvReleaseImage(&dst); 
	} 
		 
} 
 
*/ 
 
/* 
 
void main() 
{ 
	IplImage* frame = cvLoadImage("erode2.jpg",0); 
	int W = frame->width; 
	int H = frame->height; 
	IplImage* src = cvCreateImage(cvSize(W,H), 8, 1); 
	IplImage* dst = cvCreateImage(cvSize(W,H), 8, 1); 
	IplImage* cpy = cvCreateImage(cvSize(W,H), 8, 1); 
 
	CvMemStorage* storage = cvCreateMemStorage(0); 
	CvSeq* contours; 
	CvSeq* result; 
	CvSeq* squares = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvPoint), storage ); 
	CvSeqReader reader; 
//	cvResize(frame,dst); 
//	cvThreshold(frame, src, 100, 255, CV_THRESH_BINARY_INV); 
	 
//	cvSaveImage("thres.jpg", src); 
	cvNamedWindow("src",1); 
	cvShowImage("src",src); 
 
	cvWaitKey(10); 
 
	cvNamedWindow("dst",1); 
	cvNamedWindow("edge",1); 
 
	cvErode(src, dst); 
 
//	cvCanny(src,cpy, 50, 200); 
	cvShowImage("edge",dst); 
//	cvThreshold(dst,dst,120,255,CV_THRESH_BINARY); 
//ThinnerPavlidis(void *image, unsigned long lx, unsigned long ly); 
//ThinnerRosenfeld(void *image, unsigned long lx, unsigned long ly); 
	cvFindContours( dst, storage, &contours, sizeof(CvContour), 
                1, 4, cvPoint(0,0) ); 
	// test each contour 
	int count; 
	while( contours ) 
	{ 
		// approximate contour with accuracy proportional 
		// to the contour perimeter 
		result = cvApproxPoly( contours, sizeof(CvContour), storage, 
			CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0 ); 
		 
		count = result->total; 
//		cvPolyLine( cpy, ((CvPoint**)result->first), &count, 1, 1, CV_RGB(0,255,0), 3, CV_AA, 0 ); 
 
		contours = contours->h_next; 
	} 
 
 
//	cvSaveImage("erode.jpg",dst); 
 
	cvShowImage("dst",dst); 
 
	cvWaitKey(); 
} 
*/