Capstone: build an object counter
Combine thresholding, connected components and filtering into a pipeline that counts and boxes objects.
- Chain image operations into a complete pipeline
- Filter out noise with a minimum object size
- Report a count and a bounding box for every object
A biology lab needs to count cells in microscope images - hundreds of images a day, too many to count by hand. You can build a working counter from the pieces you already know, no neural network required: threshold the image into a mask, find connected components, drop tiny specks of noise, and report each object’s bounding box.
Try it
Step 1: threshold the cells
Find a threshold that separates the bright cells from the dark background. This image is evenly lit, so a single threshold works - check what Otsu picks. Notice the two lonely bright pixels: they will become “objects” too unless you filter them out.
Histogram: how many pixels have each brightness
Try it
Step 2: count the components
Here is a mask like the one you just made. How many components are there before filtering out the single-pixel specks? And after?
How many separate objects are there?
1def count_objects(image, threshold, minimum_size):
2 mask = [[1 if value >= threshold else 0 for value in row] for row in image]
3 components = find_components(mask)
4 return [component for component in components if component["size"] >= minimum_size]That is the shape of the whole program: three lines of logic, each a lesson you have finished. The exercise asks you to write find_components too (your flood fill from the connected-components lesson) and print the results.
Key takeaways
A complete vision pipeline can be a chain of simple steps: threshold → components → filter → report.
A minimum size removes noise; the threshold and connectivity choices change the count.
Classical pipelines are fast and explainable; learned models handle messier images.
Lesson quiz
5 questions · pass with 4 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: apply computer vision with Python
Use small pixel arrays to explore vision concepts, run your code against sample images, and connect each result to the larger computer vision idea.
Count and box the objects
Read a JSON grayscale image, then a threshold, then a minimum size, each on its own line.
Make a mask (pixel ≥ threshold), find its 4-connected components, and keep those with at least the minimum size. Print the number of kept objects, then one line per kept object, in the order you first meet it scanning row by row: top left bottom right.
- Two cells and a speck
- Keep the speck
- Nothing bright enough
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…