• Welcome to the PopMalt Forums! Whether you're new to forums or a veteran, welcome to our humble home on the web! We're a 20-year old forum community with thousands of discussions on entertainment, lifestyle, leisure, and more.

    Our rules are simple. Be nice and don't spam. Registration is free, so what are you waiting for? Join today!.

set certain pixels to a colour, but not others

toocool

Registered Member
here's what i'm doing:

i'm updating my 'camcap' application. it uses a webcam and displays a live feed in the picturebox.

the update will enable the user to create frames and use a particular colour as a 'transparency key'. in other words, in the picture attached, the red regions will be replaced with whatever is being by the webcam, while the black regions remain unchanged

the problem: it treats the ENTIRE image (including the red section) as not red. and since red is in this case the transparency key, the red is not replaced by the webcam image.




here's the code:

Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim pic2 = cameracap.CurrentCamera.GetCurrentImage
        Dim pic As Bitmap = New Bitmap(My.Computer.FileSystem.SpecialDirectories.MyPictures & "\test.png")
        If pic2 IsNot Nothing Then
            Dim x As Integer = 1
            Dim y As Integer = 1
            For x = 1 To pic2.Width - 1
                For y = 1 To pic2.Height - 1
                    Dim col As Color = pic.GetPixel(x, y)
                    If col = Color.Red Then
                        Dim chg As Color = pic2.GetPixel(x, y)

                        pic.SetPixel(x, y, chg)


                    End If
                Next
            Next
            PictureBox1.Image = pic
        End If

    End Sub

please help
 

Attachments

toocool

Registered Member
i fixed the issue. kinda


except it changes half the red section, instead of none of it. its better, but not good enough for distribution


code is same as above, except i replaced if col = color.red with If col.R > 100

this gives the application 'tolerance', and it's not searching for one specific shade of colour (e.g. R=28 G=254 B=0).

please help me fix this bug (snapshot of what happens is available in the attachments. its called 'bug.jpg')


EDITED:

fixed! yay! there's a lesson to be learnt here:

don't go asking for help unless you have spent more than 2 minutes (literally) troubleshooting every possiblity that you can think of because it can take longer than 2min to work out what was wrong

anyway, the problem was the image size. it was supposed to be 320 by 240 pixels, not double that.


anyway, for those of you with the same troubles that i had, here's the code:

Code:
 Dim pic2 = cameracap.CurrentCamera.GetCurrentImage
        Dim pic As Bitmap = New Bitmap(My.Computer.FileSystem.SpecialDirectories.MyPictures & "\test.png")
        If pic2 IsNot Nothing Then
            Dim x As Integer = 1
            Dim y As Integer = 1
            For x = 1 To pic2.Width - 1
                For y = 1 To pic2.Height - 1
                    Dim col As Color = pic.GetPixel(x, y)
                    If col.R > 100 Then
                        Dim chg As Color = pic2.GetPixel(x, y)

                        pic.SetPixel(x, y, chg)

                        Me.Text = pic2.Width & ", " & pic2.Height 'testing purposes only. tells you the size that the frame (background image) must be
                    End If
                Next
            Next
            PictureBox1.Image = pic
        End If
i am working on a reverse version, where you sit against a wall and the program takes out the background and replaces it with another picture (kinda similar macintosh's photobooth thing)
 

Attachments

Last edited:
Top