Image Processing
InterviewTime limit1sMemory limit512 MB
Read an N by M grid of RGB triples, binarize each pixel by whether its average meets threshold T, then count the connected components of 255 pixels under 4-directional adjacency.
- Level
Medium4 of 10
- Topics
- BFS, Graph, Matrix, Implementation
- Solved
- No attempts yet
Problem
You are given a simple but tedious image processing assignment. Its specification is as follows.
A screen with height and width consists of pixels, and the pixel at holds the values of three colors: (Red), (Green), and (Blue). Each color is represented by an integer between 0 and 255.
For every pixel, take the average of the three colors. If it is greater than or equal to the threshold , set the pixel value to 255; otherwise set it to 0. Store the results as a new screen.
In the new screen, a pixel with value 255 is recognized as an object. If pixels with value 255 are adjacent vertically or horizontally, they are recognized as the same object.
Write a program that determines how many objects are on the screen.
Input
The first line gives the height and width of the screen, separated by a space.
From the second line to the -th line, the values , , of the pixels making up the -th row are given, separated by spaces, for a total of pixels.
The last line gives the threshold .
Output
Print the number of objects on the screen. If there are no objects, print 0.
Constraints
- , and are integers
- , and is an integer