Count row in matrix that has repeated numbers

3

I would like to add the number of lines to linhaPreta with only 0, and the linhaBranca with only 1.

public class Pixel {
    public static void main(String[] args) {

        int[][] img = {
        { 1, 1, 1, 1, 1, 1, 1, 1 },
        { 0, 1, 0, 1, 1, 1, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0 },
        { 1, 1, 1, 1, 1, 1, 1, 1 }, 
        { 1, 1, 1, 1, 1, 1, 1, 1 },
        { 1, 1, 1, 1, 1, 1, 1, 1 },
        { 1, 1, 0, 0, 1, 1, 1, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0 },};

        int ppreto = 0;
        int pbranco = 0;
        int linhaPreta = 0;
        int linhaBranca = 0;

        int i;
        int j = 0;

        for (i = 0; i < img.length; i++) {
            for (j = 0; j < img[i].length; j++) {

                if (img[i][j] == 0) {
                    ppreto++;
                }

                if (img[i][j] == 1) {
                    pbranco++;
                }

                if (img[i].length == 0) {
                    linhaPreta++;
                }
                if (img[i].length == 0) {
                    linhaBranca++;
                }
            }
        }

        System.out.print("qtde ponto preto = " + ppreto + "\n");
        System.out.print("qtde ponto branco = " + pbranco + "\n");
        System.out.print("Qtd linha preta = " + linhaPreta + "\n");
        System.out.print("Qtd linha branca= " + linhaBranca + "\n");
    }
}
    
asked by anonymous 11.12.2014 / 18:43

2 answers

6

Simple solution suggestion:

For each "line" of your image, add the pixels. If you add zero (0) the line is "all black". If the sum equals the number of pixels in the line, the line is "all white".

int[][] img = {
    { 1, 1, 1, 1, 1, 1, 1, 1 },
    { 0, 1, 0, 1, 1, 1, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0 },
    { 1, 1, 1, 1, 1, 1, 1, 1 }, 
    { 1, 1, 1, 1, 1, 1, 1, 1 },
    { 1, 1, 1, 1, 1, 1, 1, 1 },
    { 1, 1, 0, 0, 1, 1, 1, 0 }, 
    { 0, 0, 0, 0, 0, 0, 0, 0 },};

int linhaPreta = 0;
int linhaBranca = 0;

for (int i = 0; i < img.length; i++) {

    int soma = 0;
    for (int j = 0; j < img[i].length; j++)
        soma += img[i][j];

    if(soma == 0)
        linhaPreta++;
    else if(soma == img[i].length)
        linhaBranca++;
}
    
11.12.2014 / 19:22
3

The count of the lines is really all wrong. The code used does not make sense. You have to compare item by item to know if a row is all made up of 0 or all by 1.

In this way I have created a counter to accumulate what is 0 or 1 on each line individually. This is why the counter zeroes at the end of each line.

And just at the end of the line I can see if all elements are of the same number. Obviously this is obtained by checking if the items counted in the row is equal to the row size.

public class Main { 
    public static void main(String[] args) {
        int[][] img = {
        { 1, 1, 1, 1, 1, 1, 1, 1 },
        { 0, 1, 0, 1, 1, 1, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0 },
        { 1, 1, 1, 1, 1, 1, 1, 1 }, 
        { 1, 1, 1, 1, 1, 1, 1, 1 },
        { 1, 1, 1, 1, 1, 1, 1, 1 },
        { 1, 1, 0, 0, 1, 1, 1, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0 },};
        int ppreto = 0;
        int pbranco = 0;
        int linhaPreta = 0;
        int linhaBranca = 0;
        for (int i = 0; i < img.length; i++) {
            int itensPreto = 0;
            int itensBranco = 0;
            for (int j = 0; j < img[i].length; j++) {
                 if (img[i][j] == 0) {
                     ppreto++;
                     itensPreto++;
                }
                if (img[i][j] == 1) {
                    pbranco++;
                    itensBranco++;
                }
            }
            if (itensPreto == img[i].length) linhaPreta++;
            if (itensBranco == img[i].length) linhaBranca++;
        }
        System.out.print("qtde ponto preto = " + ppreto + "\n");
        System.out.print("qtde ponto branco = " + pbranco + "\n");
        System.out.print("Qtd linha preta = " + linhaPreta + "\n");
        System.out.print("Qtd linha branca= " + linhaBranca + "\n");
    }
}

See running ideone . And no Coding Ground . Also I placed in GitHub for future reference .

    
11.12.2014 / 19:21