首先,不知道這篇文章的標題合不合適,呵呵,大家有沒有更合適的標題呢。
這篇博文是在前兩篇的基礎上寫的,大家可以參看一下。下面貼代碼了。
相關鏈接:
http://www.linuxidc.com/Linux/2012-09/70010.htm
http://www.linuxidc.com/Linux/2012-09/70011.htm
- #include <cv.h>
- #include <highgui.h>
- #include <stdio.h>
- #pragma comment( lib, "cv.lib" )
- #pragma comment( lib, "cxcore.lib" )
- #pragma comment( lib, "highgui.lib" )
- IplImage* org = 0;
- IplImage* img = 0;
- IplImage* tmp = 0;
- IplImage* msk = 0;
- IplImage* dst = 0 ;
- void on_mouse( int event, int x, int y, int flags, void* ustc)
- {
- static CvPoint pre_pt = {-1,-1};
- static CvPoint cur_pt = {-1,-1};
- CvFont font;
- cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);
- char temp[16];
-
- if( event == CV_EVENT_LBUTTONDOWN )
- {
- cvCopy(org,img);
- sprintf(temp,"(%d,%d)",x,y);
- pre_pt = cvPoint(x,y);
- cvPutText(img,temp, pre_pt, &font, cvScalar(0,0, 0, 255));
- cvCircle( img, pre_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
- cvShowImage( "image", img );
-
- cvCopy(img,tmp);
- }
- else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))
- {
- cvCopy(tmp,img);
- sprintf(temp,"(%d,%d)",x,y);
- cur_pt = cvPoint(x,y);
- cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
- cvLine(img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, CV_AA, 0 );
- cvShowImage( "image", img );
- }
- else if( event == CV_EVENT_LBUTTONUP )
- {
- sprintf(temp,"(%d,%d)",x,y);
- cur_pt = cvPoint(x,y);
- cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
- cvCircle( img, cur_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
- cvLine( img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, CV_AA, 0 );
- cvSetZero(msk);
- cvLine( msk, pre_pt, cur_pt, cvScalar(255,255,255,0), 1, CV_AA, 0 );
- cvShowImage( "image", img );
- int width=org->width;
- int height=org->height;
- int step=org->widthStep;
- uchar *data=(uchar*)org->imageData;
- int mask_step=msk->widthStep;
- uchar *mask_data=(uchar*)msk->imageData;
- cvSet(dst,cvScalar(255,255,255));
- int length=cvCeil(sqrt((pre_pt.x-cur_pt.x)*(pre_pt.x-cur_pt.x)+(pre_pt.y-cur_pt.y)*(pre_pt.y-cur_pt.y)));
- double bin_width=(double)dst->width/length;
- double bin_height=(double)dst->height/255;
- int count=0;
- for(int i=0;i<width;i++)
- {
- for(int j=0;j<height;j++)
- {
- if(mask_data[j*mask_step+i]!=0)
- {
- CvPoint pt;
- pt=cvPoint(bin_width*count,(dst->height-data[j*step+i*3+2])*bin_height);
- cvCircle(dst, pt,1,cvScalar(0,0,255,0) ,CV_FILLED, CV_AA, 0 );
- pt=cvPoint(bin_width*count,(dst->height-data[j*step+i*3+1])*bin_height);
- cvCircle(dst, pt,1,cvScalar(0,255,0,0) ,CV_FILLED, CV_AA, 0 );
- pt=cvPoint(bin_width*count,(dst->height-data[j*step+i*3])*bin_height);
- cvCircle(dst, pt,1,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
- count++;
- }
- }
- }
-
- cvNamedWindow("dst",1);
- cvShowImage("dst",dst);
- }
- }
- int main()
- {
- org=cvLoadImage("lena.jpg",1);
- img=cvCloneImage(org);
- tmp=cvCloneImage(org);
- msk=cvCreateImage(cvSize(img->width,img->height),8,1);
- cvSetZero(msk);
- dst=cvCreateImage(cvSize(400,300),8,3);
- cvSet(dst,cvScalar(255,255,255));
- cvNamedWindow("image",1);
- cvSetMouseCallback( "image", on_mouse, 0 );
- cvShowImage("image",img);
- cvWaitKey(0);
- cvDestroyAllWindows();
- cvReleaseImage(&org);
- cvReleaseImage(&img);
- cvReleaseImage(&tmp);
- cvReleaseImage(&msk);
- cvReleaseImage(&dst);
- return 0;
- }
效果圖如下,上圖是繪線窗口,下圖是線上的RGB強度分布圖。
這裡離散的顯示分布圖,大家有沒有方法使連續起來呢?