get few pixels value of grayscale image with android API

asked 2015-11-26 06:09:26 -0500

valexor89 gravatar image

Hi men, first of all sorry for my bad englisjh. I need to use a few pixels value of a grayscale image, i've searched everywhere on the web and have understood how read the pixel value but cannot resolve my problem.

In short i have a grayscale image 8UC1, under the significant code:

public boolean verify(Mat pupil, Point center, int r, int i)
{//verifica se i cerchi da disegnare coincidono esattamente con gli occhi
 //ovvero se non si discotano di tanto o anche di poco dall'iride viene escluso
    boolean flag=false;

    int x,y; 
    x = (int) (center.x); 
    y = (int) (center.y);
    int radiusx[] = {0,0};
    int radiusy[] = {0,0};
    radiusx[0] =  (x+r);
    radiusy[0] =  (y+r);
    radiusx[1] =  (x-r);
    radiusy[1] =  (y-r);
    int ep=2;

    double[] data1u,data2u,data3u,data4u; //pixel superiori
    double[] data1d,data2d,data3d,data4d; //pixel inferiori
    double diff1,diff2,diff3,diff4; //differenze tra i pixel
    //primo pixel superiore ed inferiore (orientamento 90° all'asse y1 del cerchio)
    //per alcune ragioni data1u ecc...sono nulli
    data1u=(pupil.get((radiusx[1]-ep),y));
    data1d=(pupil.get((radiusx[1]+ep),y));
    //ora usando (int)data1u[0] ottengo il valore dell'intensità
    diff1=(data1u[0]-data1d[0]); //differenza tra intensittà???[1°] e [2°] cordinate pixel
    //secondo pixel superiore ed inferiore (orientamento 180° all'asse y1 cerchio)
    data2u=(pupil.get(x,(radiusy[0]+ep)));
    data2d=(pupil.get(x,(radiusy[0]-ep)));
    diff2=(data2u[0]-data2d[0]); 
    //terzo pixel superiore ed inferiore (orientamento -90° all'asse y1 del cerchio)
    //essendo verso opposto, cambia il verso dell'asse positivo relativo al cerchio
    data3u=(pupil.get((radiusx[0]+ep),y));  
    data3d=(pupil.get((radiusx[0]-ep),y));
    diff3=(data3u[0]-data3d[0]);
    //quarto pixel superiore ed inferiore (orientamento 0° all'asse y1 del cerchio)
    data4u=(pupil.get((radiusy[1]-ep),y));  
    data4d=(pupil.get((radiusy[1]+ep),y));
    diff4=(data4u[0]-data4d[0]);
    //controllo: se le differenze sono valori bassi (idealmente zero) è un occhio rilevato male
    double soglia=10;
    if (diff1>soglia && diff2>soglia && diff3>soglia && diff4>soglia) //soglia un certo valore>0 non basso
        flag=true;
    return flag;
};

Sorry for the cofuded code, the important is that pupil is an image not empty in grayscale and 8UC1, i'm sure, and the vectors data1u, etc... are null (viewed with debug) on diff1=(data1u[0]-data1d[0]);

Why is that? Hope in some advices. Thanks you all.

edit retag flag offensive close merge delete

Comments

Sorry men, i've found the solution at this question, the problem is that when you use the method get on Mat need to switch the value x and y of cordinates, otherwise could go out of range and return null value.

So must to use as follow: Mat m = something double[] buff = m.get(Y,X); //in this form if want read only few values double value=buff[0]; //contain the pixel intensity for grayscale image //or intensity of one channel according to the model RGB or BGR

However thanks alot all of you.

valexor89 gravatar imagevalexor89 ( 2015-11-26 08:15:01 -0500 )edit