[OpencvForUnity]
I created Mat for webcamTexture and I draw contour on a particular area that was determined by me. I want to access all coordinate on contour. Because I want not only to change mat color but also add some color.
I reached x_min,x_max,x_min,x_max point on contour. When I put them for loop and I changed color I didn’t get a good result.
Is there any other method that I can use?
I create get coordinates function for access all points of contour.
ListMaxAndMins means that max and min points (x,y)
x_min,x_max,x_min,x_max points of contour.
Mat image means that your clone of contour.
You can create your clone contour. cloneContour = Yourexistingcontour.clone()
You can access all points of your contour values using the following function.
public List<Point> getCoordinates(List<int> _maxAndMins, Mat image)
{
int x_max = _maxAndMins[0];
int x_min = _maxAndMins[1];
int y_max = _maxAndMins[2];
int y_min = _maxAndMins[3];
List<Point> cordinates = new List<Point>();
for (int i = x_min; i <= x_max; i++)
{
for (int j = y_min; j <= y_max; j++)
{
//Debug.Log("i:" + i + " j:" + j);
if (image.get(j, i)[0] == 0 & image.get(j, i)[1] == 1)
{
cordinates.Add(new Point(i, j));
}
}
}
return cordinates;
}
Also, you can get the color (R-G-B-A) values that are your accessed points using the following function.
public List<double[]> getColor(List<Point> cordinates, Mat image)
{
int r = 0, g = 0, b = 0, a = 0;
List<double[]> colorlist = new List<double[]>();
List<double[]> colorAvg = new List<double[]>();
for (int i = 0; i < cordinates.Count; i++)
{
int x = (int)cordinates[i].x;
int y = (int)cordinates[i].y;
double[] color = image.get(x, y);
colorlist.Add(color);
r = (r + (int)colorlist[i][0]);
g = (g + (int)colorlist[i][1]);
b = (b + (int)colorlist[i][2]);
a = (a + (int)colorlist[i][3]);
//Debug.Log("Color: "+ color.GetValue(0)+ "-"+ color.GetValue(1) + "-" + color.GetValue(2) + "-" + color.GetValue(3));
}
r = r / colorlist.Count;
g = g / colorlist.Count;
b = b / colorlist.Count;
a = a / colorlist.Count;
colorAvg.Add(new double[4] { r, g, b, a } );
// Average R-G-B-A values
Debug.Log("Avg R:" + colorAvg [0][0]);
Debug.Log("Avg G:" + colorAvg [0][1]);
Debug.Log("Avg B:" + colorAvg [0][2]);
Debug.Log("Avg A:" + colorAvg [0][3]);
return colorAvg ;
}