Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "cv.h"
- #include "highgui.h"
- void merge_array(int c[], int a[], int aN, int b[], int bN)
- {
- int ai, bi, ci;
- ai = bi = ci = 0;
- while ( (ai<aN) && (bi<bN) )
- {
- if(a[ai]>b[bi])
- {
- c[ci] = a[ai];
- ai++; ci++;
- }
- else
- {
- c[ci] = b[bi];
- bi++; ci++;
- }
- }
- if(bi==bN)
- {
- for( ; ai<aN; ai++)
- c[ci++] = a[ai];
- }
- else
- {
- for( ; bi<bN; bi++)
- c[ci++] = b[bi];
- }
- }
- void merge_sort(int a[], int n)
- {
- if(n<=1) return; // if the array has been divided into one element, then just return.
- int *a0, *b0; // else we have to divide the array into two half-size arrays.
- int aN, bN; int i;
- aN = n/2; bN = n - aN; // Note: aN is n/2, but bN can not be n/2! Why?
- a0 = a; b0 = a+aN; // a0 is the first half array, and b0 is the bottom half array.
- // a0 = &a[0]; b0 = &a[aN];
- merge_sort(a0, aN); // sort the upper half array elements.
- merge_sort(b0, bN); // sort the lower half array elements.
- // Then we merge a0 and b0 to array c, and copy back from c to a.
- int c[n]; //int *c; //c = malloc(n *sizeof(int));
- merge_array(c, a0, aN, b0, bN);
- for(i=0; i<n; i++) a[i] = c[i]; // copy back to array a;
- // memcpy(a, c, n*sizeof(int)); // You can use memcpy()
- //free(c); // if you use malloc();
- }
- int sort(int *ary,int n)
- {
- int i,j,temp=0;
- for(i=0;i<n-1;i++)
- for(j=i;j<n;j++)
- if(ary[i]>ary[j])
- {temp=ary[i];ary[i]=ary[j];ary[j]=temp;}
- return ary[4];
- }
- int main(int argc, char* argv[])
- {
- int w, h, nc; int step;
- int x, y, i;
- IplImage* img; IplImage* img2;
- int Y, R, G, B;
- int *ary[9]={0};
- srand(time(NULL));
- //if( argc == 2 && (img = cvLoadImage(argv[1], -1)) != 0 )
- if( (img = cvLoadImage("step_up_rev.jpg", CV_LOAD_IMAGE_GRAYSCALE )) != 0 ) //
- {
- printf("Channels %d depth %d widthStep %d \n",
- img->nChannels, img->depth, img->widthStep);
- printf("The image Width by Height is %d * %d \n", img->width,img->height);
- printf("The image origin is %d\n", img->origin);
- step = img->widthStep;
- w = img->width;
- h = img->height;
- nc = img->nChannels;
- img2 = cvCreateImage(
- cvSize(w, h), //CvSize size,
- IPL_DEPTH_8U , //int depth,
- nc ); //int channels
- CvPoint P;
- for(i = 0 ; i < 200 ; i++)
- {
- P = cvPoint(rand()%w,rand()/h);
- cvLine(
- img, //CvArr* img,
- P, //CvPoint pt1,
- P, //CvPoint pt2,
- cvScalar(0, 0, 0, 0), //CvScalar color,
- 1, 8, 0); //int thickness=1, int line_type=8, int shift=0 );
- cvWaitKey(1);
- }
- printf("sss");
- cvNamedWindow( "Image 2", 1 );
- cvShowImage( "Image 2", img2 );
- cvNamedWindow( "Image 1", 1 );
- cvShowImage( "Image 1", img );
- cvWaitKey(0);
- /*
- P1 = cvPoint(100,100); // 設定起點座標
- P2 = cvPoint(400,400); // 設定終點座標
- Color=CV_RGB(0,0,255); // 設定顏色 -- 藍色
- cvLine(Image1,P1,P2,Color,10,CV_AA,0); // 畫線
- Color=CV_RGB(255,0,0); // 設定顏色 -- 紅色
- cvLine(Image1,P1,P2,Color,1,CV_AA,0); // 畫線
- cvLine(
- img, //CvArr* img,
- cvPoint(100, 100), //CvPoint pt1,
- cvPoint(400, 500), //CvPoint pt2,
- cvScalar(0, 255, 255, 0), //CvScalar color,
- 4, 8, 0); //int thickness=1, int line_type=8, int shift=0 );
- void cvLine(IplImage img,
- CvPoint pt1,
- CvPoint pt2,
- CvScalar color,
- int thickness,
- LINE_TYPE lineType,
- int shift)
- */
- uchar* ptr;
- uchar* ptr2;
- int temp;
- for (y = 1; y < h-1; y++) // using macro
- {
- for (x = 1; x < w-1; x++)
- {
- ptr = &CV_IMAGE_ELEM(img, uchar, y, x * img->nChannels);
- ptr2 = &CV_IMAGE_ELEM(img2, uchar, y, x * img2->nChannels);
- //CV_IMAGE_ELEM( image, elemtype, row, col )
- ary[0] = ptr[-1-step] ;
- ary[1] = ptr[0-step] ;
- ary[2] = ptr[1-step] ;
- ary[3] = ptr[-1] ;
- ary[4] = ptr[0] ;
- ary[5] = ptr[1] ;
- ary[6] = ptr[step-1] ;
- ary[7] = ptr[step+0] ;
- ary[8] = ptr[step+1] ;
- ptr2[0]=sort(ary,9);
- }
- }
- cvShowImage( "Image 2", img2 );
- cvWaitKey(0);
- cvDestroyWindow( "Image 2" );
- cvDestroyWindow( "Image 1" );
- cvReleaseImage( &img );
- cvReleaseImage( &img2 );
- return 0;
- }
- printf("Can't opne image file\n");system("PAUSE"); return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment