Click here to Skip to main content
14,106,748 members
Rate this:
Please Sign up or sign in to vote.
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# -_-
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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 :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190518.1 | Last Updated 3 Oct 2014
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100