Count line in the matrix that have repeated numbers

Asked

Viewed 846 times

3

I wanted to add to linhaPreta the number of lines with only 0, and the linhaBranca only with 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");
    }
}
  • 1

    You could use crase, help understand better... Give a read on the question, there is no way to understand if it is to be added or added to this...

2 answers

6

Suggestion of a simple solution:

For each "line" of your image, add the pixels. If the sum is zero (0) the line is "all black". If the sum equals the number of pixels on 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++;
}

3

The line count is really all wrong. It doesn’t make sense the code used. You have to compare item by item to know if a row is all made up of 0 or all of 1.

This way I created a counter to accumulate what is 0 or 1 on each line individually. That’s why the Zera counter at the end of each line.

And only at the end of the line can I see if all the elements are of the same number. Obviously this is obtained by checking whether the items counted on the line is equal to the line 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");
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.