Jump to content

Help please on array population inside a loop

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
mattybennett

mattybennett

    Newbie

  • Members
  • Pip
  • 8 posts
Hi all,

I'm hoping some kind soul can help me with this difficult problem. I'm using VB.Net, Visual Studio 2008 making a Windows Forms application. I have a FOR NEXT loop (actually one nexted inside the other) which scans across a bitmap image and retrieves data from the image, pixel by pixel. This data then needs to be stored into a series of arrays. At this stage I have got it all working using a structure section, but am trying to replace that. I also need the array data to be available publicly across the project.

I have removed the Structure section and instead declared the arrays as public arrays the size that there are pixels. Now, I cannot seem to place data into the array slots. Can anyone help with highlighting the error? I suspected it was because the loop might be going from pixel 1 to 9...

Secondly, It would be extremely helpful if somebody could show me how to adapt the code so that I can save to a 2-dimensional array (rows,columns).

Thirdly, I need to display the contents of the array on the form and allow the user to change values, instantly updating the array. Can I do this with a datagrid?

Also any obvious glitches please let me know.

Code follows below:

Imports System

Imports System.Data

Imports System.Data.SqlClient

Imports System.IO

Imports System.Drawing.Bitmap

Imports System.Drawing.Imaging.Metafile

Imports Microsoft.VisualBasic

Public Class Imagereader

    Public strFileName As String

    Public image1 As Bitmap

    Public DidWork, pixels As Integer

    Public pixelColor As Color

    Protected pixa(pixels - 1), pixr(), pixg(), pixb(), pixgrey(), lux(), Y(), U(), V(), pixcount As Integer

    Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        OpenFD.InitialDirectory = "C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\Hex_Lux Calculator\RGB tests"

        OpenFD.Title = "Open a Bitmap File"

        OpenFD.FileName = ""

        OpenFD.Filter = "Bitmap Files|*.bmp"

        DidWork = OpenFD.ShowDialog()

        If DidWork = DialogResult.Cancel Then

            MsgBox("Cancel Button Clicked")

        Else

            strFileName = OpenFD.FileName

            ' Retrieve the image.

            image1 = New Bitmap(strFileName, True)

            PictureBox1.Image = image1

            ' Display the pixel format in Label1.

            Label2.Text = "Pixel format: " + image1.PixelFormat.ToString()


            ' Loop through the images pixels to reset color.

            pixels = image1.Width * image1.Height

            pixcount = 0


            MsgBox("number of pixels in the image: " & (pixels))

            For col = 1 To (image1.Height)

                For row = 1 To (image1.Width)


                    ' count status - used for checking through the 2 loops

                    ' MsgBox("y: " & (y) & " x: " & (x))


                    pixelColor = image1.GetPixel(row, col)


                    pixa(pixcount) = pixelColor.A

                    'pixr(pixcount) = pixelColor.R

                    'pixg(pixcount) = pixelColor.G

                    'pixb(pixcount) = pixelColor.B

                    'pixgrey(pixcount) = ((Int(pixelColor.R) + Int(pixelColor.G) + Int(pixelColor.B)) / 3)

                    'Y(pixcount) = (0.299 * pixr(pixcount) + (0.587 * pixg(pixcount)) + (0.114 * pixb(pixcount)))

                    'U(pixcount) = Math.Round(0.492 * (pixb(pixcount) - Y(pixcount)))

                    'V(pixcount) = Math.Round(0.877 * (pixr(pixcount) - Y(pixcount)))

                    'lux(pixcount) = ((pixgrey(pixcount) / 255) * 100)

                    'image1.SetPixel(row, col, Color.FromArgb(pixa(pixcount), pixr(pixcount), pixg(pixcount), pixb(pixcount)))


                    'set conditions - find the red pixel and colour it white

                    'If image1.GetPixel(x, y) = Color.FromArgb(255, 255, 0, 0) Then

                    '    image1.SetPixel(x, y, Color.White)

                    'End If


                    pixcount = pixcount + 1

                Next

            Next

            'MsgBox("Pixel count: " & (pixcount))

            PictureBox2.Image = image1

        End If

    End Sub 

-------------

Best regards,

Matthew Bennett

Btw the commented out sections are old. The loop variables used to be 'x' and 'y', but are now 'row' and 'col'.

Edited by Vswe, 07 March 2010 - 02:43 PM.
Don't double post, edit instead. Also use code tags for codes


#2
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
I don't have time to look through your code right now but these tutorials might help:
http://forum.codecal...-10-arrays.html
http://forum.codecal...nal-arrays.html

#3
mattybennett

mattybennett

    Newbie

  • Members
  • Pip
  • 8 posts
Thank you for the links. I will go over these tonight. Hopefully will cover what I need.

Best regards,

Matthew

#4
mattybennett

mattybennett

    Newbie

  • Members
  • Pip
  • 8 posts
I must say that the guide you mentioned was very helpful. This section has helped a little:

http://forum.codecal...18-classes.html

Now for an obvious question: I want to view the contents of the 2D array I'm working on. I realise I can look at it in the debugger, but would like a table on the form I am making, and to be able to scroll across and edit the values. Can this be done? I can't see a 'tables' option in the toolbox and DataGrid seems to be for working with SQL which I don't really want to do.

Kind regards,

Matthew

#5
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
Do you want to show the colors themselves or just their textvalues?

#6
mattybennett

mattybennett

    Newbie

  • Members
  • Pip
  • 8 posts
Hi,

I'm looking for the RGB values seperately from each pixel in the image, as a 2D array, as values from 0-255. Once parsed (is this the correct term?) I need to convert to YUV.

The image will then be redrawn from the RGB array data onto the form. At this point I then need to record mouse positions as I want the user to select from a starting pixel to and ending pixel. Reading across the row the pixels area stored out as a number sequence which can be used for mapping.

The code I have provided is part of a larger colourisation application I am building. Where the greyscale (Y value) matches the profile (mouse selection) roughly then this area will be colourised. Colour data is stored in a seperate table and linked to the mouse selections. I already have the code for the colour fill section as C++ and am rewriting this for VB at present (uses a threshold transformation on the picture to define the edge of the shape.)

I hope this makes sense and clairifes what I'm trying to achieve.

Thanks,

Matthew

#7
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
I think I get what you're trying to do but for me it sounds that you want to draw the image on the form but before you said you wanted to just write the data from the 2d array, not actually draw it. Can you please clarify what you want to print out to the form(what the user will see)?

#8
mattybennett

mattybennett

    Newbie

  • Members
  • Pip
  • 8 posts
In this section of the application, the Image reader, the intention is to open the file and parse the data from the BMP to the arrays. The user would need to see the values of each 2D array and move through the values. I'm imagining an Excel-like table in the middle of a large form.

That said it would be helpful to have a thumbnail of the image on an imageviewer object as well, with a little red dot so that when a certain cell is selected in the table this is reflected on the thumbnail so that the user knows whereabouts they are (a bit like the navigator pane on Photoshop).

http://colourisation...imagereader.jpg

With that said I will have to take the array data and put it into an imageviewer, but I know how to do this - this bit's working.

#9
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
I'm not sure what to use but after a quick look in the toolbox I found "DataGridView" which is maybe what you're looking for, if it is here's an example I did very fast:


        Dim myArray(,) As Integer = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
        For i As Integer = 0 To myArray.GetUpperBound(0)
            DataGridView1.Columns.Add("myName", "myHeader")
        Next
        For j As Integer = 0 To myArray.GetUpperBound(1)
            DataGridView1.Rows.Add()
        Next

        For i As Integer = 0 To myArray.GetUpperBound(0)
            For j As Integer = 0 To myArray.GetUpperBound(1)
                DataGridView1.Item(i, j).Value = myArray(i, j)
            Next
        Next

This will add the content of an array to the DataGridView, however I'm not really sure how a DataGridView's working. :)

#10
mattybennett

mattybennett

    Newbie

  • Members
  • Pip
  • 8 posts
This is extremely helpful - thank you.

In my program the array is populated within two FOR loops as (paraphrasing):

For columns = 0 to image.width(-1)
For rows = 0 to image height(-1)
' counter is also present to determine the pixel we are looking at
R(counterposition) = image.colour.R ' (amount of Red in the pixel at this point of the parse)
next
next

Can the form table be populated at the same time? Surely it would make more sense if the table was bound to the R array? If so how can I accomplish this?

Secondly I'm looking to improve the code to store the above as a 2D array (row, height). How can I adapt this, please?

Kind regards,

Matthew

#11
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
Just change R to be a 2d array,
Dim R(,) as Integer


And then combine the loops:
For columns = 0 to image.width(-1)
For rows = 0 to image height(-1)
' counter is also present to determine the pixel we are looking at
R(columns,rows) = image.colour.R ' (amount of Red in the pixel at this point of the parse)
 DataGridView1.Item(columns, rows).Value = image.colour.R
next
next




Also remember to use Code tags(the # button) when posting codes.

#12
mattybennett

mattybennett

    Newbie

  • Members
  • Pip
  • 8 posts
Ah!!

oK - i'll try that.

Thank you.