我正在尝试开发一个 CBIR 应用程序,因此我正在获取输入图像,在三个不同的变量中计算其 RGB 值并修改这些值,然后将它们存储在三个变量中:r1
, g1
, b1
.
然后我试图将这些值传递给数据库,以便我可以通过使用比较运算符比较它们来检索图像,但是我无法获取任何数据,尽管我认为我以正确的方式使用参数。
这是我的代码:
double r1, r2, g1, g2, b1, b2;
r1 = aveR -(.1 * aveR);
r2 = aveR +(.1 * aveR);
g1 = aveG -(.1 * aveG);
g2 = aveG +(.1 * aveG);
b1 = aveB -(.1 * aveB);
b2 = aveB +(.1 * aveB);
cn = new SqlConnection(@"Data Source=HOME;Initial Catalog=test_image;Integrated Security=True");
cmd = new SqlCommand("select path from image where red BETWEEN @r1 AND @r2 AND green BETWEEN @g1 AND @g2 AND blue BETWEEN @b1 AND @b2" , cn);
cmd.Parameters.AddWithValue("@r1", r1);
cmd.Parameters.AddWithValue("@r2", r2);
cmd.Parameters.AddWithValue("@g1", g1);
cmd.Parameters.AddWithValue("@g2", g2);
cmd.Parameters.AddWithValue("@b1", b1);
cmd.Parameters.AddWithValue("@b2", b2);
cn.Open();
SqlDataReader dr;
try
{
dr = cmd.ExecuteReader();
while (dr.Read())
{
progressBar1.Value = progressBar1.Value + 9;
listBox1.Items.Add(dr["path"].ToString());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cn.Close();
}
/*after this i dont get any data in my list box, where i am expecting the paths of image("path" col in my DB) which should be returned after the comparison query.*/
这是填充的代码(希望我没有弄错,即此代码一张一张地获取图像,然后将它们插入数据库)。
int width = 256;
int height = 128;
int sumr = 0, sumg = 0, sumb = 0;
int rank = 0;
openFileDialog1.Filter = "All Images|*.jpg; *.bmp; *.png; *.gif";
DialogResult dr = openFileDialog1.ShowDialog();
listBox1.Items.Add("Pixel RED GREEN BLUE ");
listBox2.Items.Add("avgR avgG avgB ");
if (dr == System.Windows.Forms.DialogResult.OK)
{
foreach(string file in openFileDialog1.FileNames)
{
try
{
Image img = Image.FromFile(file);
Image img1 = Resizeimage(img, width, height);
pictureBox1.Image = img1;
textBox1.Text = file;
Bitmap img2 = new Bitmap(img1);
Color c;
for (int i = 0; i < img2.Width; i++)
{
for (int j = 0; j < img2.Height; j++)
{
c = img2.GetPixel(i, j);
int r = Convert.ToInt16(c.R);
int g = Convert.ToInt16(c.G);
int b = Convert.ToInt16(c.B);
sumr = sumr + r;
sumg = sumg + g;
sumb = sumb + b;
listBox1.Items.Add(i.ToString() + "," + j.ToString() + " " + r.ToString() + " " + g.ToString() + " " + b.ToString());
}
}
int total = img2.Height * img2.Width;
int aveR = sumr / total;
int aveG = sumg / total;
int aveB = sumb / total;
listBox2.Items.Add(aveR.ToString() + " " + aveG.ToString() + " " + aveB.ToString());
Application.DoEvents();
Thread.Sleep(5000);
SqlConnection con = new SqlConnection(@"Data Source=HOME;Initial Catalog=test_image;Integrated Security=True");
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] pic_arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(pic_arr, 0, pic_arr.Length);
SqlCommand cmd = new SqlCommand("insert into image(path,image,red,green,blue,rank)values(@path,@image,@red, @green, @blue, @rank)", con);
cmd.Parameters.AddWithValue("@path", textBox1.Text);
cmd.Parameters.AddWithValue("@image", pic_arr);
cmd.Parameters.AddWithValue("@red", aveR);
cmd.Parameters.AddWithValue("@green", aveG);
cmd.Parameters.AddWithValue("@blue", aveB);
cmd.Parameters.AddWithValue("@rank", rank);
con.Open();
try
{
int rs = cmd.ExecuteNonQuery();
if (rs > 0)
{
MessageBox.Show("image stored successfully");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("cannot display the image"+ ex.Message);
}
listBox1.Items.Clear();
listBox2.Items.Clear();
}
}
}
public static Image Resizeimage(Image img, int width, int height)
{
Image newimg = new Bitmap(width, height);
Graphics g = Graphics.FromImage(newimg);
g.DrawImage(img, 0, 0, width, height);
return newimg;
}
/** I am sorry if braces make you all little upset, I am new to this site, so editing might be poor**/