A
A
artur_agishev2021-03-04 20:11:15
Algorithms
artur_agishev, 2021-03-04 20:11:15

Translation of a photo table into a matrix of 0 and 1?

604114325f6c6120761636.jpeg
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

3 answer(s)
W
Wataru, 2021-03-04
@artur_agishev

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+1to 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.

A
Anton Shamanov, 2021-03-04
@SilenceOfWinter

цикл по y
  цикл по х
    матрица[x, y] = цвет пикселя черны? 1 : 0

U
U235U235, 2021-03-05
@U235U235

Use imagemagick and the PBM format.

convert 604114325f6c6120761636.jpeg -sample 100x100 -compress none out.pbm
tail -n 100 out.pbm > out.txt

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question