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 temp_sort(int *a,int n)
- {
- int i,j,temp;
- for(i=0 ; i<n ; i++)
- {
- for(j=0 ; j<n-1; j++)
- {
- if(a[j]>a[j+1]) temp = a[j]; a[j] = a[j+1]; a[j+1] = temp;
- }
- }
- }
- 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};
- //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
- );
- cvNamedWindow( "Image 2", 1 );
- cvShowImage( "Image 2", img2 );
- cvNamedWindow( "Image 1", 1 );
- cvShowImage( "Image 1", img );
- cvWaitKey(0);
- 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] ;
- temp_sort(ary,9);
- ptr[0] = ary[4] ;
- /*
- temp =
- ptr[-1-step] + ptr[0-step] + ptr[1-step] +
- ptr[-1] + ptr[0] + ptr[1] +
- ptr[step-1] + ptr[step+0] + ptr[step+1];
- ptr2[0] = temp/9;
- */
- }
- }
- cvShowImage( "Image 2", img2 );
- cvWaitKey(0);
- cvDestroyWindow( "Image 2" );
- cvDestroyWindow( "Image 1" );
- cvReleaseImage( &img );
- cvReleaseImage( &img2 );
- return 0;
- for (y = 1; y < h-1; y++) // using pointer
- {
- for (x = 1; x < w-1; x++)
- {
- ptr = img->imageData;
- ptr = ptr + y * img->widthStep + x * img->nChannels;
- //ptr2 = img2->imageData + y * img2->widthStep + x * img2->nChannels;
- B = ptr[0]; G = ptr[1]; R = ptr[2];
- Y = 0.299 * R + 0.587*G + 0.114*B;
- ptr[0] = 0;
- ptr[0] = 0;
- ptr[1] = y/10 * 10;
- ptr[2] = 0;
- } // remove Red component
- }
- cvShowImage( "Image 2", img );
- //cvSaveImage( "Gray.bmp" , img );
- cvWaitKey(0); // very important, contains event processing loop inside
- cvDestroyWindow( "Image 2" );
- cvReleaseImage( &img );
- cvReleaseImage( &img2 );
- return 0;
- uchar *ptr_1, *ptr_2;
- for(x=1; x<w/2-1; x++)
- {
- ptr = img->imageData;
- ptr = ptr + 100 * img->widthStep + x * img->nChannels;
- ptr[0] = 0; ptr[1] = 255; ptr[2] = 0;
- }
- cvShowImage( "Image 2", img );
- cvWaitKey(0);
- ptr = img->imageData;
- ptr2 = img2->imageData;
- for (y = 1; y < h-1; y++) // using macro
- {
- ptr_1 = ptr;
- ptr_2 = ptr2;
- for (x = 1; x < w-1; x++)
- {
- //ptr = ptr + y * img->widthStep + x * img->nChannels;
- //ptr2 = img2->imageData + y * img2->widthStep + x * img2->nChannels;
- B = ptr_1[0]; G = ptr_1[1]; R = ptr_1[2];
- //B = *ptr_1++; G = *ptr_1++; R = *ptr_1++;
- Y = 0.299 * R + 0.587 * G + 0.114 * B;
- ptr_2[0] = Y;
- ptr_1 += img->nChannels;
- ptr_2 += img2->nChannels;
- } // remove Red component
- ptr += img->widthStep ;
- ptr2 += img2->widthStep;
- }
- cvNamedWindow( "Image 1", 1 );
- cvShowImage( "Image 1", img2 );
- //cvSaveImage( "Gray.bmp" , img );
- cvWaitKey(0); // very important, contains event processing loop inside
- cvDestroyWindow( "Image 2" );
- 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