void salt(IplImage *img, int saltNum) { int x,y; int i ; unsigned char *src = NULL; src = (unsigned char*)img->imageData; for(i = 0; i < saltNum; i++) { x = rand() % img->width; y = rand() % img->height; if(img->nChannels == 1) { src[y * img->widthStep + x] = 255; } else if(img->nChannels == 3) { src[y * img->widthStep + x * 3] = 255; src[y * img->widthStep + x * 3 + 1] = 255; src[y * img->widthStep + x * 3 + 2] = 255; } } }
IplImage* img = cvLoadImage("E:\\bgtest.bmp", CV_LOAD_IMAGE_GRAYSCALE); IplImage* img1 = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); img1 = cvCloneImage(img); salt(img1,38000); cvSaveImage("E:\\saltfull38000.bmp",img1);