|
以下ソースコード全文です。 修正してみましたが、デバックも通らなくなりました・・・ お手上げです・・・
#include "stdafx.h" #include <cv.h> #include <highgui.h> #include <windows.h> #include <time.h>
CvMoments moment; FILE *result; IplImage* mask = 0; IplImage* mask1 = 0; int hsv = 0;
int GetMaskHSV(IplImage* src, IplImage* mask,int erosions, int dilations,int a,int old_hsv) { int x = 0, y = 0; uchar H, S, V; uchar minH, minS, minV, maxH, maxS, maxV;
CvPixelPosition8u pos_src, pos_dst; uchar* p_src; uchar* p_dst; IplImage* tmp;
tmp = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 3);
//HSVに変換 cvCvtColor(src, tmp, CV_RGB2HSV);
CV_INIT_PIXEL_POS(pos_src, (unsigned char*) tmp->imageData, tmp->widthStep,cvGetSize(tmp), x, y, tmp->origin);
CV_INIT_PIXEL_POS(pos_dst, (unsigned char*) mask->imageData, mask->widthStep, cvGetSize(mask), x, y, mask->origin);
if(a == 1){//閾値を設定 minH = 0; maxH = 255;//H(色相)の設定 minS = 0; maxS = 255;//S(彩度)の設定 minV = 0; maxV = 45;//V(明度)の設定 } else if(a == 0){ minH = 0; maxH = 0; minS = 0; maxS = 255; minV = 0; maxV = 0; }
for(y = 0; y < tmp->height; y++) { for(x = 0; x < tmp->width; x++) { p_src = CV_MOVE_TO(pos_src, x, y, 3); p_dst = CV_MOVE_TO(pos_dst, x, y, 3);
H = p_src[0]; S = p_src[1]; V = p_src[2];
if( minH <= H && H <= maxH && minS <= S && S <= maxS && minV <= V && V <= maxV ) { p_dst[0] = 255; p_dst[1] = 255; p_dst[2] = 255; hsv++; } else { p_dst[0] = 0; p_dst[1] = 0; p_dst[2] = 0; } } }
if(erosions > 0) cvErode(mask, mask, 0, erosions); if(dilations > 0) cvDilate(mask, mask, 0, dilations);
if(a == 0){ result = fopen("test69.txt","a"); printf("%d %d\n",hsv,old_hsv); fprintf(result,"%d\n",hsv); fclose(result);
if(hsv *10 <= old_hsv){
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,NULL); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,NULL);
return ; } } else { }
cvReleaseImage(&tmp); }
int _tmain(int argc, _TCHAR* argv[]) { int key; double sme; IplImage* frame; IplImage* frame1,* img,*back,* back1,*mask_img,*img_diff,*img_out2,*img_out3,*img_out4,*img_out5,*img_gray,*img_gray4; CvMat *mat_src,*mat_diff,*mat_back; CvCapture *src; CvVideoWriter*rec;
clock_t start, stop;
cvNamedWindow("in"); cvNamedWindow("out");
src = cvCaptureFromCAM(0);
if(src == NULL){ printf("ファイルが読み込めません。\n"); cvWaitKey(0); return -1; }
frame = cvQueryFrame(src); frame1 = cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_8U, 3);
back = cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_8U, 3); back1 = cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_8U, 3); mask_img = cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_8U, 1); img_diff = cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,3); mat_back = cvCreateMat(frame->height,frame->width,CV_32FC3); mat_diff = cvCreateMat(frame->height,frame->width,CV_32FC3); mat_src = cvCreateMat(frame->height,frame->width,CV_32FC3); img_out2 = cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,3); img_out3 = cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,3); img_out4 = cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,3); img_out5 = cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,3); img_gray = cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,1); img_gray4 = cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,1); mask = cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_8U, 3);
rec = cvCreateVideoWriter("Mov00hy.avi",-1,5, cvSize(frame->width, frame->height)); while(1){ frame = cvQueryFrame(src);
start = clock();
cvCopy(frame,frame1); cvConvert(frame,mat_src); cvAbsDiff(mat_src,mat_back,mat_diff); cvRunningAvg(mat_src,mat_back,0.99,0); cvConvert(mat_diff,img_diff);
cvCvtColor(img_diff,img_gray,CV_BGR2GRAY); cvCvtColor(img_gray,img_out2,CV_GRAY2BGR); cvNot(img_out2,img_out2); cvThreshold(img_out2,img_out4,230,255,CV_THRESH_BINARY);
cvNot(img_out4,img_out4);
cvSetImageCOI (img_out4, 1); //3チャンネル画像のためCOIをセット //動いているものの重心算出// cvMoments(img_out4,&moment,1); cvSetImageCOI (img_out4, 0); //COIの解除 double m00 = cvGetSpatialMoment(&moment, 0, 0); double m10 = cvGetSpatialMoment(&moment, 1, 0); double m01 = cvGetSpatialMoment(&moment, 0, 1); int gX = m10/m00; int gY = m01/m00; //ここまで// cvZero(mask_img); cvEllipse(mask_img, cvPoint(gX, gY),cvSize(120,60), 0, 360, 0,CV_RGB(0,255,255),-1,0); cvNot(mask_img,mask_img);
GetMaskHSV(frame, mask,1,1,1,0); cvOr(frame, mask,back); //cvNot(frame,back); cvCvtColor(img_out4,img_gray4,CV_BGR2GRAY); cvCvtColor(img_gray4,img_out5,CV_GRAY2BGR);
cvNot(img_out4,img_out5); cvAnd (frame1, img_out5, frame1); cvOr (frame1, img_out4, frame1); cvAnd (back, frame1,back); cvNot(back,back);
cvDilate(back,back1,NULL,2);
cvCircle(back, cvPoint(gX, gY), 10, CV_RGB(0,255,255), 6, 8, 0); cvEllipse(back, cvPoint(gX, gY),cvSize(120,60), 0, 360, 0,CV_RGB(255,0,0),6,0);
hsv = GetMaskHSV(back,mask,1,1,0,hsv); cvCopy(frame,back,mask_img); stop = clock(); cvShowImage("in",frame); cvShowImage("out", back); cvWriteFrame(rec,back);
key = cvWaitKey(33); if(key == 27) break; }
cvDestroyAllWindows(); cvReleaseCapture(&src); cvReleaseVideoWriter(&rec);
cvReleaseImage(&back); cvReleaseImage(&mask_img); cvReleaseImage(&mask); cvReleaseImage(&img_diff); cvReleaseImage(&img_out2); cvReleaseImage(&img_out3); cvReleaseImage(&img_out4); cvReleaseImage(&img_out5); cvReleaseImage(&img_gray); cvReleaseImage(&img_gray4); cvReleaseMat(&mat_src); cvReleaseMat(&mat_diff); cvReleaseMat(&mat_back); return 0; }
|