I am trying to write a code for contrast but contrast depends on threshold value.What I mean is using entered threshold value or if it is not entered by user I want to write a default value for threshold.As far as I know I should do this by using constructor but I am not good at object oriented programming.How can I do that?
Here is the code for Form1.cs:
using AForge.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace imageprocessing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public FilterInfoCollection devices;
public VideoCaptureDevice camera;
private void Form1_Load(object sender, EventArgs e)
{
devices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo item in devices)
{
cb_show.Items.Add(item.Name);
}
camera = new VideoCaptureDevice();
cb_show.SelectedIndexChanged = cb_show_SelectedIndexChanged;
}
private void cb_show_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (camera.IsRunning == false)
{
camera = new VideoCaptureDevice(devices[cb_show.SelectedIndex].MonikerString);
camera.NewFrame = Camera_NewFrame;
camera.Start();
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message "");
}
}
List<byte> alldata = new List<byte>();
public void Camera_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
Bitmap image = (Bitmap)eventArgs.Frame.Clone();
byte[] maindata = new byte[image.Width * 3];
if (btn_grayWasClicked == true)
{
alldata.Clear();
for (int i = 0; i < image.Height; i )
{
int count = 0;
for (int j = 0; j < image.Width; j )
{
Color color = image.GetPixel(j, i);
maindata[count] = color.R;
maindata[count 1] = color.G;
maindata[count 2] = color.B;
count = 3;
}
byte[] processGray = Gray.GrayFilter(maindata, image.Width);
alldata.AddRange(processGray);
}
}
if (btn_thresholdWasClicked == true)
{
alldata.Clear();
int thresh= Convert.ToInt32(tb_contrast.Text);
Threshold.ApplyThreshold(alldata.ToArray(),1920,thresh);
for (int i = 0; i < image.Height; i )
{
int count = 0;
for (int j = 0; j < image.Width; j )
{
Color color = image.GetPixel(j, i);
maindata[count] = color.R;
maindata[count 1] = color.G;
maindata[count 2] = color.B;
count = 3;
}
byte[] processthreshold = Threshold.ApplyThreshold(maindata, image.Width,thresh);
alldata.AddRange(processthreshold);
}
}
}
private bool btn_grayWasClicked = false;
private bool btn_thresholdWasClicked = false;
private void btn_gray_Click(object sender, EventArgs e)
{
btn_grayWasClicked = true;
}
private void btn_threshold_Click(object sender, EventArgs e)
{
btn_thresholdWasClicked = true;
}
private void btn_contrast_Click(object sender, EventArgs e)
{
int kontrast = Convert.ToInt32(tb_contrast.Text);
Contrast.contrast(alldata.ToArray(),1920,kontrast);
}
private void button2_Click(object sender, EventArgs e)
{
unsafe
{
byte[] a = new byte[691200];
a = alldata.ToArray();
Bitmap image = new Bitmap(1920, 360);
Rectangle rect = new Rectangle(0, 0, 640, 360);
BitmapData bmpData = image.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
IntPtr ptr = bmpData.Scan0;
byte* ptr0 = (byte*)ptr.ToPointer();
for (int i = 0; i < 360; i = i 3)
{
for (int j = 0; j < 1920; j )
{
a[i]=ptr0[i];
}
}
Marshal.Copy(a, 0, ptr, 691200);
image.UnlockBits(bmpData);
picfilter.Image = image;
}
}
}
}
Here is the code for Threshold.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace imageprocessing
{
class Threshold
{
public static byte[] ApplyThreshold(byte[] data, int width,int thresh)
{
List<byte> threshold = new List<byte>();
byte[] all = data.ToArray();
for (int j = 0; j < width-1; j )
{
if (all[j] > thresh)
{
threshold.Add(all[j]);
}
else
threshold.Add(all[j]);
}
return data;
}
}
}
CodePudding user response:
Instead of using:
int thresh= Convert.ToInt32(tb_contrast.Text);
You should use the int.TryParse()
for example:
int thresh;
// this is a better method to check if the textbox contains a valid integer.
if(!int.TryParse(tb_contrast.Text, out thresh))
thresh = 20; // enter here your default value.
// you should also check the range of the value, never trust user input
// in this case you are only comparing, Always good to be critical on it
if (thresh > 255)
thresh = 255;
else if (thresh < 0)
thresh = 0;
To answer the title in your question.
"How can I get default value for threshold with constructor in C#?"
Your threshold class doesn't have a constructor. There is just one static method. So it's more like a helper class.