Posts Tagged ‘Count

29
Apr
09

[PHP] Hit Counter using MySQL Database

Little script I wrote this morning which uses a database to store number of hits on a page:

<?php
	$dbhost = '1';
	$dbuser = '2';
	$dbpass = '3';
	$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('0');
	$dbname = '4';
	mysql_select_db($dbname);

	$page = $_SERVER['REQUEST_URI'];
	$result = mysql_query("SELECT HIT_COUNT FROM  Hit_Count WHERE Page_Name = '$page'");

	$any = false;
	while($row = mysql_fetch_array($result)) {
		$any = true;
		$counter = mysql_result($result, 0) + 1;
		$sql = "UPDATE Hit_Count SET HIT_COUNT = '$counter' WHERE Page_Name = '$page'";
		mysql_query($sql);
		echo($counter);
	}

	if($any == false) {
		$sql = "INSERT INTO Hit_Count (Page_Name, HIT_COUNT) VALUES ('$page', '1')";
		mysql_query($sql);
		echo("1");
	}

	mysql_close($conn);
?>

The script will output the current hit count, you would use the script like this:

<?php include("location/hitcount.php"); ?>

The tables set up is this:

Page_Name [text]		HIT_COUNT [int(11)]
/files/Image%20Gallery.php 	2
/ 				4
Advertisements
30
Mar
09

[VB.Net] GetPixelColourCount

Decided to turn my solution to the question here into an easy to use function.
It uses the goto function, which I feel is frowned upon, but go ahead and complain ^_^

    ''' <summary >
    ''' Function to count the number of pixels of the same colour
    ''' </summary >
    ''' <param name="bmpImage" > The image as a Bitmap that you wish to count the pixels of</param >
    ''' <param name="Bounds" > The rectangle for the area that you wish to calculate</param >
    ''' <returns > A list of strings containing the pixels colour and the number of pixels of that colour</returns >
    ''' <remarks > sim0n</remarks >
    Private Function GetPixelColourCount(ByVal bmpImage As Bitmap, ByVal Bounds As RectangleF) As List(Of String())
        Dim Pixels As New List(Of String())
        For x As Integer = Bounds.Left To Bounds.Right - 1
            For y As Integer = Bounds.Top To Bounds.Bottom - 1
                Dim Found As Boolean = False
                For Each str As String() In Pixels
                    If str(0) = CStr(bmpImage.GetPixel(x, y).ToArgb) Then
                        str(1) = CStr(CInt(str(1)) + 1)
                        Found = True
                        GoTo pixelFound
                    End If
                Next
                Pixels.Add(New String() {CStr(bmpImage.GetPixel(x, y).ToArgb), 1})
pixelFound:
            Next
        Next
        Return Pixels
    End Function

Usage:
Using image: http://privatewww.essex.ac.uk/~sjs/research/iso_colour_blocks.png

        ''Get our image from PictureBox1
        Dim btmp As New Bitmap(PictureBox1.Image)
        ''Set the cursor to wait
        Me.Cursor = Cursors.WaitCursor
        ''Loop through the results - Rectangle is the entire image
        For Each str As String() In GetPixelColourCount(btmp, New RectangleF(0, 0, btmp.PhysicalDimension.Width, btmp.PhysicalDimension.Height))
            Debug.WriteLine(Color.FromArgb(str(0)).ToString & " | Count: " & str(1))
        Next
        ''Reset Cursor
        Me.Cursor = Cursors.Default

Would output:
Color [A=255, R=85, G=85, B=85] | Count: 65536
Color [A=255, R=0, G=255, B=0] | Count: 32768
Color [A=255, R=127, G=0, B=128] | Count: 32768
Color [A=255, R=0, G=0, B=255] | Count: 32768
Color [A=255, R=127, G=128, B=0] | Count: 32768
Color [A=255, R=255, G=0, B=0] | Count: 32768
Color [A=255, R=0, G=127, B=128] | Count: 32768

You can also just select regions of the image to calculate using the Bounds parameter, for example to get the colour of just one square, you could do:

  ''64 Pixels along
  ''0 Pixels down
  ''Width 64 pixels
  ''Heigh 64 Pixels
  For Each str As String() In GetPixelColourCount(btmp, New RectangleF(64, 0, 64, 64))
      Debug.WriteLine(Color.FromArgb(str(0)).ToString & " | Count: " & str(1))
  Next

Would output:
Color [A=255, R=127, G=128, B=0] | Count: 4096

30
Mar
09

[VB.Net] Q: Get the amount and colour of pixels in an Image

The question asked “How do i get the amount and colour of pixels in an Image?”

The Solution
I had to think a bit about this one, dealing with image processing needs to be as efficent as possible, however I couldnt see a better way than just looping through the pixels and adding them to a list.
Here is the solution that I posted to the users project:

    Dim PixelList As New List(Of String())

    Public Sub ColourInList(ByVal Colour As Color)
        Dim Found As Boolean = False
        For Each str As String() In PixelList
            If str(0) = CStr(Colour.ToArgb) Then
                str(1) = CStr(CInt(str(1)) + 1)
                Found = True
                Exit For
            End If
        Next
        If Found = False Then PixelList.Add(New String() {CStr(Colour.ToArgb), 1})
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Dim btmp As New Bitmap(PictureBox1.Image)
        For x As Integer = 0 To btmp.Width - 1
            For y As Integer = 0 To btmp.Height - 1
                ColourInList(btmp.GetPixel(x, y))
            Next
        Next
        For Each str As String() In PixelList
            Debug.WriteLine(Color.FromArgb(str(0)).ToString & " | Count: " & str(1))
        Next
    End Sub

Would Output:
Using this Image:
http://privatewww.essex.ac.uk/~sjs/research/iso_colour_blocks.png

Color [A=255, R=85, G=85, B=85] | Count: 65536
Color [A=255, R=0, G=255, B=0] | Count: 32768
Color [A=255, R=127, G=0, B=128] | Count: 32768
Color [A=255, R=0, G=0, B=255] | Count: 32768
Color [A=255, R=127, G=128, B=0] | Count: 32768
Color [A=255, R=255, G=0, B=0] | Count: 32768
Color [A=255, R=0, G=127, B=128] | Count: 32768