Custom Algorithms
continuation of bitmaps
this is the continuation of bitmaps Now we want to count the pixels and divide them in 3 areas and find which ones are orange...
public static int getconepos(List<int[]> rgbValues) {
String[] possiblePos = {"left", "middle", "right"};
List<int[]> rgb = rgbValues;
int size = rgb.size();
int section = size/3;
List<Integer> sum = new ArrayList<Integer>(3);
sum.add(0);
sum.add(0);
sum.add(0);
int indx = 0;
while(indx < size)
{
int[] pixel = rgb.get(indx);
// System.out.println(pixel[0] + " " + pixel[1] + " " + pixel[2]);
if(pixel[0] > 145 && pixel[0] < 254 && pixel[1] > 60 && pixel[1] < 127 && pixel[2] >= 0 && pixel[2] <= 20)
{
//adds one to the certain section number
int exSum = sum.get(indx/section);
exSum++;
sum.set(indx/section, exSum);
}
indx++;
}
// System.out.println(sum.get(0) + " " + sum.get(1) + " " + sum.get(2));
int maxIndx = sum.indexOf(Collections.max(sum));
if(sum.get(0) == 0 && sum.get(1) == 0 && sum.get(2) == 0)
{
System.out.println("Don't fool me! There's no cone there!");
return -1;
}
else
{
System.out.println("The cone is in the " + possiblePos[maxIndx] + " position");
}
return maxIndx;
}
}
String[] possiblePos = {"left", "middle", "right"};
: This array holds the possible positions of the cone, assuming there are three positions: left, middle, and right.List<int[]> rgb = rgbValues;
: This line creates a new List (rgb
) and initializes it with the providedrgbValues
list, which contains RGB values of pixels from an image.int size = rgb.size();
: It calculates the total number of RGB values (pixels) in thergb
list.int section = size/3;
: This variablesection
determines the number of pixels that belong to each possible position (left, middle, right). Thesize
ofrgb
is divided by 3 because we are assuming three possible positions.List<Integer> sum = new ArrayList<Integer>(3);
: This creates a new List (sum
) to store the count of pixels that belong to each possible position (left, middle, right).sum.add(0); sum.add(0); sum.add(0);
: It initializes thesum
list with three elements, all set to 0. Each element represents the count of pixels for the corresponding position.int indx = 0;
: This variableindx
is used to iterate through thergb
list.The
while
loop iterates through all the RGB values in thergb
list.int[] pixel = rgb.get(indx);
: This line retrieves the RGB values of a pixel from thergb
list.The
if
condition checks if the RGB values of the pixel fall within specific color ranges, suggesting the presence of a cone. If the conditions are met, it increments the corresponding section's pixel count in thesum
list.After the loop,
maxIndx
is calculated, which represents the index of the position (left, middle, right) with the highest pixel count in thesum
list.If all pixel counts in the
sum
list are 0, it indicates that there is no cone detected, and the method returns -1. Otherwise, it prints the detected cone's position and returns the index of the cone's position (maxIndx
).
In summary, this method processes RGB values of pixels from an image and determines the position of a cone based on certain color ranges in the RGB values. The position with the highest number of pixels falling within the specified color ranges is considered the cone's position. If no cone is detected, it returns -1.
Last updated