Answer the question
In order to leave comments, you need to log in
Translation of a photo table into a matrix of 0 and 1?
There is a set consisting of a photo table, painted in black and white. We need to convert this photo into a matrix consisting of 0 and 1, where 1 is black and 0 is white. How to implement it? I lack the knowledge and ideas to complete this task
Answer the question
In order to leave comments, you need to log in
There are no algorithms here. Stupidly go through the image in two cycles and fill the corresponding cell with zero or one.
The image has 2x2 squares with one empty pixel between them. And there are still empty pixels along the borders. If you crop 1 pixel from the right and bottom, it turns out that each cell corresponds to a 3x3 square: either completely white, or with 4 black pixels in the lower right corner.
Accordingly, it is enough to check the pixel with coordinates 3*i+1
, 3*j+1
to get the value of the cell [i][j] (all indices start from 0).
Those. in your programming language, you get an image in the form of a matrix of pixels, then fill in all the cells of the matrix in two cycles up to 100 - look at the desired pixel and, if it is not white, set 1.
If the image has compression artifacts, then you can take the average between 4 pixels 3*i+x
, 3*j+y
(x,y=1..2) for all three components (RGB) and see that it is very different from black. To do this, do 2 more nested loops in x and y, add the pixel color value to the variable, then divide by 12 and compare, say, with 200. If less, this is a black square, if more, then white.
цикл по y
цикл по х
матрица[x, y] = цвет пикселя черны? 1 : 0
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question