Exercise #10

How Much Mineral?

Geologists take thin slices of rock to analyze the minerals within the rock.  A sample slice is shown below.

The black, white, greenish, and brownish grains correspond to different minerals.  A useful piece of information for geologists is to determine what percentage of the sample corresponds to each mineral and there is a time-consuming process of manually counting the grains.  Another technique is to use image analysis techniques to count number of the pixels that are close to a particular color.  For example, if we count the pixels that are white then divide by the total number of pixels in the image then we get an estimate for the percentage of the white mineral.

Your job in this lab is write a program that counts the number of "black" pixels in the image.  Consider any pixels whose Red, Green, and Blue component colors are all less than 50 to be black. 

To do this you should write a nested loop where x goes from 0 to the bitmap's width and y goes from 0 to the bitmap's height.  Inside the nested loop, get the color of the pixel at coordinate (x,y). If this pixel's red, green, and blue values are less than 50, then increment a variable that counts the number of black pixels.  At the same time, set the pixel's color at (x,y) to Color.Red so the user can see which pixels the program is actually counting.

Here is a sample of the final program: RockCounter.exe

To help you out, here is a zip file of the project:  RockCounter.zip

Extract the contents of the zip folder to your Visual Studio Projects folder, then double-click on the RockCounter.sln file to open the project.