Question: make a c# programm for windonsFormApp ( visual code studio ) on the form that binarizes so that, 1 ) the image displayed as a

make a c# programm for windonsFormApp(visual code studio) on the form that binarizes so that, 1) the image displayed as a preview has about the same resolution as the picturebox it is displayed in (downsampling to speed up the preview with the different thresholds)2) there is a button for threshold selection with Otsu's algorithm. For otsu i have this private static float Px(int init, int end, int[] hist)
{
int sum =0;
int i;
for (i = init; i <= end; i++)
sum += hist[i];
return (float)sum;
}
private static float Mx(int init, int end, int[] hist)
{
int sum =0;
int i;
for (i = init; i <= end; i++)
sum += i * hist[i];
return (float)sum;
}
private static int FindMax(float[] vec, int n)
{
float maxVec =0;
int idx =0;
int i;
for (i =1; i < n -1; i++)
{
if (vec[i]> maxVec)
{
maxVec = vec[i];
idx = i;
}
}
return idx;
}
unsafe private static void GetHistogram(byte* p, int w, int h, int ws, int[] hist)
{
hist.Initialize();
for (int i =0; i < h; i++)
{
for (int j =0; j < w *3; j +=3)
{
int index = i * ws + j;
hist[p[index]]++;
}
}
}
public static int GetOtsuThreshold(Bitmap bmp)
{
byte t =0;
float[] vet = new float[256];
int[] hist = new int[256];
vet.Initialize();
float p1, p2, p12;
int k;
BitmapData bmData = bmp.LockBits(new Rectangle(0,0, bmp.Width, bmp.Height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
unsafe
{
byte* p =(byte*)(void*)bmData.Scan0.ToPointer();
GetHistogram(p, bmp.Width, bmp.Height, bmData.Stride, hist);
for (k =1; k !=255; k++)
{
p1= Px(0, k, hist);
p2= Px(k +1,255, hist);
p12= p1* p2;
if (p12==0)
p12=1;
float diff =(Mx(0, k, hist)* p2)-(Mx(k +1,255, hist)* p1);
vet[k]=(float)diff * diff / p12;
}
}
bmp.UnlockBits(bmData);
t =(byte)FindMax(vet,256);
return t;
}(i think it needs form1,form2 and class1 thank you)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!