hi,
i need some help here, i have an geometry image example diamond with boundary lines without fill, i need to get all the black pixels coordinate of the lines.
can someone help me to give an ideas and suggestion?
thank you so much
aznimah
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
FroglegPosted Jan 13, 2011, 12:14 AM
private void bttnSave_Click(object sender, EventArgs e)
{
if (coordinate.Items.Count < 1)
{
MessageBox.Show("Please generate vertices");
return;
}
saveFileDialog1.Filter = "Text files (*.txt)|*.txt";
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName ))
{
foreach (string itm in coordinate.Items)
sw.WriteLine(itm);
}
}
}
Noor Aznimah Abdul AzizPosted Jan 13, 2011, 2:32 AM
Noor Aznimah Abdul AzizPosted Jan 12, 2011, 11:36 PM
here is my code,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Collections;
using System.IO;
//c corner
namespace gettyPixel
{
public partial class Form1 : Form
{
private Bitmap OriginalImage;
// private Bitmap imgOut;
private double BWThresh ;
private Color cblack = System.Drawing.Color.FromArgb(255, 0, 0, 0);
private Color cwhite = System.Drawing.Color.FromArgb(255, 255, 255, 255);
//private ArrayList outList;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
BWThresh = 0.5;
}
private void bttnOpen_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Image Files(*.PNG;*.JPG;*.GIF)|*.PNG;*.JPG;*.GIF";
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
pictureBox1.Image = null;
Image img = Image.FromFile(openFileDialog1.FileName);// load image
OriginalImage = new Bitmap(img.Width, img.Height);
Graphics g = Graphics.FromImage(OriginalImage);// draw to bitmap
g.DrawImage(img, 0, 0);
pictureBox1.Image = Img2BW(OriginalImage, BWThresh);
}
}
private void bttnVertex_Click(object sender, EventArgs e)
{
/*
display all pixels coordinate in listbox
*/
ArrayList pxList = scan(OriginalImage);
ShowVertexList(pxList);
label1.Text = ("Listbox1 count = " + coordinate.Items.Count);
}
/* Convert the image into binary image*/
// --> create a new B/W image from source image with brightness threshold = 0.5
// --> this action is to make sure we only deal with black and white color
private Bitmap Img2BW(Bitmap imgSrc, double threshold)
{
int width = imgSrc.Width;
int height = imgSrc.Height;
int px; double br;
Color pixel;
Bitmap imgOut = new Bitmap(imgSrc);
for (int row = 0; row < height - 1; row++)
{
for (int col = 0; col < width - 1; col++)
{
pixel = imgSrc.GetPixel(col, row);
px = pixel.ToArgb();
br = pixel.GetBrightness();
if (pixel.GetBrightness() < threshold)
imgOut.SetPixel(col, row, cblack);
else
imgOut.SetPixel(col, row, cwhite);
}
}
return imgOut;
}
/* Show the pixels coordinate in listBox*/
private void ShowVertexList(ArrayList al)
{
/*
* Show the vertex in ListBox
*/
coordinate.Items.Clear();
Vertex prv;
for (int k = 0; k < al.Count; k++)
{
prv = (Vertex)al[k];
coordinate.Items.Add("{" + prv.X.ToString() + "," + prv.Y.ToString() + "}");
}
}
private ArrayList scan(Bitmap imgSrc)
{
pictureBox1.Refresh();
Graphics g = pictureBox1.CreateGraphics();
ArrayList pixels = new ArrayList();
for (int j = 0; j < imgSrc.Height; j++)
{
for (int i = 0; i < imgSrc.Width; i++)
{
if (imgSrc.GetPixel(i, j) == cblack)
{
Vertex pix = new Vertex(i, j);
pixels.Add(pix);
if (checkBox1.Checked == false)
{
g.FillRectangle(Brushes.Red, i, j, 2, 2);
}
}
}
}
return pixels;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
BWThresh = Convert.ToDouble(numericUpDown1.Value);
}
private void button1_Click(object sender, EventArgs e)
{
saveFileDialog1.ShowDialog();
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
if (saveFileDialog1.FileName != "")
{
StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);
sw.WriteLine(coordinate.Items);
sw.Close();
}
}
}
}
but it does not return any file,
thanks
aznimah
Noor Aznimah Abdul AzizPosted Jan 12, 2011, 9:37 AM
i already try it ;) it return all the black pixels coordinate right. thanks for your help on my code. really thank you for your help, thank you so much ;)
FroglegPosted Jan 12, 2011, 8:55 AM
Noor Aznimah Abdul AzizPosted Jan 12, 2011, 6:27 AM
thanks for the reply,
what im trying to do is:
1) load an image
2) convert image to binary image so that the pixels color would be only black and white [0 or 1]
2) get display in listbox all the pixels coordinate which color black [1] only
i have try to code earlier but the code is not working, here is the code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace PixelsCoordinate
{
public partial class Form1 : Form
{
private Bitmap OriginalImage;
private Bitmap imgOut;
private double BWThresh = 0.5;
private Color cblack = System.Drawing.Color.FromArgb(255, 0, 0, 0);
private Color cwhite = System.Drawing.Color.FromArgb(255, 255, 255, 255);
private ArrayList outList;
public Form1()
{
InitializeComponent();
}
private void buttonOpen_Click(object sender, EventArgs e)
{
openFileDialog1.FileName = "";
openFileDialog1.Title = "Images";
openFileDialog1.Filter = "All Images|*.png; *.bmp; *.png";
openFileDialog1.ShowDialog();
if (openFileDialog1.FileName.ToString() != "")
{
pictureBox1.ImageLocation = openFileDialog1.FileName.ToString();
OriginalImage = new Bitmap(openFileDialog1.FileName.ToString());
}
}
private void buttonVertex_Click(object sender, EventArgs e)
{
/*
* to convert image to binary image and display all pixels coordinate in listbox
*/
Bitmap imgSrc = new Bitmap(OriginalImage);
Bitmap imgOut = new Bitmap(imgSrc.Width, imgSrc.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
// --> create a new B/W image from source image with brightness threshold = 0.5
// --> this action is to make sure we only deal with black and white color
imgOut = Img2BW(imgSrc, BWThresh);
ArrayList pxList = scan(imgOut);
ShowVertexList(pxList);
}
/* Convert the image into binary image*/
private Bitmap Img2BW(Bitmap imgSrc, double threshold)
{
int width = imgSrc.Width;
int height = imgSrc.Height;
int px; double br;
Color pixel;
Bitmap imgOut = new Bitmap(imgSrc);
for (int row = 0; row < height - 1; row++)
{
for (int col = 0; col < width - 1; col++)
{
pixel = imgSrc.GetPixel(col, row);
px = pixel.ToArgb();
br = pixel.GetBrightness();
if (pixel.GetBrightness() < threshold)
imgOut.SetPixel(col, row, cblack);
else
imgOut.SetPixel(col, row, cwhite);
}
}
return imgOut;
}
/* Show the pixels coordinate in listBox*/
private void ShowVertexList(ArrayList al)
{
/*
* Show the vertex in ListBox
*/
Vertex prv;
for (int k = 0; k < al.Count; k++)
{
prv = (Vertex)al[k];
coordinate.Items.Add("{" + prv.X.ToString() + "," + prv.Y.ToString() + "}");
}
}
private ArrayList scan(Bitmap imgSrc)
{
coordinate.Items.Clear();
Bitmap bp = new Bitmap(imgSrc);
ArrayList pixels = new ArrayList();
for (int j = 0; j < bp.Height; j++)
{
for (int i = 0; i < bp.Width; i++)
{
if (bp.GetPixel(i, j) == cblack)
{
Vertex pix = new Vertex(i, j);
pix.AddPixels(v);
}
}
}
return pixels;
}
//private
class Vertex
{
ArrayList vertices = new ArrayList();
private int x;
private int y;
public Vertex(int i, int j)
{
this.X = i;
this.Y = j;
}
public void insert(Vertex v)
{
vertices.Add(v);
}
public ArrayList GetPixels()
{
return vertices;
}
public int GetCount()
{
return vertices.Count;
}
public void AddPixels(Vertex v)
{
vertices.Add(v);
}
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
}
}
}
really need help on this urgently, thank you so much
aznimah
FroglegPosted Jan 12, 2011, 6:14 AM
Bitmap myBitmap = new Bitmap(yourWidth,yourHeight);
Color pixelColor = myBitmap.GetPixel(myX, myY);
int red = pixelColor.R;
int blue = pixelColor.B;
int green = pixelColor.G;
Then loop through myBitmap and check if red,blue and green = 0* and if so record myX and myY.
*You may need to check value between 0 & 10 to give yourself a bit of leeway