Beispiel #1
0
        public void getOrientations(classimage sourceimage, classimage edgesimage, int radius, int no_of_angles, int tx, int ty, int bx, int by, int[,,] radius_lookup, int trim, ref int average_confidence, classimage secondaryimage)
        {
            int x,y,xx,yy,xx2,yy2,r,prev_ang,c,ang,min,max,max_ang,min_ang,value,ang_diff,min_diff,half_angles,min_radius;
            int[] angle_values = new int[30];
            int centre_value,max_r,min_r,diff,max_diff;
            int positive_gradient,negative_gradient,local_radius,next_ang;
            int hits,av,confidence,confidence_thresh;
            int variance,max_variance,hits2;

            clearArea(tx,ty,bx-1,by-1);
            secondaryimage.clearArea(tx,ty,bx-1,by-1);

            hits=0;
            av=0;
            r = 0;
            //confidence_thresh = average_confidence*40/100;
            confidence_thresh = average_confidence*10/100;
            local_radius=1;
            min_radius=radius*8/10;
            min_diff = 0; //no_of_angles/6;
            half_angles = no_of_angles/2;
            if (min_diff<1) min_diff=1;
            for (y=ty;y<by;y++)
            {
                max_variance=0;
                for (x=tx;x<bx;x++)
                {
	                if (edgesimage.image[x,y,0]>0)
	                {
	                    max_ang=0;
		                min_ang=0;
		                max_r=0;
		                min_r=0;
		                max=-99999;
		                min=99999;

		                //get the value at this location
		                hits2=0;
		                centre_value=0;
		                for (xx2=x-local_radius;xx2<=x+local_radius;xx2++)
		                    for (yy2=y-local_radius;yy2<=y+local_radius;yy2++)
		                        if ((xx2>-1) && (xx2<width) && (yy2>-1) && (yy2<height))
	                                for (c=0;c<3;c++)
			                        {
			                            centre_value += sourceimage.image[xx2,yy2,c];
				                        hits2++;
			                        }

		                //get the values at the periphery for each rotation
	                    for (ang=0;ang<no_of_angles;ang++)
	                    {
		                    hits2=0;
	                        value=0;
		                    r = radius-1;
	                        xx = x+radius_lookup[ang,r,0];
		                    yy = y+radius_lookup[ang,r,1];
		                    for (xx2=xx-local_radius;xx2<xx+local_radius;xx2++)
		                        for (yy2=yy-local_radius;yy2<yy+local_radius;yy2++)
		                            if ((xx2>-1) && (xx2<width) && (yy2>-1) && (yy2<height))
			                        {
		                                value += sourceimage.image[xx2,yy2,0];
				                        hits2++;
			                        }
		  
		                    angle_values[ang] = value; // /hits2;
	                    }

		                //get minimum and maximum values
		                max_diff=0;
		                min_diff=0;
		                negative_gradient=0;
                        positive_gradient=0;
		                for (ang=0;ang<no_of_angles;ang++)
	                    {
		                    prev_ang = ang-1;
		                    if (prev_ang<0) prev_ang = no_of_angles-1;
		                    next_ang = ang+1;
		                    if (next_ang>=no_of_angles) next_ang=0;
		                    value = angle_values[ang] + angle_values[prev_ang] + angle_values[next_ang];
		                    diff = value - centre_value;

		                    if (diff>max_diff) max_diff = diff;
		                    if (diff<min_diff) min_diff = diff;
		                    if (value>max)
		                    {
		                        max=value;
		                        max_ang = ang;
			                    max_r = r;
			                    if (diff>0) 
			                        positive_gradient=diff;
			                        else
                                    negative_gradient=-diff;
	                        }
		                    if (value<min)
		                    {
 		                        min=value;
                                min_ang = ang;
		                        min_r = min;
	                        }
		                }

		                variance = (max-min)/6;
		                if (variance>max_variance) max_variance=variance;

		                confidence=max-min;
		                av += confidence;
		                hits++;

		                if (confidence>confidence_thresh)
		                {
		                    ang_diff = max_ang - min_ang;
		                    if (ang_diff<0) ang_diff = -ang_diff;
		                    if (ang_diff>half_angles) ang_diff = no_of_angles - ang_diff;
		                    if (max_diff<-min_diff) max_diff=-min_diff;
		                    if (-min_diff<max_diff) min_diff=-max_diff;
		                    image[x,y,0] = 127;
		                    if (positive_gradient>negative_gradient)
		                    {
		                        if (max_diff>0) image[x,y,0] = (Byte)(127+((positive_gradient*127)/max_diff));		  
		                    }
		                    else
		                    {
		                        if (min_diff<0) image[x,y,0] = (Byte)(127-((negative_gradient*127)/(-min_diff)));
		                    }
  		                    image[x,y,1] = (Byte)((ang_diff*255) / half_angles);
		                    image[x,y,2] = (Byte)variance;

		                    secondaryimage.image[x,y,0] = (Byte)((max_ang*255) / no_of_angles);
		                    secondaryimage.image[x,y,1] = (Byte)((min_ang*255) / no_of_angles);
		                    //if (min_ang>half_angles)
		                    //  secondaryimage.image[x,y,1] = 255;
		                    //  else
		                    //  secondaryimage.image[x,y,1] = 0;
		                    if (max_ang>half_angles)
		                        secondaryimage.image[x,y,2] = 100;
		                        else
		                        secondaryimage.image[x,y,2] = 0;
		                }
		                else edgesimage.image[x,y,0]=0;
	                }
	            }  

	            //normalise variance
                if (max_variance>0)
	            {
                    for (x=tx;x<bx;x++)
                    {
	                    if (edgesimage.image[x,y,0]>0)
	                    {
		                    image[x,y,2] = (Byte)(((int)(image[x,y,2])*255)/max_variance);
		                }
	                }
	            }
            }

            if (hits>0)
                average_confidence = (average_confidence + (av/hits))/2;
        }
Beispiel #2
0
        //------------------------------------------------------------------------------------------------------------------------
        //detects the centre points of image
        //------------------------------------------------------------------------------------------------------------------------
        public int detectBlobCentres(classimage sourceimage, classimage tempimage, int min_volume, ref int average_volume, ref int average_radius, int[,] blob_data, int no_of_angles, classimage colour_image, int[,,] radius_lookup)
        {
            int x,y,no_of_blobs;
            int tx,ty,bx,by,cx,cy;
            long pixels,av_r,av_g,av_b,av_x,av_y,av_vol,av_rad;

            clear();
            tempimage.clear();
            no_of_blobs=0;
            av_vol=0;
            av_rad=0;
            for (x=1;x<width-1;x++)
            {
                for (y=1;y<height-1;y++)
                {
	                if (sourceimage.image[x,y,0]>0)
	                {
		                pixels=0;
	                    av_x=0; av_y=0;
		                av_r=0; av_g=0; av_b=0;
		                tx=x; ty=y; bx=x; by=y;
	                    floodFill(x,y,0,100,100,100,ref tx,ref ty,ref bx,ref by,ref pixels,ref av_r,ref av_g,ref av_b,sourceimage,ref av_x,ref av_y,false,0,tempimage,colour_image);
		                if (pixels>min_volume)
		                {
		                    cx = (int)(av_x / pixels);
		                    cy = (int)(av_y / pixels);
		                    image[cx,cy,0]=255;
		                    image[cx,cy,1]=255;
		                    image[cx,cy,2]=255;
		                    av_vol += pixels;
		                    av_rad += (((bx-tx) + (by-ty))/2);

                            //update blob data
		                    if (no_of_blobs<2000)
		                    {
		                        blob_data[no_of_blobs,0] = (int)pixels;
		                        blob_data[no_of_blobs,1] = cx;
		                        blob_data[no_of_blobs,2] = cy;
		                        blob_data[no_of_blobs,3] = bx-tx;
		                        blob_data[no_of_blobs,4] = by-ty;
			                    blob_data[no_of_blobs,5] = (int)(av_r / pixels);
			                    blob_data[no_of_blobs,6] = (int)(av_g / pixels);
			                    blob_data[no_of_blobs,7] = (int)(av_b / pixels);
                                if (no_of_angles>1) tempimage.getBlobShape(no_of_angles,cx,cy,tx,ty,bx,by,blob_data,no_of_blobs,8,radius_lookup);
		                    }

		                    no_of_blobs++;
		                }
		                tempimage.clearArea(tx,ty,bx,by);
	                }
	            }
            }

            if (no_of_blobs>0)
            {
                average_volume = (int)(av_vol / no_of_blobs);
	            average_radius = (int)(av_rad / no_of_blobs);
            }
            return(no_of_blobs);
        }