Page 35 -
P. 35
Chapter 1 ■ Practical Aspects of a Vision System 9
At this point in the loop, count and sum the pixel values so that the mean
can be determined later.
mean += k;
count++;
}
Make a new window and display the grey image in it.
cvNamedWindow( “grey“, CV_WINDOW_AUTOSIZE);
cvShowImage( “grey“, image );
cvWaitKey(0); // wait for a key
Finally, compute the mean level for use as a threshold and pass through the
image again, setting pixels less than the mean to 0 and those greater to 255;
mean = mean/count;
for (i=0; i<image->height; i++)
for (j=0; j<image->width; j++)
{
k=(image->imageData+i*image->widthStep)
[j * image->nChannels + 0];
if (k < mean) k = 0;
else k = 255;
(image->imageData+i*image->widthStep)[j*image->nChannels+0]
= (UCHAR) k;
(image->imageData+i*image->widthStep)[j*image->nChannels+1]
= (UCHAR) k;
(image->imageData+i*image->widthStep)[j*image->nChannels+2]
= (UCHAR) k;
}
One final window is created, and the final thresholded image is displayed
and saved.
cvNamedWindow( “thresh“);
cvShowImage( “thresh“, image );
cvSaveImage( “thresholded.jpg“, image );
Wait for the user to type a key before destroying all the windows and
exiting.
cvWaitKey(0); // wait for a key
cvDestroyWindow(“mainWin“);
cvDestroyWindow(“grey“);
cvDestroyWindow(“thresh“);
}