Click here to Skip to main content
14,825,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm using the code below to find a threshold value of white pixels so that i can distinguish white pixel's from "whiter" ones.Any ideas on how to improve the code above to avoid the error I'm getting?

Color  colorValue=image.GetPixel(x,y);
       var averageValue = ((int)colorValue.R + (int)colorValue.B + (int)colorValue.G)/3; // get the average for black and white
      int  threshold=averageValue * 0.9;
VC.J 3-Oct-14 7:11am
you are multiplying with .9 so you can't convert to int
may be you can try this
int threshold = Convert.ToInt16(averageValue * 0.9);

other way to convert .9 to int and that will result 1 that may effect your output more than the above code
VC.J 3-Oct-14 7:17am
the value of threshhold should be integer or can be any other value?
If it can be like double than use
var threshold = averageValue * 0.9;
Member 11117703 3-Oct-14 7:26am
I "think" i need it as integer because i need to put it's value on a vector and find the max value of it.I used the convertion like @vipinjoshi said and that error is gone but Im getting another convertion error when I use the vector, This f*cking C# -_-

yes you can
dynamic array c#......[^]

List<double> list = new List<double>();

if it work please mark are solution or rate the solution
Member 11117703 3-Oct-14 8:30am
That was it !!! Finally a solution to my problem :D
VC.J 3-Oct-14 12:15pm
if the answer is accepted than Y I am down voted :(
Matt T Heffron 3-Oct-14 22:45pm
I'd guess for posting the same Solution twice.
VC.J 4-Oct-14 5:41am
Ohh Yes sorry about that but one thing that needed to notice here its not a single question btw in this question we had series of question

thanks for pointing out my mistake Matt :)
double averageValue = ((double)colorValue.R + (double)colorValue.B + (double)colorValue.G)/3d; // get the average for black and white
      int  threshold=(int)(averageValue * 0.9d);
Member 11117703 3-Oct-14 7:44am
THNK u, thats nice it worked, One last question:
i passed threshold value to a matrix element like this:
And it says: "Use of unassigned local variable 'thresholdvec' " how is that even possible ?!!!
VC.J 3-Oct-14 8:04am
thresholdvec[x][y] is the array that you have declared am I right ?
and x and y are integer type
Member 11117703 3-Oct-14 8:09am
I've declared it at the beginning of the function like this
double [] thresholdvec;
and x, y are part of the for loops that iterate through every pixel on a photo, So there shouldn't be any kind of error. right ?
Member 11117703 3-Oct-14 8:10am
double [][] thresholdvec; i forgot to type the second brackets, still the error stays...
VC.J 3-Oct-14 8:12am
declare like this

double[] thresholdvec= new double[6];
here 6 is array lengh
for 2 d array as in your case
double[,] names = new double[5, 4];
Member 11117703 3-Oct-14 8:17am
that worked,thank u, but isn't there any way to make it a dynamic vector, because i dont know it's size,and setting up a large number for size takes up memory space and processing speed
yes you can
dynamic array c#......[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900